0

I have a requirement, I need to get all the data extension fields dynamically based on DE customer key and need to pass those fields into AMPscript to execute other functions. Below is my SSJS code:

<script runat="server">

Platform.Load("Core","1");
  var dataExtensionExternalKey = '5027D7B3-A0AD-4BA0-A0E0-5040B1D36D9B';
  var sourceDataExtension = DataExtension.Init(dataExtensionExternalKey);
  var dataExtensionFields = sourceDataExtension.Fields.Retrieve(); 
  var dataExtensionFieldsLength = dataExtensionFields.length; 
   for (var i = 0; i < dataExtensionFieldsLength; i++) {

               Write('<th>');

                Write(dataExtensionFields[i].Name+",");

                Write('</th>');

            }
</script> 

It is returning data extension column name with comma separated value. How can I pass the entire string with comma separated value to AMPscript? `

1

The code below will loop through all fields in a data extension and builds out a comma separated string. It then sets the @ampstring variable to that string.

<script runat=server>
Platform.Load("Core","1"); 
var string = ""
var dataExtensionFields = DataExtension.Init('5027D7B3-A0AD-4BA0-A0E0-5040B1D36D9B').Fields.Retrieve()
if (dataExtensionFields.length > 0) {
    for (var i = 0; i < dataExtensionFields.length; i++) {
        string += dataExtensionFields[i].Name + ','
    }
  Variable.SetValue("@ampstring",string)
}
</script>
%%=v(@ampstring)=%%
3

Use SSJS functions GetValue and SetValue to pass variable data back and forth.

Pass an AMPScript variable into SSJS using GetValue. Example:

%%[
    SET @AMPScriptVar = "Example"
]%%

<script runat="server" language="javascript">
   set var = Variable.GetValue("@AMPScriptVar ");
</script>

Pass an SSJS variable into AMPScript using SetValue. Example:

<script runat="server" language="javascript">
   set ssjsVar = 'Example';
   Variable.SetValue("@AMPScriptVar","ssjsVar");
</script>
  • Hi @Rachid Mamai.....Thanks for your input. This can be worked in normal way. But how can i use with For loop? Please help! – Naveen VM May 31 '19 at 5:38
2

If you want to pass the entire string (i.e., after the loop completes) to AMPscript, initialize a variable to be populated prior to the loop. For example:

%%[
var @csvString
]%%
<script runat="server">
Platform.Load('Core', '1');

var csvString = '';
for (var i = 1; i < 6; i++) {
  csvString += (csvString ? (',' + i) : i); 
}

// csvString is now fully populated
Variable.SetValue('@csvString', csvString);
</script>
%%[
/* you can now use the entire string */
Output(Concat(@csvString, '<br>')) /* 1,2,3,4,5 */

/* or work with individual items from the string */
set @fields = BuildRowsetFromString(@csvString, ',')
for @i = 1 to RowCount(@fields) do
  set @row = Row(@fields, @i)
  set @field = Field(@row, 1)
  Output(Concat(@field, '<br>'))
next @i

/* 1
 * 2
 * 3
 * 4
 * 5
 */
]%%

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.