Add an input variable to new records created by script

Hi there,

I’m using this script by @Justin_Barrett to batch duplicate some records:

This is what it looks like::

let table = base.getTable("TaskLog");
let query = await table.selectRecordsAsync()
let checked = query.records.filter(record => {return record.getCellValue("Copy") === true})


// Make new records based on those that were checked
let newRecords = []
for (let record of checked) {
    let newRecord = {fields: {}}
    for (let field of table.fields) {
        // @ts-ignore
        if (field.isComputed || field.name === "Copy")
            continue
    
        let value = record.getCellValue(field.name)
        if (value === record.name)
            value += ""
        newRecord.fields[field.name] = value

    }

    newRecords.push(newRecord)
}

while (newRecords.length > 0) {
    await table.createRecordsAsync(newRecords.slice(0, 50))
    newRecords = newRecords.slice(50)
}

My question is:

How should I edit this script so that after the new duplicate records are created, the script will grab an input variable from a previous step in my automation and add it to all the new duplicate records that have just been created.

Hi @Dean_Arnold ,

At the top of your script add:

let inputConfig = input.config()

and then one line below this line:

newRecord.fields[field.name] = value

add the following:

newRecord.fields["Name of Field You Want To Add Your Input Value To"] = inputConfig["Name of Input"]

(Remember to add this input on the left hand side of the script on the automation script editor)

To clarify how @Jono_Prest is answering your question, @Dean_Arnold, it’s far more more efficient to add the new data while the records are being created instead of after they’ve been created. The new record data is already being collected prior to creation, and the lines that Jono lists allow you to insert that extra data item at the same time. When each new record is made, it already has all of the data that you want it to have.

2 Likes

Hi @Jono_Prest, thanks for your instructions.

Do the last lines of the script need to be edited in any way? I’m getting an error on line 27 (“Field “fldBM96IGFCRN66Gp” cannot accept the provided value.”), as pictured here:

The account field is a linked field to which I’m trying to enter the input variable as either a record ID or name of a linkable field. Both of which result in that error.

As a workaround, I’m sending the input variable value to a text field and using a separate automation to move that value to the linked field. It would be better if I could make the above script work as intended.

Hi @Dean_Arnold,

Ah I didn’t realise you were trying to link a record in a linked field. The structure you need to use is a little strange. Try edit line 20 to look like this:

newRecord.fields[“Account”] = [{id: inputConfig[“AccRecordID”]}]

Essentially to link a record you need to pass in an array containing objects with the property id matching a record ID in the linked table. In this case it’s only one record so you have an array with only one object.

Brilliant! Thanks for the explanation.

1 Like

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.