Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards

Create a list programatically and insert reciepients info into it

Avatar

Level 6

I've used a JS activity to query the recipients, and also extract the IDs.

I need to to this to apply some logic to sort them randomly, and apply some other logic to them. (After that, I need to split those in halfs and save them into different lists), in order to them to them different emails, sms, or pushs.

So how to create a list using JS, let's calle it "my_list" and then insert the recipients into it?

var query = NLWS.xtkQueryDef.create(
  <queryDef schema={"nms:recipient"} operation="select" lineCount="999999999">
    <select>
      <node expr="@id"/>
    </select>
  </queryDef>
);  
  

var res = query.ExecuteQuery();


var recipients = res.getElementsByTagName("recipient")


var allRecipients = [];

for each (var r in recipients)
  //logInfo(r.getAttribute("id"))
  allRecipients.push(r.getAttribute("id"))

// Mezclar el array aleatoriamente para el A/B testing
allRecipients.sort(function() { return 0.5 - Math.random() });

.

 

6 Replies

Avatar

Community Advisor

Hi @god_prophet ,

Try using activities to do this. Because limiting the line count to 999999999 will leads to major server performance issue.

Avatar

Level 6

Hi Partha, in any case, how to build a list using JS and insert recipients into it?

for my use case, I could use a query activity to get all recipients where primary key is not empty... so I don't use lineCount=999999... but still need to know how to build a list programatically.

Ty.

Avatar

Level 10

Hi @god_prophet ,

 

As you have already got the list, and renaming it to "my_list", try below syntax to achieve the rest:

 

// 1. Create a copy
var my_list = allRecipients.slice(); // 2. Add a property to each recipient ID (replace with your actual logic) my_list = my_list.map(id => ({ id: id, group: Math.random() < 0.5 ? 'A' : 'B' })); // 3. Split the list into two halves const middleIndex = Math.floor(my_list.length / 2); const listA = my_list.slice(0, middleIndex); const listB = my_list.slice(middleIndex); // Example of sending different communications (replace with your actual sending logic): logInfo("List A (for emails): " + JSON.stringify(listA)); logInfo("List B (for SMS): " + JSON.stringify(listB));

Explanation:

  1. my_list = allRecipients.slice();: This creates a copy of the allRecipients array. It's crucial to work with a copy to avoid modifying the original data.
  2. my_list.map(...): This part is a placeholder for your custom logic. The example shows assigning recipients randomly to group A or B. Replace this with your actual logic to process each recipient ID.
  3. Splitting the List: The code efficiently splits my_list into two halves using slice().
  4. Saving to Separate Lists: listA and listB now contain the two halves. You would then use these arrays to send different communications (emails, SMS, push notifications) based on your requirements. The example shows how to log the contents of these arrays; replace this with your actual communication sending logic.

 

Thanks,

Jyoti

Avatar

Level 6

Hi, @Jyoti_Yadav I was wondering how to create the object list, I'd need to create 2 and then save each half into those.

So then with a regular workflow: I can send an email to each list, using an email activity. 

I was refering to a list in here. using JS:

god_prophet_0-1750267094533.png

 

Avatar

Level 10

Hi @god_prophet ,

 

If you need to save the list to ACC list folder, then the best approach is to design a workflow logic and try to save the list using 'List update' activity. You can split your recipients into half using 'Split' activity and then use 2 'list update' activity to save it in the specific folder with specific name.

 

Thanks,

Jyoti

Avatar

Level 6

Hi @Jyoti_Yadav ,

How to access the 

const listA = my_list.slice(0, middleIndex) 

from the JS activity in the List Update activity?

Thank you.