Server Connect Set Value in a loop

Before I spend more time trying to get this to work, I just want to check I’m doing it right.

I have a loop in Server Connect which goes through all the records. I have a variable called ‘ordertotal’ which needs to hold the total of all the products.

I’ve used the ‘Set Value’ right at the beginning (above the loop) and set it to zero. I’ve then put another ‘Set Value’ with the same name in the loop and put in this value:

{{ordertotal}}+({{detailQty}}*{{queryProductDetails.sizePrice}})

but it’s just giving me zero.

Am I doing something wrong or is it a bug?

1 Like

Hey Jon,

Not a bug. You will want to also put “ordertotal” into the Global Name property of your Set Value actions. As you have it, you likely have a variable ordertotal both inside and outside your loop. Usisng a global name will make them one in the same.

Also, I believe your expression is wrong. I’m pretty sure you want this:

{{ ordertotal + ( detailQty * queryProductDetails.sizePrice ) }}

Hard to say if the dynamic values are correct since I don’t know what they represent, but the main point is that you can include the entire thing in one set of double curly braces.

1 Like

@mebeingken, you da man! Yep, my syntax was way off. I still can’t get my head around the {{ }} going around all the variables collectively rather than in each one.

Incidentally, can you explain how the Global Name works? Is there any harm in always putting a name in there? Should I also put a name in the Name field if I’m also using Global Name? Would be good to know the difference and how best to use them.

Thanks for the really fast and spot-on reply. :slight_smile:

1 Like

Please check Patrick’s explanation on how the global name works:

The syntax is pretty simple. If you want to do any math with dynamic expression, the syntax is always:

{{ you do math opeartions inside these }}

Wrong:

{{var1}} + {{var2}} + ({{var3}} * {{var4}}) / 7

Right:

{{var1 + var2 + (var3 * var4) / 7 }} 
3 Likes

Thanks @Teodor, that’s really helpful. I hadn’t seen @patrick’s explanation before and now wish I had!

1 Like