Help

Re: Add an input variable to new records created by script

Solved
Jump to Solution
2216 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Dean_Arnold
7 - App Architect
7 - App Architect

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.

1 Solution

Accepted Solutions
Jono_Prest
6 - Interface Innovator
6 - Interface Innovator

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)

See Solution in Thread

6 Replies 6
Jono_Prest
6 - Interface Innovator
6 - Interface Innovator

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.

Dean_Arnold
7 - App Architect
7 - App Architect

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:

Screen Shot 2021-10-10 at 9.51.08 pm

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.