Create a list programatically and insert reciepients info into it | Community
Skip to main content
Level 6
June 16, 2025
Solved

Create a list programatically and insert reciepients info into it

  • June 16, 2025
  • 2 replies
  • 622 views

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() });

.

 

Best answer by Jyoti_Yadav

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

2 replies

ParthaSarathy
Community Advisor
Community Advisor
June 17, 2025

Hi @god_prophet ,

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

 ~  ParthaSarathy S~  Click here to join ADOBE CAMPAIGN USER GROUP for Quarterly In-person | Hybrid | Virtual Meetups
Level 6
June 17, 2025

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.

Jyoti_Yadav
Jyoti_YadavAccepted solution
Level 8
June 18, 2025

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

Level 6
June 18, 2025

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:

 

Jyoti_Yadav
Level 8
June 19, 2025

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