Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Automation Code help

651 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Alg
4 - Data Explorer
4 - Data Explorer

I am trying to assign 4 different users records as the come in.  I found this code

 

const collaborators = [
    {name: "Person 1"},
    {name: "Person 2"},
    {name: "Person 3"},
    {name: "Person 4"}
]
const chosenOne = collaborators[Math.floor(Math.random() * collaborators.length)];
output.set("collaborators", chosenOne.name)

 

My problem is that it doesn't exhaust the 4 names before repeating.  What do I need to do to this code for it to go through all 4 options and then repeat?

1 Reply 1

It sounds like you want to go through all users before repeating, across different automation runs. However, the script itself does not know who was chosen in other automation runs. So you cannot solve this issue by adjusting only the script.

One option is to use a table to store the users. Have a checkbox or number field to identify who has been chosen already.

If you use a checkbox field, have the script set the checkbox for a person when the script chooses that person. When you pick a person, pick only from people who do no have their checkboxes selected. If everyone has their checkboxes selected, clear the checkboxes for everyone.

If you use a number field, when you pick a person, have the script increment that person's number. When you pick a person, only pick from people whose numbers are tied for the smallest.

Both of these cases require more complex scripting than shown in your current script.

You might also end up with a race condition with unexpected results if the automation ends up running for multiple records simultaneously.