Sep 08, 2021 05:58 AM
I have this script I made a month ago. It worked perfectly fine. Just tried it today and it doesn’t work. The fields that I’m updating with updateRecordAsync do not update. They are just blank.
let newRecord = await main.createRecordAsync(obj)
main.updateRecordAsync(newRecord, {
"ID" : newID,
"Discipline" : {name: record.getCellValueAsString("Discipline")},
"Experience" : {name: record.getCellValueAsString("Experience")},
"Duration" : {name: record.getCellValueAsString("Duration")},
"Activity Type" : {name: record.getCellValueAsString("Activity Type")},
"Content": newCombinedText
})
Like I said, this script did exactly as I wanted to in the past. Did something change with updateRecordAsync?
Sep 09, 2021 08:10 AM
Yay it worked making it an array! Not sure why it working fine two weeks ago haha.
Though what do I do when the field it’s copying from is blank? Some of them are blank, but I want that to be a ok. When the original field is blank, I get this error:
Can’t create records: invalid cell value for field ‘Specialization’.
Could not find a choice with that ID or name
Sep 09, 2021 07:00 PM
Hi,
feel like the problem is that you are trying to populate Select or Multiselect field with value doesn’t existing in “possible values”. Maybe empty value not allowed in your case.
in that case you should filter cellvalue (or update field options)
sorry, can’t remember proper single update format and doesn’t fully understand ‘create/then update’ logic
(after create)
let arr=[“Discipline”,“Experience”,“Durion”,“Activity Type”].map(x=>
[x,record.getCellValue(x)]).filter(a=>a[1]);
arr.push([“ID”,newID],[“Content”,newCombinedText]);
await main.updateRecordAsync(newRecord,Object.fromEntries(arr));
Sep 10, 2021 07:29 AM
I’m still very confused. The script doesn’t work every time… I have no idea what I’m doing wrong and it’s really halting progress :frowning:
I have two identical tables. The Specialization field has the EXACT same inputs as the other table. However, every once and a while one of the record creations just won’t work when I run the app
Can’t create records: invalid cell value for field ‘Specialization’.
Could not find a choice with that ID or name.
Like there is NOTHING different. The record right above it has the same selection in the field, but this particular one is invalid? Even though it’s exactly the same haha.
These are the two fields:
Like I said, exactly the same
Sep 10, 2021 07:33 AM
Current script:
let activity = base.getTable('Create Activity');
let main = base.getTable('Main');
let record = await input.recordAsync('Pick a record template', activity);
if(record)
{
// Get total num of objects in the three fields
let content = record.getCellValue('Content')
let numContent = content.length
let response = record.getCellValue('Response')
let numResponse = response.length
//let totalNewRecords = numContent * numResponse * numReflection
//console.log(totalNewRecords)
// New template record made
let fields = activity.fields
// Doesn't include these fields since in Main they are either different field types or tags which have different ids
let exclude = ["ID", "Discipline", "Specialization", "Experience", "Duration", "Activity Type", "Content", "Response"]
let filteredFields = fields.filter(x => !exclude.includes(x.name) && x.isComputed == false)
let obj = {}
filteredFields.map(x => {
// @ts-ignore
Object.assign(obj, {[x.name]: record.getCellValue(x.name)})
})
let IDCount = 0
//loop through the content, response, and reflection and make new records for each
for (let cn = 0; cn < numContent; cn++)
{
let contentText = content[cn].name
for (let rs = 0; rs < numResponse; rs++)
{
let responseText = response[rs].name
let newCombinedText = contentText + "\n\n" + responseText
IDCount++
let newID = record.getCellValue("ID") + "." + IDCount
//Queries
let mainRecordsCheck = await main.selectRecordsAsync({ fields: main.fields });
//Filter to find product
let IDMatch = mainRecordsCheck.records.filter(x=>x.getCellValue("ID") == newID)[0];
//If a match is found
if(IDMatch !== undefined)
{
throw new Error("ID in template already exists in Main. Make sure to remove records in Main that match the same ID OR change the template ID!");
} else
{
let newRecord = await main.createRecordAsync({
...obj,
"ID" : newID,
"Discipline" : {name: record.getCellValueAsString("Discipline")},
"Specialization" : [{name: record.getCellValueAsString("Specialization")}],
"Experience" : {name: record.getCellValueAsString("Experience")},
"Duration" : {name: record.getCellValueAsString("Duration")},
"Activity Type" : {name: record.getCellValueAsString("Activity Type")},
"Content": newCombinedText
})
}
}
}
}
Sep 10, 2021 07:34 AM
I had to do it this way because copying it over directly gave me a similar error to what I’m getting now. Where the ID’s don’t match.
Sep 10, 2021 08:29 AM
Just for future reference - while your tables might be identical, record IDs never are. Assuming this is the built-in record.id property being referenced.
Sep 10, 2021 08:38 AM
you can just put output.inspect(record.getCellValueAsString(“Experience”)) before ‘let newrecord…’ to avoid guessing.
but here is another problem. I just discovered that you are trying to use ‘createRecordAsync’ (and ‘updateRecordAsync’ before) in a loop, that’s wrong if you have many loop cycles
Sep 10, 2021 08:52 AM
Ok so did that and it outputted the correct strings. So there definitely is something there!
That’s interesting. I had it work with one where it made 75 records in one go. No problem with it.
Sep 10, 2021 08:53 AM
Is there a good way to copy over filed inputs then? If they never match, then there is no full proof way to copy over data from one table to another?
Which again, is so odd, since this script worked 2 weeks ago no problem. I used it like 40+ times. No edits made to it. I’m either really dumb or something fishy is goin lol
Sep 10, 2021 09:26 AM
I suppose it depends on some (undocumented?) limit calls/sec
in June, when i tried to create ‘bulk deduper’, for tables with hundreds of duplicates, and just started with JS and airtables, I put ‘createRecordAsync’ in a >500 loop and it stopped after 20-30 creations, without any error. So one day may be lucky, another - no.
JS is a “friendly” language, it lets you run even if your code is not 100% correct and tries to convert if you mess with data types and else, But such 'sometimes OK, sometimes not" behaviour is harder to debug. It’s good from one side, but bad from another.
btw, it’s also not OK to put async functions like “let mainRecordsCheck = await main.selectRecordsAsync” in a loop, when it should run once.
usual airtable script is: [get input data] => [process data] => {write data]
selectRecordsAsync usually performed in 1st step, and createRecordsAsync - in last step
[processing data] usually prepare some ‘array of new records’ or ‘array of updates’