Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Sep 21, 2023 06:54 AM - edited Sep 21, 2023 06:55 AM
Hi all,
I'm creating an automation to retrieve data from an endpoint then update a record in Airtable.
The api call works, the data is formatted into a string, and correctly passed into the `updateRecord` function.
However I'm getting the following error when testing the automation:
Error: Request parameters failed validation.
Here is the code:
let inputConfig = input.config();
let table = base.getTable('Orders');
let record = inputConfig.recordId;
fetch(`https://api.myEndpoint/${record}`, {
method: "GET",
withCredentials: true,
headers: {
"Authorization": inputConfig.apiKey,
"Content-Type": "application/json"
}
}).then((response) => {
return response.json().then((data) => {
updateRecord(data)
return data
})
})
const updateRecord = async (data) => {
let status = data.status
await table.updateRecordAsync(recordId, {
"Status": status,
})
}
To follow up, I modified the code to query the table and pull the latest created record and the update works.
I don't understand why we'd need to go through this extra step. Any insight or correction is appreciated.
This works but I don't know why it's necessary vs the first code example:
let inputConfig = input.config();
let table = base.getTable('Orders');
let query = await table.selectRecordsAsync({
fields: ["Order #", "Status"],
sorts: [{field: "Order #"}]
})
let recordId = query.records[query.records.length - 1].id // Unique identifier for a line item in order ex. rec9xNNtedRGoQZEY
let recordName = query.records[query.records.length - 1].name // The order number ex. 13247
fetch(`https://api.myEndpoint/${recordName}`, {
method: "GET",
withCredentials: true,
headers: {
"Authorization": inputConfig.apiKey,
"Content-Type": "application/json"
}
}).then((response) => {
return response.json().then((data) => {
updateRecord(data)
return data
})
})
const updateRecord = async (data) => {
let status = data.status
await table.updateRecordAsync(recordId, {
"Status": {"name": status}
})
}
Solved! Go to Solution.
Sep 21, 2023 01:27 PM
Hi,
You need to pass record name to fetch function (in https link). Record name is a value of primary field. String like 'rec123qwe456..' is record ID. You can just add recordName in input parameters, left side of automation script step UI, and then retrieve it from input.config().
but it's not only 'Name' vs 'ID' makes the difference.
I guess your field 'Status' is single-select.
You cannot just put string 'Option1' (for example) there. You should put object {name:'Option1'} to update 'Status' in record. Also, 'Option1' should already be present in field settings.
Sep 21, 2023 01:27 PM
Hi,
You need to pass record name to fetch function (in https link). Record name is a value of primary field. String like 'rec123qwe456..' is record ID. You can just add recordName in input parameters, left side of automation script step UI, and then retrieve it from input.config().
but it's not only 'Name' vs 'ID' makes the difference.
I guess your field 'Status' is single-select.
You cannot just put string 'Option1' (for example) there. You should put object {name:'Option1'} to update 'Status' in record. Also, 'Option1' should already be present in field settings.
Sep 22, 2023 07:03 AM
Thanks @Alexey_Gusev !
I updated my input.config to give me the value of the Airtable record ID and passed that as the first argument to the updateRecordAsync function. The test now passes with the correct data being updated.
Good catch on the single-select, too.