Store an array in variable, pass it and display in delivery | Community
Skip to main content
Level 3
Jan 17, 2023 7:38 PM UTC
Solved

Store an array in variable, pass it and display in delivery

Hi,

I have a workflow that is built from Query activity, Advanced Javascript and delivery. For each recipient from the query I am doing calculations in Advanced JS activity and it returns an array. Its length may be different - from 1 element to 5.

 

So for example, in JS activity after I'm done with all business logic I save the array (myArray) to instance.vars:

var myArray = [1, 50, 100]; instance.vars.arrayForDelivery = myArray;

Let's assume that I want to have each element in a separate paragraph:

<% for (var i=0; i < (arrayForDelivery.length - 1); i++) { %> <p>instance.vars.arrayForDelivery[i]</p> <%}%>

What should be done next, in the delivery, to loop through all arrayForDelivery elements? The code above is just example because it seems to not be working.

 

Best,

Dominik

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
_Manoj_Kumar_
_Manoj_Kumar_Accepted solution
New Participant
Jan 18, 2023 11:16 PM UTC

Hello @dwnuk 

 

Follow these steps:

  1. Open your delivery properties and create a string variable there. Let's call it arrayForDelivery. Select the data type as a string.
  2. Now in your workflow, use the Delivery activity from the Action tab.
  3. In the script tab of the Delivery activity, use this code
    delivery.variables._var[0].stringValue=instance.vars.arrayForDelivery;​
  4. Now in the delivery, you will have access to this data using variables.arrayForDelivery

 

You were missing point no 3.

 

Let me know if it works.

 

     Manoj     Find me on LinkedIn

4 replies

_Manoj_Kumar_
_Manoj_Kumar_Accepted solution
New Participant
Jan 18, 2023 11:16 PM UTC

Hello @dwnuk 

 

Follow these steps:

  1. Open your delivery properties and create a string variable there. Let's call it arrayForDelivery. Select the data type as a string.
  2. Now in your workflow, use the Delivery activity from the Action tab.
  3. In the script tab of the Delivery activity, use this code
    delivery.variables._var[0].stringValue=instance.vars.arrayForDelivery;​
  4. Now in the delivery, you will have access to this data using variables.arrayForDelivery

 

You were missing point no 3.

 

Let me know if it works.

 

Translate
     Manoj     Find me on LinkedIn
dwnukAuthor
Level 3
Jan 23, 2023 11:00 AM UTC

Thank you both for help. Adding the code from point #3 solved the issue.

Translate
Shubham_Goyal__
Level 6
Jan 18, 2023 9:36 AM UTC

Hi @dwnuk ,

 

I see you have almost completed most of the tasks required here and now you can do as such to display these arrayBlocks in the delivery.

 

Step 1: Create an Html variable with labels such as:

var deliveryVar = "";
deliveryVar += "<table id=t01 border=0>";                                                               // Table header
deliveryVar += "  <tr><th colspan=7><h2><u>Array List</u></h2></th></tr>";
deliveryVar += "  <br>";
deliveryVar += "  <tr>";
deliveryVar += "    <th align='left'>My arrays</th>";
deliveryVar += "  </tr>";

 

Step 2: Keep storing the values of Arrays in this variable with a loop, such as:

for (var i=0; i < (arrayForDelivery.length - 1); i++) {        
            deliveryVar += "  <tr>";  
            deliveryVar += "    <td>" +   instance.vars.arrayForDelivery[i]  + </td>";
            deliveryVar += "  </tr>";
          };

Once all the loop runs the HTML variable will have all the array values stored in Html table rows. 

 

Step 3: Covert the HtmlVariable to instance variable:

vars.deliveryHtml = deliveryVar;

 

Step 4: Place this Html variable in your delivery wherever you like.

<P><%= vars.deliveryHtml %></P>

 

P.S. As I used the variables as per your query so there might be syntax errors above which can be easily solved when testing, but I hope you got the idea how it can done! 🙂

 

Br,

Shubham

Translate
dwnukAuthor
Level 3
Jan 18, 2023 3:17 PM UTC

Thanks @shubham_goyal__, indeed I can pass in from one activity to another. However, I have a strange issue when I try to use the variable inside the delivery. 

 "Error while compiling script 'content htmlContent' line 4: vars is not defined. SCR-160012 Javascript&colon; error while evaluating script 'content htmlContent'."

I have added additional javascript activity in between Advanced one and delivery. I can ready variable in it but it throws the error in delivery:

1. Advanced JS: save to vars (or instance.vars) and log to console -> works

2. Javascript activity: read vars (or instance.vars) -> works, I can see variable

3. Delivery: the error

 

Any clue what's wrong?

Translate