Oct 20, 2021 10:30 AM
I am new to the scripting app. I have a form that people fill out when they come to my office. It feeds into a table called Arrivals (QR Code). I want to create an automation that will run a script when the form is submitted:
I’m getting an error with this code:
let mainTable= base.getTable("Arrivals (QR Code)");
let mainTableRecords = await mainTable.selectRecordsAsync();
let lookupTable = base.getTable("Data About People");
let lookupRangeRecords = await lookupTable.selectRecordsAsync();
for (let record of mainTableRecords.records) {
let lookupValue = record.getCellValue("Guest Email");
for (let rangeRecord of lookupRangeRecords.records) {
if (rangeRecord.getCellValue("Email") === lookupValue) {
let returnValue = rangeRecord.id;
mainTable.updateRecordAsync(record, {
"Guest": returnValue
});
}
}
}
The error message:
Error: Field “fldwtE0s49vY9FBb9” cannot accept the provided value.
at main on line 14
Many thanks to anyone who can help get this thing running!
Oct 20, 2021 11:19 AM
Is the {Email }
field the primary field in the Data About People
table? If so, then you could do this without a script by using the Update Record step to copy the value from the {Guest Email}
into the {Guest}
field. If that isn’t the case, then ignore this method.
There are a few problems with your script:
{Guest}
appears to be the Link to Another Record-type field that connects Arrivals to Data. You are passing a singular ID, but all Link to Another Record-type fields are arrays of objects. returnValue
should be [{id: returnValue}]
. This is what is causing your error.To fix these issues, I recommend the following Automation setup. Instead of querying records within the script, my solution involves the Find Records step. You could do everything within the Script if you so chose.
Trigger the Automation “when a form is submitted”
Include a Find Records step that searches the Data About People table for emails which match the email used in the form (click the gear icon to the right of the field to enable dynamic variables):
Include a Run a Script step with these input variables and the following script:
// Get the values from the trigger and previous steps in this Automation
let {matchingRecordIds, triggerRecordID, lookupValue} = input.config()
// Get the tables from this base
let mainTable= base.getTable("Arrivals (QR Code)");
let lookupTable = base.getTable("Data About People");
/*
// Un-comment this section if you do not want to include a Find Records step
// Be sure to also remove "matchingRecordIds" from Line 2 if you use this portion of the code
let lookupRangeRecords = await lookupTable.selectRecordsAsync({fields: ["Email"]});
let matchingRecordIds = []
for (let rangeRecord of lookupRangeRecords.records) {
if (rangeRecord.getCellValue("Email") === lookupValue) {
matchingRecordIds.push(rangeRecord.id)
}
}
*/
// If any records in the lookup table have matching values to the trigger record,
// then update the trigger record to link to those matches
if(matchingRecordIds.length > 0) {
let mappedValue = matchingRecordIds.map(id => {return {id: id}})
mainTable.updateRecordAsync(triggerRecordID, {
"Guest": mappedValue
})
} else {
// If there are no matching lookup table records,
// then create a new record that links to the trigger record and give it the value
// from the lookup field
lookupTable.createRecordAsync({
"Arrivals": [{id: triggerRecordID}],
"Email": lookupValue
})
}
Oct 25, 2021 03:53 PM
Thank you @Kamille_Parks! This response helped me find a solution. Appreciate your time and I am now listening to BuiltOnAir.