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