Page 1 of 1

If statement

Posted: 24 Jun 2014, 10:22
by davemunro
Good morning,
New to the forums so hello to all! We use Sage Accounts Professional 2014
I'm trying (in vain so far) to create an expression:
I have two separate expressions which I want to combine into a single if statement expression
expr1 StringToInteger(SOP_ITEM.COMMENT_2) * StringToInteger(SOP_ITEM.EXT_ORDER_REF) * 6.3 / 1000000
expr2 STOCK.UNIT_WEIGHT * 6.3

If expr2 is less than or equal to 0 then use expr1, if the value is less than 0.26 then set the result to 0.26

The context of the expressions is to calculate square meter rates of a particular item, setting the minimum rate to 0.26 if the result is less than that..

I hope I've explained that well enough.

Thanks in advance.

Dave

Re: If statement

Posted: 24 Jun 2014, 11:22
by brucedenney
The if function is confusing.

Because there are 2 of them, one the "if" is only used in properties.

In expressions you need to use the ? : version

Condition ? Instruction 1 : Instruction 2

In your case you need to nest conditions

Condition 1 ? Instruction 1 : Condition 2 ? Instruction 2 : Instruction 3

Re: If statement

Posted: 25 Jun 2014, 22:32
by Geordie
Dave

Having looked at what you have asked, you could combine the 2 expressions into one such as:

STOCK.UNIT_WEIGHT * 6.3 <= 0 ? (StringToInteger(SOP_ITEM.COMMENT_2) * StringToInteger(SOP_ITEM.EXT_ORDER_REF) * 6.3 / 1000000 < 0.26 ? 0.26 : StringToInteger(SOP_ITEM.COMMENT_2) * StringToInteger(SOP_ITEM.EXT_ORDER_REF) * 6.3 / 1000000) : 0.26

However I am not at my Sage Computer to test this out in practice.
The easiest solution is to do a 3rd expression which references EXPR1 and EXPR2, such as:

EXPR2 <= 0 ? (EXPR1 < 0.26 ? 0.26 : EXPR1) : 0.26

This is saying that if Expression 2 is equal or less than 0 then to look at Expression1, there is another if in brackets, looking at the result of expression 1, if this is less than 0.26, then return 0.26, otherwise give the value returned for Expression 1 (which will be 0.26 or greater) otherwise just return 0.26.

However, the flaw in this is that Expression 2 could return a value for example 0.1 in which case the result of the 3rd expression will be 0.1.

Its difficult to give an accurate expression without looking at a sample of what EXPR1 and EXPR2 return, however, I expect, EXPR2 never returns anything between 0 and 0.26 ?