3

We have a lovely AMPscript function for adding 2 numbers together.

Add(N1,N2)

However what if I want to add 4? I've tried it in ExactTarget and it give me an error that I can only add 2 numbers. Obviously I can try (x + y) + (a + b) but at the end I will need to add up about 12 numbers. (this is going to be a receipt of a purchase email)

Is there any way to add multiple (more than 2) numbers so I will not need to create a vastly complicated and long AMPscript line?

3
  • How are you generating each line in that receipt? The typical approach to this is to have a running total - that is, after every output/print of an amount, you'd add it to a variable. This way, at the end, the variable's value is equal to the total. Commented Sep 10, 2014 at 16:50
  • thanks for the input edu8rdo. whats the upside of having a running total instead of just adding everything up at the end? the numbers for each item in the receipt is coming from another system. And all I need to do is to put in the attribute at the end of each line. (promo prices are coming as a full number also and not as like -20%)
    – Balint
    Commented Sep 10, 2014 at 17:05
  • Hi Balint - just to clarify, I don't mean you should have a display of the running total in the email/receipt, but instead added to a variable during every iteration of the line items. Below in the answers section, @ryan-bridges covers the idea way to do this - via a loop. However, if I'm reading your question and follow up correctly, it sounds like there's a different column for each number? Is that right? Commented Sep 10, 2014 at 20:31

1 Answer 1

1

Depending on how you have your numbers coming in you could possibly create a loop and add the next number to the running total:

%%[
    VAR @array, @rows, @i, @row, @value, @total
    SET @total = 0 
    SET @array = Concat(1, "|", 3, "|", 10, "|", 33, "|", 27, "|", 9) 
    SET @rows = BuildRowsetFromString(@array, '|')
    FOR @i = 1 to RowCount(@rows) DO 
        SET @row = Row(@rows, @i) 
        SET @value = Field(@row,1)
        SET @total = Add(@total, @value)
    NEXT @i
]%%

%%=v(@total)=%%

An alternative would be use Server-Side Javascript. That seems like it would be a lot easier for this situation.

5
  • Welcome to SFSE. Could you expand your answer with more information or snippets describing how you would "create a loop" or use "Server-Side Javascript"
    – Benj
    Commented Sep 10, 2014 at 17:46
  • Sure @Benj! I'm not sure how Balint is pulling the numbers, so I'll illustrate with an array, then build a rowset from the array, then iterate through the values adding them. %%[ VAR @array, @rows, @i, @row, @value, @total SET @total = 0 SET @array = Concat(1, "|", 3, "|", 10, "|", 33, "|", 27, "|", 9) SET @rows = BuildRowsetFromString(@array, '|') FOR @i = 1 to RowCount(@rows) DO SET @row = Row(@rows, @i) SET @value = Field(@row,1) SET @total = Add(@total, @value) NEXT @i ]%% %%=v(@total)=%% Commented Sep 10, 2014 at 20:10
  • Sorry for the ugly formatting - I'm new to posting on SFSE. That's the AMPscript version. The numbers I have in the array are just examples and the script will end up giving you a total of whatever is in there. For SSJS it's just javascript adding them together total = a + b + c + d + e; Commented Sep 10, 2014 at 20:18
  • Thanks @Ryan Bridges. One of the great things about SFSE is that you can actually go back and edit your posts, so I went ahead and included your code sample directly in the answer so it's easier to read.
    – Benj
    Commented Sep 11, 2014 at 14:19
  • Thanks @Benj. That's much better. And thanks for the pointers! Commented Sep 11, 2014 at 14:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .