Help

Re: Script for Automation to Round Robin pick of Collaborators

1096 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Cole_Wedemeier
7 - App Architect
7 - App Architect

Hello!

I have a script to pick a collaborator from another thread. I’m telling the script which collaborators to choose from as not all would be assigned to a record. However, when I’m running the script, it’s consistently only assigning the first collaborator in the list.

Does anyone know how I can modify this script in my automation to basically round robin the collaborators? Where if it picked Collaborator1 this time, next time it picks Collaborator2? Or even, at a minimal request, get the script below to actually pick someone other than the first one all the time? :slightly_smiling_face:

I so appreciate this communities wealth of knowledge, so thank you in advance!


Trigger: When new record is created
Action: Run Script

const collaborators = [
    {name: "Victoria Ward"},
    {name: "Jordan Gill"}
]
const chosenOne = collaborators[Math.floor(Math.random() * collaborators.length)];
output.set("chosenOne", chosenOne.name)

Action: Update Record (the record that was just created) with collaborator chosen

2 Replies 2

Hi @Cole_Wedemeier - the problem here is the law of large numbers (or lack of, in this case). This part is good:

Math.floor(Math.random() * collaborators.length)

as a way of getting a random element from your array, but my guess is that it just hasn’t been run enough times for you to see the chosen collborators converge to 50%.

Modifying your script a little bit illustrate this:

const array = [
    {name: "Victoria Ward", score: 0},
    {name: "Jordan Gill", score: 0}
]
for(let i = 0; i < 5; i++) {
    let index = Math.floor(Math.random() * array.length)
    array[index]['score'] = array[index]['score'] + 1
}

console.log(array)

If you run this 5 times you might get the split weighted to one or the other - in my case 4 to 1. But if you run the randomisation 100 times or 1000 times you’ll get something close to 50% of each.

I think the answer to your problem might be in keeping score as I have done in the script above. I’m guessing the script runs are isolated from each other as they are based on an automation with “on record create”. So my suggestion is to log a score against each collaborator and each time it runs, pick the collaborator with the lowest score (and increment their score by 1). Or if they have the same score, pick the first. But this assumes the collboarators and their scores are held in a table within the base, which might not be the case for you.

@JonathanBowen Thank you!

I have no issue having a table to host their scores in. I do have a couple questions though.

  1. Where in that script you provided is it that I’m identifying the table name in which it’s looking for the score to choose the lowest name to input and then update by 1. In your example is that the “array” such as in my script it was looking at “collaborator” even though collaborators are not an actual table within the base?

  2. Also, it seems as though if I update to this script, there is no output that allows me to then amend the record that was created (which triggered the automation) with the collaborator the script chose.

I so appreciate your help!

Thank you.