Aug 11, 2020 11:45 AM
We use our AirTable for content management for our blog. I’m looking to randomly assign a collaborator when an article updates in a “For Review” view. Any thoughts on how I might be able to do this?
Aug 11, 2020 01:39 PM
You could do this with an Automation. Set up a trigger for when a record enters your For Review
view. Then add a Run A Script action with a script like this:
const collaborators = base.activeCollaborators
const chosenOne = collaborators[Math.floor(Math.random() * collaborators.length)];
output.set("chosenOne", chosenOne.name)
(That script is basically saying “give me a a random number from 0 to the total number of collaborators in the base” and then “give me the name of the collaborator who matches that number”)
Then add and Update Record action. For Record ID use the ID of the record which triggered the automation. Click “Choose field”, select the collaborator field, and use the output from the script step (my example calls the “output variable” chosenOne
).
Aug 11, 2020 01:44 PM
This is awesome @Kamille_Parks. If I’m understanding you correctly, when I run this script, I should be able to keep it focused exclusively on a certain field too?
For example, I have 10 authors and 3 editors, but both use the collaborator fields to assign the work. I want to make sure it’s only the editors are getting pinged on the random assignment. Does that sound right?
Also, do I need to modify that script or is it plug and play?
Aug 11, 2020 01:54 PM
There are a couple ways to do that. You can either manually assign the list of editors like this:
const collaborators = [
{name: Editor 1},
{name: Editor 2},
{name: Editor 3}
]
Or, if you have a table where all the collaborators are listed you could set up a filter. That can be done either by selecting a random record from a View of editors, or filtering those records where the value of a SingleSelect field == “editor”. This option is more flexible.
Aug 11, 2020 02:02 PM
So, rather than collaborator we go with a single select field @Kamille_Parks. Sorry if I’m a little dense on this one, I haven’t yet gotten into scripting and am a bit of a weenie.
Ideally, I stick with the collaborator so I can still have the editors pinged on Slack and Email when it’s ready for their eyes.
Aug 11, 2020 02:05 PM
No, the second option I was describing is for if you have a table in your base where each collaborator is a record. Each record would have a collaborator field as well as a single select field describing if they are an editor, author, etc. If that isn’t your setup, go with the first option I described.
Aug 11, 2020 02:09 PM
Cool cool, and which parts of the script would I need to edit to customize it to my table?
(by the way, I really appreciate you taking the time here. I’m really thick on the script side, but am keen to learn)
Aug 11, 2020 02:14 PM
No part of the script needs to be customized other than you typing in the actual names of the editors.
So {name: Editor 1}
would become {name: "Thomas Walker"}
, assuming you are in fact an editor. Match names exactly as they appear in the collaborator field (case sensitive).
For the avoidance of doubt, your full script would look like this:
const collaborators = [
{name: "Thomas Walker"},
{name: "Jane Doe"},
{name: "John Doe"}
]
const chosenOne = collaborators[Math.floor(Math.random() * collaborators.length)];
output.set("chosenOne", chosenOne.name)
Aug 12, 2020 10:25 AM
Hey @Kamille_Parks, sorry to be a pain, but I’m getting the error “output.set is not a main function” when I enter the code. Any thoughts?
Aug 12, 2020 10:36 AM
That means you’re tying to do this in a Scripting Block. Back in my first reply I explained we are doing this in an Automation which includes a “Run a script” action. This is not the same thing as the Script Block. I know the terminology is confusing.