Help

Re: Automation - what's wrong with my code?

536 0
cancel
Showing results for 
Search instead for 
Did you mean: 
wooti
6 - Interface Innovator
6 - Interface Innovator

(Javascript novice) I’ve adapted some code to work with my different field names but it isn’t working and I’m not sure why.

This script is supposed to update the fields “Proposal Accepted”, “Checklist Sent”, and “Checklist Received” based on the amount of time that a single select field was set to either “Proposal Accepted”, “Checklist Sent”, or “Checklist Received”.

Strangely, the automation runs successfully sometimes. When it doesn’t run successfully, the execution log is as follows:

Error: Could not find a field with name or ID “undefined”.
at main on line 72

Can anyone see the problem? Here is the code:

let table = base.getTable('Jobs')
let proposalAcceptedTimeFieldName = "Proposal Accepted Time"
let checklistSentTimeFieldName = "Checklist Sent Time"
let checklistReceivedTimeFieldName = "Checklist Received Time"
let proposalAcceptedLastModifiedFieldName = "Proposal Accepted Last Modified"
let checklistSentLastModifiedFieldName = "Checklist Sent Last Modified"
let checklistReceivedLastModifiedFieldName = "Checklist Received Last Modified"


let inputConfig = input.config()
let recordId = inputConfig.recordId
let proposalAcceptedTime = inputConfig.proposalAcceptedTime
let checklistSentTime = inputConfig.checklistSentTime
let checklistReceivedTime = inputConfig.checklistReceivedTime
let proposalAcceptedLastModified = inputConfig.proposalAcceptedLastModified
let checklistSentLastModified = inputConfig.checklistSentLastModified
let checklistReceivedLastModified = inputConfig.checklistReceivedLastModified
let currentChange = inputConfig.currentChange

let lastChangedToFieldToUpdate = (() => {
    switch(currentChange){
        case "Proposal Accepted":
            return proposalAcceptedLastModifiedFieldName;
        case "Checklist Sent":
            return checklistSentLastModifiedFieldName;
        case "Checklist Received":
            return checklistReceivedLastModifiedFieldName;
        default:
            return null;
    }
})();

let holdingArray = [
    {
        type: 'proposal accepted',
        date: proposalAcceptedLastModified,
        timeSpent: proposalAcceptedTime,
        timeSpentField: proposalAcceptedTimeFieldName,
    },
    {
        type: 'checklist sent',
        date: checklistSentLastModified,
        timeSpent: checklistSentTime,
        timeSpentField: checklistSentTimeFieldName,
    },
    {
        type: 'checklist received',
        date: checklistReceivedLastModified,
        timeSpent: checklistReceivedTime,
        timeSpentField: checklistReceivedTimeFieldName,
    }
]

let array = new Array

for (let item of holdingArray){
    if (item.date != null){
        array.push(item)
    }
}

let currentDateTime = new Date()

if(array.length > 0){
    let latestChange = array.reduce((a, b) => {
    return new Date(a.date) > new Date(b.date) ? a : b;
    });

    let timeSpent = toMinutes(currentDateTime - (new Date(latestChange.date)))

    if (lastChangedToFieldToUpdate != null){
        await table.updateRecordAsync(recordId,
            {
                [latestChange.timeSpentField]: timeSpent + latestChange.timeSpent,
                [lastChangedToFieldToUpdate]: currentDateTime
            }
        )
    }
    else{
        await table.updateRecordAsync(recordId,
            {
                [latestChange.timeSpentField]: timeSpent + latestChange.timeSpent,
            }    
        )
    }   
}
else{
    if (lastChangedToFieldToUpdate != null){
        await table.updateRecordAsync(recordId,
            {
                [lastChangedToFieldToUpdate]: currentDateTime
            }
        )
    }    
}

function toMinutes(value){
    return value / 1000 / 60
}
1 Reply 1

Hi @wooti - looks like it is this that is going wrong:

    if (lastChangedToFieldToUpdate != null){
        await table.updateRecordAsync(recordId,
            {
                [latestChange.timeSpentField]: timeSpent + latestChange.timeSpent,
                [lastChangedToFieldToUpdate]: currentDateTime
            }
        )
    }

The error message is referring to one of the two fields you are trying to update. Either latestChange.timeSpentField or lastChangedToFieldToUpdate is undefined. The update action is trying to find a field with name or ID of “undefined” which, of course, it can’t do.

From here I would do the following:

  • Just before the await line, console log out all of the variables uses in the update. Find out which one specifically is undefined
  • Then trace this undefined variable through the earlier code to see where it becomes undefined, e.g. is lastChangedToFieldToUpdate actually set? Does latestChange have a timeSpentField attribute?