More than one IF parameter

I made up a ‘Low margin report’. To avoid the ‘divide by zero’ I use the following code:IF "Sales (LCY)" <> 0 THEN gDecMargin := "Profit (LCY)" / ("Sales (LCY)"/100); Now I want to add a second condition, to be able to have the gDecMargin calculated as a minus % if the “Profit (LCY)” is negative. In Excel I would use: IF("Profit (LCY)"<0;-"Profit (LCY)"/("Sales (LCY)"/100);"Profit (LCY)"/("Sales (LCY)"/100) How do I write this in C/AL?

gDecMargin := 0; IF "Sales (LCY)" <> 0 THEN BEGIN IF "Profit (LCY)< 0 THEN gDecMargin := -"Profit (LCY)"/("Sales (LCY)"/100) ELSE gDecMargin := "Profit (LCY)"/("Sales (LCY)"/100); END; Maybee? :slight_smile:

Thank you Johan! [:)]

Create your own “immediate-if” (IIF) function: function IIf(Condition: boolean; ValueIfTrue: decimal; ValueIfFalse: decimal): decimal begin case Condition of true: exit(ValueIfTrue); false: exit(ValueIfFalse); end; end; Now call this function as x := IIf(“Profit (LCY)”<0,-“Profit (LCY)”/(“Sales (LCY)”/100),“Profit (LCY)”/(“Sales (LCY)”/100) just like in Excel!

Sounds good, Jan. I will try it. Dank!

Hi Jan, I have experimented with the suggested code, but I can’t get it working. I’m only a humble end user so you have to help me some more. [:I]

  • Is it right that I should add your code to a function in the reports’ Function Tab in the C/AL Globals?
  • How should I name the function. Could it be just IIf?
  • If I do so, I receive an error saying that FUNCTION is an unknown variable

Michiel Why don’t you just open Programming manual and read this stuff. In other words - RTFM [;)] -

Michiel, 1. Create the function in the C/AL Globals; 2. Call it any valid function name (I would prefer IIf myself); 3. Add the following parameters: Condition: boolean; ValueIfTrue: decimal; ValueIfFalse: decimal; 4. Set the function’s return value type to decimal; 5. Copy the lines between begin and end from my sample code; 6. Paste this code (in the C/AL editor) in your function. Good luck!

quote:


Originally posted by Arthur
Why don’t you just open Programming manual and read this stuff. In other words - RTFM [;)]


Uhhhh, programming manual? All I had was two days training on reports & forms. I’m an end user, not a programmer, so [;)] And Jan, thanks again!

My problem with the IIF function proposed in this thread is that both ValueIfTrue and ValueIfFalse must be evaluated in order to pass the appropriate values to the IIF function. Granted this is not a big deal with the example given where we are only talking about a couple divisions. But if the function is used many times in a loop or if the expresssions that need to be evaluated involve significant processing then this could add significant additional processing to the entire process. In my opinion it is better to use IF … THEN … ELSE to evaluate the condition and then only the single expression that is required.

IF “Sales (LCY)” <> 0 THEN gDecMargin := ABS(“Profit (LCY)”) / (“Sales (LCY)”/100);