Aug 09, 2022 08:43 AM
Hi all, I’m new to scripting in Airtable and I got a bit confused about my error.
I’m using two variables for the script
When I run this script it gives an error “Error: Field XXX cannot accept the provided value.” for the project field although via the console.log I see that the correct projectID is being used. I don’t really understand why it doesn’t work for linking the project if it does work for the indicator (I tested succesfully the script without the project linking).
let inputConfig = input.config();
let table = base.getTable('Progress General Indicator 🚫');
let indicators = inputConfig.linkedIndicatorIDs;
let projectID = inputConfig.projectID;
for (let indicator of indicators) {
console.log(`Adding indicator ${indicator} to the new record.`);
console.log(`Adding project ${projectID} to the new record.`);
await table.createRecordsAsync([
{
fields: {
'Projects 🖌': [{id: projectID}],
'General indicators 🖌': [{id: indicator}],
},
}
])
}
Thanks in advance for your help!
Aug 09, 2022 03:58 PM
Can I assume projectID
is not the Airtable record ID of the referenced project? If so, pretty sure it won’t work because a linked field (i.e., I assume is Projects) requires a record ID, not an arbitrary field value that serves as a project ID.
Aug 10, 2022 12:23 AM
Hi Bill, thanks for your help. Actually I believe it is the record ID of the linked project that I’m trying to add there. Also, the console is saying " 1. “Adding project recrKig6PwXmDRblB to the new record.”"
Aug 10, 2022 05:26 AM
That looks right. And you’re certain the field Projects is a link field?
Aug 10, 2022 06:49 AM
Hi Bill, thanks for getting back to me. Yes, I’m certain.
Luckily, I found the issue, namely, projectID was a vector of strings here (with only 1 element). So when I printed it in the console I couldn’t understand the issue.
The issue is now solved by selecting the first element in the array (projectID2):
let inputConfig = input.config();
let table = base.getTable('Progress General Indicator 🚫');
let projectsTable = base.getTable('Projects 🖌');
let indicators = inputConfig.linkedIndicatorIDs;
let projectID = inputConfig.projectID;
let projectID2 = projectID[0];
let recordID = inputConfig.recordID;
let linkedIndicators = inputConfig.linkedIndicators;
/*Create new records (children)*/
for (let indicator of indicators) {
console.log(`Adding indicator ${indicator} to the new record.`);
console.log(`Adding project ${projectID2} to the new record.`);
let newRecordId = await table.createRecordsAsync([
{
fields: {
'Projects 🖌': [{id: projectID2}],
'General indicators 🖌': [{id: indicator}]
}
}
])
}
Thanks a lot for your help!
Aug 10, 2022 07:21 AM
@Pello_Mugica_Gonzale , are you the same person as @Airtable_admin ?
Or are there two people with similar issues?
Aug 10, 2022 07:45 AM
Woops, same person indeed. Didn’t realize I switched accounts!
Aug 10, 2022 08:07 AM
Glad you got it fixed.
Proof that sharing the actual code at the outset might have saved some anguish and time. :winking_face: