Oct 12, 2022 06:30 AM
Hi all.
I have a fairly quick problem. I’m a relative novice to JavaScript and coding, so forgive me if this is a silly question.
I found some code that will duplicate a record and copy all fields, with the ability to exclude certain fields. I can fill in my table and exclusion fields, and it works as intended. The original thread is here:
I am looking to do exactly what was described in this post. The only part of the code that does not work for me is to include the “parent”, i.e. the original record to be duplicated, as a new, linked field in the duplicated record. Maybe I’m missing it, but I don’t see where in the new duplicated record that the old, originally selected record is referenced.
Please let me know if you need any more info! I’m hoping that in the coming months my familiarity with Javascript and Airtable will improve to the point where I can design these systems myself, but for now I just need to a fix. Thanks!
Best regards,
Nareg
Solved! Go to Solution.
Oct 12, 2022 06:58 AM
Welcome to the community, @Nareg_Kara-Yacoubian! :grinning_face_with_big_eyes:
You’re not missing anything. The way I read that, the update to add a link to the original record was never added. What was added was an update to copy over all linked record fields.
If you want to add a link to the original, change this line:
let obj = {}
to this:
let obj = {"Link to Original": [{id: original.id}]}
Replace "Link to Original"
with the name of the field that will contain the link to the original record.
Oct 12, 2022 06:58 AM
Welcome to the community, @Nareg_Kara-Yacoubian! :grinning_face_with_big_eyes:
You’re not missing anything. The way I read that, the update to add a link to the original record was never added. What was added was an update to copy over all linked record fields.
If you want to add a link to the original, change this line:
let obj = {}
to this:
let obj = {"Link to Original": [{id: original.id}]}
Replace "Link to Original"
with the name of the field that will contain the link to the original record.
Oct 13, 2022 02:46 AM
Hi Justin,
Thanks for the quick response!
What was added was an update to copy over all linked record fields.
Strange, as even without the second addition, i.e. the original code, it copies over linked records no problem in the duplicate. In any case that part works so I’m not going to touch it further.
Replace “Link to Original” with the name of the field that will contain the link to the original record.
Hm, I tried this but it didn’t work. Here is the exact code I’m using:
let table = base.getTable("Sample")
let query = await table.selectRecordsAsync({fields: table.fields})
let records = query.records
let fields = table.fields
let exclude = ["Trans 10"]
let filteredFields = fields.filter(x => !exclude.includes(x.name) && x.isComputed == false)
let original = await input.recordAsync("Original Record", table)
let obj = {"Parent": [{id: original.id}]}
filteredFields.map(x => {
let fieldValue = original.getCellValue(x.name)
let method = fieldValue && x.type=="multipleRecordLinks" ? fieldValue.map(y => ({id: y.id})) : fieldValue
Object.assign(obj, {[x.name]: method})
})
await table.createRecordAsync(obj)
In this structure, “Sample” is the table name, “Trans 10” is the excluded field, and “Parent” is the name of the field that needs to contain the link to the original record. Parent is a linked field type, and will sometimes contain a value (which needs to be replaced) or will sometimes be blank (in which the most recent parent needs to be filled). We will use this data down the line to construct trees, in which all children can be linked down to child samples.
Fairly soon we’re going to get some dedicated Javascript assistance, but I would really like to get this one thing working to dramatically simplify our workflow in the interim.
Thanks!
Nareg
Oct 13, 2022 04:16 PM
Could you please describe what exactly didn’t work? Did you receive an error message? If so, what’s the error? If there was no error, please describe in detail what did occur, and how that differs from your expectation.
Oct 17, 2022 01:41 AM
Hi Justin,
I was able to figure it out. When the code was run as described in my previous post, the “Parent” field, where the original record was supposed to be linked was not being filled in correctly. It would always return the result that was stored from the original inherited sample, i.e. like a “grandparent”.
However, I was able to solve this by including “Parent” as an excluded field. Now, it would not copy the original record’s value, but instead replace it with the link to the selected record as intended.
Thanks for your help!
Best regards,
Nareg