Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Sep 27, 2024 01:17 PM
I'm trying to build an automation that uses a custom script to determine whether all necessary leads have been assigned to a record within the base. The teamsNeeded field (multiple select field type) determines the teams that need leads on the record. Each team then has a lead field (single select field type) where the lead is selected. If "Comms" and "KM" are selected in the teamsNeeded field then the "Comms Lead" and "KM Lead" field need to have a selection/not be empty.
I have limited scripting experience but have been able to build the one below. The issue is that it is outputting 'All Leads Assigned' even when leads are missing. The console log for teamsNeeded shows that all teams are being captured - the log for the example with "Comms" and "KM" in the teamsNeeded field reads "teamsNeeded (2) ["Comms", "KM"].
Is there something I am missing that is causing the output to always be 'All Leads Assigned' instead of 'Leads Missing' when leads are missing?
// Get values from the automation's input
let teamsNeeded = input.config().teamsNeeded || [];
console.log('teamsNeeded', teamsNeeded)
// Define mapping of teams to lead fields
const teamLeadFields = {
'CM': 'CM Lead',
'Comms': 'Comms Lead',
'LXD': 'LXD Lead',
'Training Delivery': 'TD Lead',
'KM': 'KM Lead',
'Quality': 'Quality Lead',
'Engagement': 'Engagement Lead',
'MLCE': 'MLCE Lead'
};
// Check if required lead fields are filled
let allLeadsAssigned = true;
for (const team of teamsNeeded) {
const leadField = teamLeadFields[team];
// Check if input.record is defined
if (input.record && !input.record.getCellValue(leadField)) {
console.log(`Lead field '${leadField}' is empty for team '${team}'`); // Add this line
allLeadsAssigned = false;
break; // No need to check further
} else if (input.record) {
console.log(`Lead field '${leadField}' is filled for team '${team}'`); // Add this line
}
}
// Output for the automation to use
let outputMessage = allLeadsAssigned ? 'All Leads Assigned' : 'Leads Missing';
output.set('allLeadsAssigned', outputMessage);
Solved! Go to Solution.
Sep 28, 2024 05:58 PM
Hi,
if you really want to
// Check if input.record is defined
put
const {teamsNeeded,ID}=input.config()
let record=await base.getTable('YOUR_TABLE_NAME').selectRecordAsync(ID)
then continue your script, put 'record' instead of 'input.record. in loop
if linter annoys you that 'record could be null', you can use:
if (record && record.getCellValue('Field') - exactly what you did
if (record?.getCellValue('Field')) - this is shorter form of higher
if(!record) throw new Error ('Record not defined') - put it somewhere before the if(record clause, maybe even outside the loop, but i'm not sure, testing needed. So-called 'guard clause' describes 'pessimistic' scenario at first, then work with others. Usually it helps to avoid complex nested IFs.
I would recommend to do it and test and then check about ES6 way
this part
let allLeadsAssigned = true;
for (const team of teamsNeeded) {
const leadField = teamLeadFields[team];
// Check if input.record is defined
if (input.record && !input.record.getCellValue(leadField)) {
console.log(`Lead field '${leadField}' is empty for team '${team}'`); // Add this line
allLeadsAssigned = false;
break; // No need to check further
} else if (input.record) {
console.log(`Lead field '${leadField}' is filled for team '${team}'`); // Add this line
}
}
if you OK with the fact that console.log('Lead field '${leadField}' is empty/filled ') can be omitted,
all this block can be substituted by
let allLeadsAssigned = teamsNeeded.every(team => record.getCellValue(teamLeadFields[team]))
I suggest to not use 'short' way until you check 'long' and sure to understand how 'short' works
And by the way, if your multiselect options doesn't include each other (like, for example. 'inactive' includes ' active'), you can do all this by a formula, without script. That's the first real world scenario I've seen here to use XOR logical function.
Sep 27, 2024 04:48 PM
You haven't shown what the inputs or the outputs to the script are, so it is hard to tell what is going on. For example, are you getting any output in the console?
My guess from reading the script is that your script does not output anything to the console, because input.record is undefined and thus nothing in your if statements ever executes.
Can you share more about your coding experience and what resources you used to write this script?
Sep 28, 2024 05:58 PM
Hi,
if you really want to
// Check if input.record is defined
put
const {teamsNeeded,ID}=input.config()
let record=await base.getTable('YOUR_TABLE_NAME').selectRecordAsync(ID)
then continue your script, put 'record' instead of 'input.record. in loop
if linter annoys you that 'record could be null', you can use:
if (record && record.getCellValue('Field') - exactly what you did
if (record?.getCellValue('Field')) - this is shorter form of higher
if(!record) throw new Error ('Record not defined') - put it somewhere before the if(record clause, maybe even outside the loop, but i'm not sure, testing needed. So-called 'guard clause' describes 'pessimistic' scenario at first, then work with others. Usually it helps to avoid complex nested IFs.
I would recommend to do it and test and then check about ES6 way
this part
let allLeadsAssigned = true;
for (const team of teamsNeeded) {
const leadField = teamLeadFields[team];
// Check if input.record is defined
if (input.record && !input.record.getCellValue(leadField)) {
console.log(`Lead field '${leadField}' is empty for team '${team}'`); // Add this line
allLeadsAssigned = false;
break; // No need to check further
} else if (input.record) {
console.log(`Lead field '${leadField}' is filled for team '${team}'`); // Add this line
}
}
if you OK with the fact that console.log('Lead field '${leadField}' is empty/filled ') can be omitted,
all this block can be substituted by
let allLeadsAssigned = teamsNeeded.every(team => record.getCellValue(teamLeadFields[team]))
I suggest to not use 'short' way until you check 'long' and sure to understand how 'short' works
And by the way, if your multiselect options doesn't include each other (like, for example. 'inactive' includes ' active'), you can do all this by a formula, without script. That's the first real world scenario I've seen here to use XOR logical function.
Sep 30, 2024 12:26 PM
Apologies - I did not specify in my original post that I did have an input for teamsNeeded so the output in the console was including the teams needed as expected. I was able to resolve this issue with @Alexey_Gusev solution below. Thank you!
Sep 30, 2024 12:27 PM
Thank you @Alexey_Gusev! Adding ID as an input and adjusting to the 'short' way for allLeadsAssigned worked.