I have the following lines on a CloudPage

  SET @dynamicCode = CONCAT("Some code goes here")
  TreatAsContent(CONCAT("%%[OUTPUTLINE(CONCAT('something gets printed<br>'))]%%"))

On my page I should only see

something gets printed

But instead I get

something gets printed ")) /*TreatAsContent(@dynamicCode)

Can someone tell me how to correctly terminate the TreatAsContent? for some reason its reading the rest of the AMPScript and printing it as content.

EDIT: Added real code that I am trying to use, code doesn't appear to be working.

SET @date = Replace(FormatDate(Now(),"yyyy-MM-ddT","hh:mm:ss"),' ','')

IF @optIn == "1" THEN
  SET @optLines = ',
  "Opt_In__c", 1,
  "Opt_In_Date__c", @date'
  @optInLineCount = 2
  SET @optLines = ',
  "Opt_In__c", 0'
  @optInLineCount = 1

@startLineCount = 3
@totalLines = ADD(@startLineCount, @optLines)

SET @sfStringStart = 'SET @id = CreateSalesforceObject(
  "FirstName", @firstName,
  "LastName", @lastName,
  "Email", @email'
SET @sfStringEnd = ")"
SET @dynamicAmpscript = "CONCAT(@sfStringStart, @privacyLines, @gpMarketingLines, @salesMarketingLines, @sfStringEnd)"
OUTPUT(TreatAsContent(CONCAT("%","%[", @dynamicAmpscript , "]%","%")))

OutputLine(CONCAT("@Id is: ", @id))

1 Answer 1


I am honestly not sure why you are doing things that way. Seems like a ton of extra work for a simple output.

So the issue is likely coming from that you have the 2 % next to each other which is triggering the AMPscript renderer to look and try to see if its a var/function/personalization string. This combined with the layers of rendering required for the set up is likely causing your issue. By changing your CONCAT() to do CONCAT("%","%[...]%","%") it should remove this issue.

Now even with this, it will not output like you want. TreatAsContent returns a value that is essentially equivalent to a string. Meaning that everything inside is computed, but in order to have it affect things outside, you need to OUTPUT it again.

Long story short, to make it actually output you can use one of the following options:

  SET @dynamicCode = CONCAT("Some code goes here")
  OUTPUTLINE(CONCAT('<br>outputline: ',@dynamicCode)) /*Option1*/
  OUTPUT(CONCAT('<br>output: ',@dynamicCode)) /*Option2*/
  OUTPUT(TreatAsContent(CONCAT("%","%[OUTPUTLINE(CONCAT('<br>Some code goes here'))]%","%"))) /*Option3*/
<br>Dynamic Code: %%=v(@dynamicCode)=%% <!--Option4-->
  • 1
    The code was simplified for the question, I wanted to use a LOOKUP based on dynamic number of fields and this could only be achieved using dynamic variables hence the requirement. Thank you your answer resolved my issue, I actually used your blog post as the inspiration for the solution lol. Mar 5, 2020 at 12:47
  • Glad to have helped! Mar 5, 2020 at 13:44
  • Hi again, I wondered if you could look at the post again. I've included the real code I'm trying to use and it doesn't appear to be firing as expected, nor can I access the @Id that is generated off the back of the code executed. Mar 6, 2020 at 13:36

You must log in to answer this question.

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