AMPscript to SSJS and Back Again

AMPscript is great, but it can’t do everything. As you move into more advanced concepts, code, and tutorials, you are likely to come across Server-Side JavaScript, aka SSJS.

One of the things I find myself double-checking syntax on is how to pass a variable value from one language to the other. Because they are 2 completely different languages, they need us to help move data between them.

The good news is that SSJS can push the data back and forth, so we just need to learn the SSJS functions Variable.GetValue() and Variable.SetValue().


GetValue

GetValue is used when you have an AMPScript variable you want to pass into your SSJS.

To work, you need to set a value on an AMPscript variable before you open your script tags.

Ex.

%%[
    SET @Var = "Info"
]%%

<script runat="server" language="javascript">
   set var = Variable.GetValue("@Var");
</script>

SetValue

Set Value is used when you have a SSJS variable you want to pass into AMPscript.

This allows you to set an AMPscript variable from within your SSJS script. It will work with both a set value or a variable’s value.

Ex.

<script runat="server" language="javascript">
   set var = 'Info';
   Variable.SetValue("@Var","var");
</script>

%%=v(@Var)=%%

Additional References:

Salesforce SSJS Documentation – GETVALUE / SETVALUE

Ampscript Guide Documentation – GETVALUE / SETVALUE

Github Code Example: From katydorjeeAMPscript and Server-Side JavaScript variable together.html

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.