0

For parsing variables for the respective values on the email content html blocks, I usually have 2 kinds of variables.

The first case. The variable is defined on the sendable Data Extension. The second case. I create and set a new variable using Ampscript and lookup a value from a DE.

I use the notation %%=v(var)=%% for the first scenario or %%=v(@var)=%% for the second one.

My question is, what happens if the variable is neither defined on the sendable DE nor in the script. Still can I use it as %%=v(newVar)=%% where newVar is coming on the JSON from the request's payload?

2

While %%=v(varName)=%% works, the v() function isn't necessary. All that's required is %%varName%% outside of an AMPscript block or varName inside a block.

If the value you're referencing doesn't exist, the email won't compile or preview and your send will get aborted.

My rule of thumb -- no naked personalization strings. I never use %%varname%% notation outside of AMPscript blocks, nor do I use varname in a block without a function around it.

Always set a variable for send context attributes or DE columns. Set their values with AttributeValue() so you can detect empty values and ensure they exist:

%%[

var @deColumn1
set @deColumn1 = AttributeValue("deColumn1")

if empty(@deColumn) then

  set @deColumn1 = "default value"

endif

]%%
%%=v(@deColumn1)=%%

For parsing values from a payload, you can't just reference values by name -- you'll need to parse them out.

For example if you had this XML payload in a field value:

<?xml version="1.0" encoding="UTF-8"?>
<cart>
      <items>
            <item>
                  <sku><![CDATA[123]]></sku>
                  <name><![CDATA[Square]]></name>
                  <url><![CDATA[https://spmvs.com?sku=123]]></url>
            </item>
            <item>
                  <sku><![CDATA[456]]></sku>
                  <name><![CDATA[Circle]]></name>
                  <url><![CDATA[https://spmvs.com?sku=456]]></url>
            </item>
            <item>
                  <sku><![CDATA[789]]></sku>
                  <name><![CDATA[Triangle]]></name>
                  <url><![CDATA[https://spmvs.com?sku=789]]></url>
            </item>
      </items>
</cart>

...you'd parse can parse it like this:

%%[

var @xml, @isXML, @nodes, @rowCount
set @xml = AttributeValue("xml")

if indexOf(@xml,"<cart>") > 0 then

  set @nodes = BuildRowsetFromXML(@xml,"/cart/items/item",0)
  set @rowCount = rowcount(@nodes)

  if @rowCount > 0 then

    for @i = 1 to @rowCount do

      var @nodepath
      var @sku
      var @name
      var @url

      set @nodepath = concat("/cart/items/item[",@i,"]/")

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"sku"))) > 0 then
          set @sku = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"sku"),0),1),'Value')
      endif

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"name"))) > 0 then
          set @name = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"name"),0),1),'Value')
      endif

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"url"))) > 0 then
          set @url = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"url"),0),1),'Value')
      endif

      if not empty(@sku) and not empty(@name) and not empty(@url) then

      ]%%

       <br><a href="%%=redirectto(@url)=%%">%%=v(@sku)=%%</a> - <a href="%%=redirectto(@url)=%%">%%=v(@name)=%%</a>

      %%[

      endif

    next @i

  else

   outputline(concat("<br>no products found"))

  endif

else

  outputline(concat("<br>no XML found"))

endif

]%%

Or if you had this JSON payload in a field value:

[
   {
      "sku":"123",
      "name":"Square",
      "url":"https://spmvs.com?sku=123"
   },
   {
      "sku":"456",
      "name":"Circle",
      "url":"https://spmvs.com/?sku=456"
   },
   {
      "sku":"789",
      "name":"Triangle",
      "url":"https://spmvs.com/?sku=789"
   }
]

...you'd parse it like this

<script runat="server">

Platform.Load("Core","1.1.1");
var json = Attribute.GetValue('json');
var jsonObj = Platform.Function.ParseJSON(json);

if (jsonObj.length > 0) {

  for (var i = 0; i < jsonObj.length; i++ ) {

      var item = jsonObj[i];
      Platform.Variable.SetValue("@sku",item['sku']);
      Platform.Variable.SetValue("@name",item['name']);
      Platform.Variable.SetValue("@url",item['url']);
      </script>

      <br><a href="%%=redirectto(@url)=%%">%%=v(@sku)=%%</a> - <a href="%%=redirectto(@url)=%%">%%=v(@name)=%%</a>
      <script runat="server">

  }

} else {

  Write("no products found")

}

</script>
  • I have referred to this endpoint ://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/messageDefinitionSends.htm and specifically to the SubscriberAttributes variable. So in case I want to only send a variable but that doesn't exists in the AmpScript code, only in the html email template, is not possible to get the value and reference it, correct? Let's assume that the script control all the variable parsing, constructing a long string as the email. So, if nor in the DE is defined, then there's no way to use such variable from the SubscriberAttributes object, correct?Tks – mario ruiz May 6 at 18:17

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.