Help

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

Custom Script within Automation

Topic Labels: Scripting
58 1
cancel
Showing results for 
Search instead for 
Did you mean: 
ss22
1 - Visitor
1 - Visitor

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

 

 

 

 

1 Reply 1

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?