Find your content:

Search form

You are here

Is there a way to refer to AMPscript variables dynamically?

 
Share

I have a piece of AMPscript containing 99 problems server variables which I would like to loop through and print, one by one. They're named [AX_VAR_1] through [AX_VAR_99]. I don't want to list out all 99 of them individually, but I cannot find a way to refer to them dynamically. Is this possible in AMPscript?

Thank you!

EDIT: Clarification and Update

I don't have any control over the data, the client does. We have a lot of (sequential) server variables that drive data for several different types of sends; some activate various content areas, others insert personalization data, still others determine the creative or the imagery to be used. Not all of the variables are used for every send.

What I want to do, though, is display them all as a table at the bottom of proof emails for QA purposes (i.e. to make sure that the logic is working correctly as it changes every send). I don't want to insert blank ones if I can help it.

Since this is for internal QA, it doesn't have to be fast (the maximum send will only be around 100 or so records). It also doesn't have to be particularly bulletproof, just accurate. And it doesn't have to be pretty, because we will be sending emails without this data to the client for proofing.

Thank you again!


Attribution to: ilinamorato

Possible Suggestion/Solution #1

I'm not sure why you would have 99 variables, but if you really needed to do this you would have to use SSJS and AMPScript together.

The following code requires that the variable names are the same except for an incrementing integer value, and also that you know the number of variables that exist ahead of time.

%%[
    set @AX_VAR_1 = "Variable 1"
    set @AX_VAR_2 = "Variable 2"
    set @AX_VAR_3 = "Variable 3"
    set @AX_VAR_4 = "Variable 3"
    set @numVars = 4
]%%


<script runat="server">
    Platform.Load("Core", "1")

    var numVars = Variable.GetValue("@numVars");
    for (x = 0; x <= numVars; x++)
    {
        Write(Variable.GetValue("@AX_VAR_" + x));
        Write("<br />");
    }
</script>

Attribution to: Jon Sakas

Possible Suggestion/Solution #2

OK, I have a better understanding of what you're trying to accomplish. Since you are looking for test results, this is how I would do it.

  1. Create a DE from [AX_VAR_1] through [AX_VAR_99]. These need to be nullable. Put some additional columns on that DE that will serve as the composite primary key, like emailaddress + jobid + emailid (see http://help.exacttarget.com/en/documentation/exacttarget/content/personalization_strings/ for those).
  2. Create a Content Area that pushes the fields of interest. Since [AX_VAR_1] through [AX_VAR_99] are nullable, you may or may not push them all. Obviously, you need the keys.
  3. Insert that content area on the emails you want to test. The push happens at send time.
  4. Create another DE consisting of 4 columns, the 3 column composite key and "results" (or whatever you like.).
  5. Write a query activity to SELECT the primary key(s) from the 1st DE, and Coalesce/concatenate the useful fields into the results column.
  6. Create a landing page that consists of that table you WERE going to put in that email. Pass jobid, emailid, and emailid in the query string
  7. Put a link to that landing page in the email, passing in the columns that compose the primary key as parameters.
  8. Put the send and query activity in an automation. The query won't run until the send is complete, but since this is just to test, that's fine.
  9. When you get the test email, click the landing page link, and see if you get the results you want.

Attribution to: Timothy

Possible Suggestion/Solution #3

You can do something like this

%%[


SET @names = "name1;name2;name3;name4"
SET @nameSet = BuildRowSetFromString(@names, ";") //creating a dummy rowset of variables I would like to dynamically create.


FOR @i = 1 TO ROWCOUNT(@nameSet) DO

SET @row = Row(@nameSet, @i)
SET @name = Field(@row, 1)

TreatAsContent( Concat( "%", "%[ SET @", @name, " = 'a value' ]%", "%") )

NEXT @i


]%%

What this code is doing is creating Ampscript Variables within AMPScript dynamically. It's creating a variable from my rowset, so - @name1, @name2, @name3 but with a value of "a value"


Attribution to: Amtera
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31819

My Block Status

My Block Content