1

This is my first post and I'm a newbie when it comes to anything code-related let alone AMPscript so any and all help would be very much appreciated. Thank you in advance!

I'm using AMPscript in the footer of my emails and depending on a contact's subscription preferences, I'd like to dynamically show the groups they're subscribed to. The code seems to be working fine, however, I've noticed that if a person is subscribed to multiple consent groups, I am unable to add any formatting such as commas between the two groups. Rather, the line of text that appears says

%%EmailAddress%% is subscribed to X Y Z

instead of

%%EmailAddress%% is subscribed to X, Y, and Z

Does anyone know how I can add the commas in between multiple variables? My code is below.

%%[
Var @econsent, @fconsent, @tconsent, @email, @editorial, @first, @third

Set @email=EmailAddress
Set @econsent=AttributeValue("Consent_EditorialContentEmails")
Set @fconsent=AttributeValue("Consent_FirstPartyEmails")
Set @tconsent=AttributeValue("Consent_ThirdPartyEmails")
Set @editorial='X'
Set @first='Y'
Set @third='Z'

]%%

%%Emailaddr%%</a> is subscribed to receive emails from 
                       
%%[if @econsent == true THEN]%%
X
%%[ENDIF]%% 

%%[if @fconsent == true THEN]%%
Y
%%[ENDIF]%%
  
%%[if @tconsent == true THEN]%%
Z
%%[ENDIF]%%
2

Great question. There are a few different approaches, but I'd recommend building a rowset, then looping through the rowset to build the output. The following code will do exactly this. Here are some example outputs based on the consent values:

  • email@email.com is subscribed to receive emails from Y
  • email@email.com is subscribed to receive emails from Y and Z
  • email@email.com is subscribed to receive emails from X, Y and Z

Note: you asked to format as "X, Y, and Z" but I've formatted as "X, Y and Z" which I believe is more common. However, you can use a comma prefix before the 'and' if you insist in using a serial comma. Just change ' and ' in my code to ', and '.

%%[
Var @econsent, @fconsent, @tconsent, @consentList

/*
Set @econsent=AttributeValue("Consent_EditorialContentEmails")
Set @fconsent=AttributeValue("Consent_FirstPartyEmails")
Set @tconsent=AttributeValue("Consent_ThirdPartyEmails")
*/

set @econsent=true
set @fconsent=true
set @tconsent=true

/* build string for rowset */

if @econsent==true then
   set @consentList = 'X'
endif

if @fconsent==true and not Empty(@consentList) then
   set @consentList = Concat(@consentList, '|Y')
elseif @fconsent==true then 
   set @consentList = 'Y'
endif

if @tconsent==true and not Empty(@consentList) then
   set @consentList = Concat(@consentList, '|Z')
elseif @tconsent==true then
   set @consentList = 'Z'
endif

/* check if more than one value */

if IndexOf(@consentList, '|')>0 then

   var @listRowset, @i, @value, @row, @rowCount

   set @listRowset = BuildRowSetFromString(@consentList, '|')

   set @rowCount = RowCount(@listRowset)

   var @consentList /* reset variable dictionary value to null */

   /* build string */

   for @i = 1 to @rowCount do

      set @row = Row(@listRowset, @i)
      set @value = Field(@row, 1)

      if @i == 1 then /* first row */

        set @consentList = @value

      elseif @rowCount != @i then  /* next rows */

        set @consentList = Concat(@consentList, ', ', @value)

      else /* last row */

        set @consentList = Concat(@consentList, ' and ', @value)

      endif

    next

endif

]%%

%%emailaddr%% is subscribed to receive emails from %%=v(@consentList)=%%
2
  • Wow, thank you so much for your detailed explanation, Eliot! I really appreciate it! I noticed that I'm receiving the following error: The initial end count value for the for loop could not be cast to an integer. Any idea why? Oct 15 '20 at 20:12
  • No idea. The code I supplied above should work. You can schedule a call at eliot.com.au if you want to debug further. Oct 16 '20 at 0:36

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.