0

I’m trying to pull in variable values from a SSJS content block.

I’ve looped over a base name to create incremental variable names.

It is outputting the literal variable names in to the email as opposed to their values (in attached image). I’m curious if I missed a step or need to wrap it something.

%%=ContentBlockbyKey("ssjs_testing")=%%

%%[FOR @i = 0 TO 2 DO ]%%

%%[ SET @test_data = Concat('@mls_number_0', @i) ]%%

<p>%%=v(@test_data)=%%</p>

%%[ NEXT @i ]%%


enter image description here

2 Answers 2

1

It’s doing that because you wrapped @mls_number in apostrophes which makes it a string. Do this instead:

%%=ContentBlockbyKey("ssjs_testing")=%%

%%[FOR @i = 0 TO 2 DO ]%%

%%[ SET @test_data = Concat(@mls_number_0, @i) ]%%

<p>%%=v(@test_data)=%%</p>

%%[ NEXT @i ]%%
1

To get the values, you will need to take a couple steps to get them to correctly output. And these steps are more 'hacks' than obvious.

So, when you utilize CONCAT, it will output a string no matter what. Which is why your line SET @test_data = Concat(@mls_number_0,@i) always returns the name of the variable as that is what comes from the CONCAT function. A string of the name.

Well good news is that is a good portion of the first step. Now before I go into the first step I am going to give a quick overview of what needs to be done so that when I explain the first step it does not seem like a complete nut is trying to talk to you about how to do this.

Steps to get value:

  1. Write out a string version of the name of the variable
  2. Around that string, further create that string to contain necessary AMPscript functions and code blocks that would correctly set the value (usually requires CONCAT)
  3. Utilize TreatAsContent() to render your AMPscript string and process it to correctly assign the value.

Here is an example step by step:

STEP 1:

SET @varName = CONCAT('@mls_number_0',@i)

STEP 2:

SET @ampStr = CONCAT('%','%=v(',@varName,')=%','%')

STEP 3:

SET @test_data = TreatAsContent(@ampStr)

Then you can output the @test_data like you had previously.

There are different ways you can do this, like you can set the AMPscript string to set @test_data inside of it and merge 2 and 3, or other things depending on needs, etc. But this way is usually the easiest to explain and to read, which is why I tend to push it out there in these cases.

Here is an example of it all combined into a single script:

                         
%%[
SET @var1 = "A"
SET @var2 = "B"
SET @var3 = "C"

FOR @i=1 TO 3 DO
 SET @test_data = TreatAsContent(Concat('%','%=v(@var', @i,')=%','%'))
]%%

<p>%%=v(@test_data)=%%</p>

%%[ NEXT @i ]%%
2
  • 1
    thank you for taking the time to explain this so thoroughly
    – Jason G
    Oct 31, 2022 at 13:35
  • worked perfectly, thank you!
    – Jason G
    Oct 31, 2022 at 16:16

You must log in to answer this question.

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