Help

Airtable Automation Script - Dynamically assign field names and associated values to "updateRecordAsync()" function

Topic Labels: Automations
Solved
Jump to Solution
2244 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Kevin_Bradshaw
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello,

I apologise in advance for my simplistic coding effort. It has been 40 years since I dabbled in Visual Pascal and PHP :slightly_smiling_face:

I am writing an update script for use via Airtable web form submitted updates. The record created in the update table is polled by the script and only the completed fields in the update record, ie only fields that were filled in on the web form, are updated in the original record, contained in a separate (original) table.

A series of “if-then-else” statements determine which fields in the original record require updating and an array named fieldsArray, containing the field name and the associated field value for each field that requires updating, is constructed.

The recordID of the original record (that is to be updated) is passed to the script via inputConfig and I construct an updateRecordAsync() function using that recordID and the fieldsArray.

My problem is that I cannot work out how to construct the updateRecordAsync() function such that the structure is correct and I keep getting an error message, as shown below.


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


The script, in part, is as follows:


// set variables and constants
//
let inputConfig = input.config();
let searchOriginalEquipID = inputConfig.equipID;
let searchEquipUpdateRecID = inputConfig.equipUpdateRecID;
//
// Process original Equipment record
let tableCalEquip = base.getTable(‘Calibrated Equipment’);
let queryOriginalEquip = await tableCalEquip.selectRecordsAsync();
let filteredOriginalRecords = null;
filteredOriginalRecords = queryOriginalEquip.records.filter(calEquip => {
return calEquip.getCellValue(‘Equip ID’).includes( searchOriginalEquipID )
})
//
// Process the Cal Equip Update record
let tableCalEquipUpdates = base.getTable(‘Calibrated Equipment Updates’);
let queryUpdateEquip = await tableCalEquipUpdates.selectRecordsAsync();
let filteredUpdateRecord = null;
filteredUpdateRecord = queryUpdateEquip.getRecord( searchEquipUpdateRecID );
//


Constructing the fieldsArray follows:


//
// Transfer the updates to the original record
for (let calEquip of filteredOriginalRecords) {
let fieldsArray = ;
let calEquipRecordID = calEquip.id;
console.log( 'calEquipRecordID: ’ + calEquipRecordID );
//
// Process updates
let OutOfSvc = calEquip.getCellValue(‘Out of Svc’); if ( filteredUpdateRecord.getCellValue(‘Out of Svc’) !== null ) { OutOfSvc = filteredUpdateRecord.getCellValue(‘Out of Svc’) };
let UpdateComment = ‘’;
if ( filteredUpdateRecord.getCellValue(‘Update Comment’) !== null ) { UpdateComment = filteredUpdateRecord.getCellValue(‘Update Comment’) };
if ( calEquip.getCellValue(‘Notes’) !== null && UpdateComment !== ‘’ ) { UpdateComment = UpdateComment + “\n” + calEquip.getCellValue(‘Notes’) };
if ( UpdateComment !== ‘’ && UpdateComment !== null ) { fieldsArray.push( { [“Notes”]: UpdateComment, } ) };
console.log( 'Update Comment: ’ + UpdateComment );
let CompanySites = ‘’;
if ( filteredUpdateRecord.getCellValue(‘Company Sites’) !== null ) { CompanySites = filteredUpdateRecord.getCellValue(‘Company Sites’) }
// else { CompanySites = calEquip.getCellValue(‘Company Sites’) };
if ( CompanySites !== ‘’ && CompanySites !== null ) { fieldsArray.push( { [“Company Sites”]: CompanySites, } ) };
console.log( 'Company Sites Update: ’ + CompanySites );


and so on, ie more if-else statements…

With the update actions as follows:


//
console.log( fieldsArray );
//

await tableCalEquip.updateRecordAsync(
calEquipRecordID,
fieldsArray
);
//
// Mark as updated
//
await tableCalEquipUpdates.updateRecordAsync(
searchEquipUpdateRecID,
{ “Update Processed”: true }
)
}


Console log information showing the fieldsArray content and the error message is shown below:



image


NOTE This is line 78…

await tableCalEquip.updateRecordAsync(

I hope I have provided sufficient information for a more proficient coder to point me in the right direction.

Kindest regards, Kevin

1 Solution

Accepted Solutions
Greg_F
9 - Sun
9 - Sun

Hi Kevin,

You are passing fieldsArray as …Array [] , where the updateRecordAsync function expects and Object {} with field names as properties.

So instead:

fieldsArray.push( { [“Notes”]: UpdateComment, } 

go for

let fields = {};
fields["Notes"] = UpdateComment;

BTW - when pasting code you can use code field button in editor or tripple ` (```) for a code block.

I hope it helps!

See Solution in Thread

3 Replies 3
Greg_F
9 - Sun
9 - Sun

Hi Kevin,

You are passing fieldsArray as …Array [] , where the updateRecordAsync function expects and Object {} with field names as properties.

So instead:

fieldsArray.push( { [“Notes”]: UpdateComment, } 

go for

let fields = {};
fields["Notes"] = UpdateComment;

BTW - when pasting code you can use code field button in editor or tripple ` (```) for a code block.

I hope it helps!

Kevin_Bradshaw
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello Greg

Thank you very much for your prompt and helpful response. I’ll give that a try.

Kindest regards, Kevin

Kevin_Bradshaw
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello Greg

Success!!

image

Here are the modifed script parts…

// Process updates     
    let OutOfSvc = calEquip.getCellValue('Out of Svc'); if ( filteredUpdateRecord.getCellValue('Out of Svc') !== null ) { OutOfSvc = filteredUpdateRecord.getCellValue('Out of Svc') };  
    let UpdateComment = ''; 
        if ( filteredUpdateRecord.getCellValue('Update Comment') !== null ) { UpdateComment = filteredUpdateRecord.getCellValue('Update Comment') }; 
        if ( calEquip.getCellValue('Notes') !== null && UpdateComment !== '' ) { UpdateComment = UpdateComment + "\n" + calEquip.getCellValue('Notes') }; 
        if ( UpdateComment !== '' && UpdateComment !== null ) { updateFields["Notes"] = UpdateComment };  
console.log( 'Update Comment: ' + UpdateComment );
console.log( updateFields );

and here is the update function…


await tableCalEquip.updateRecordAsync(
    calEquipRecordID,
    updateFields
);
//

PS: Thanks for the code block hint. I use the triple ` for code blocks in Evernote, but didn’t think to do the same here!

Kindest regards, Kevin