3

I have a for loop in SSJS and I am trying to output it outside this scope using AMPSCRIPT. I know there is a way to output the value of a SSJS variable into AMPSCRIPT. My question is, how can output all the values of the for loop into AMPSCRIPT?

SSJS Loop:

Setting variables to be use outside of SSJS:

for (var i = 0; i < 5; i++) 
{ 
    Platform.Variable.SetValue("client_id"+i, rows[i]['Count']);
}

AMPSCRIPT Output: I wanna avoid doing the code below: I am wondering if can do something like:

@client_id[i] instead of my current static solution:

var s1 = [
    %%=v(@client_id0)=%%,
    %%=v(@client_id1)=%%,
    %%=v(@client_id2)=%%,
    %%=v(@client_id3)=%%,
    %%=v(@client_id4)=%%,
];
2

I appears as though you are trying to use the server side to populate the front end javascript using this code:

var s1 = [
    %%=v(@client_id0)=%%,
    %%=v(@client_id1)=%%,
    %%=v(@client_id2)=%%,
    %%=v(@client_id3)=%%,
    %%=v(@client_id4)=%%,
];

In order to do that dynamically, you couldn't use AMPscript, as it doesn't allow for dynamic variables or arrays in this way (at least not one that I have found).

You can out put what you need however with SSJS

<script runat=server>
Platform.Load("Core","1");
var output = "var s1 = ["
for (var i = 0; i < 5; i++) 
{ 
    output += i + ',';
}
output += "]";

Write(output);
</script>

This would accomplish the same as what you are currently doing, without the need for AMPscript. Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.