Help

Update Record Script is not working anymore?

Topic Labels: Scripting extentions
8323 23
cancel
Showing results for 
Search instead for 
Did you mean: 
Austin_Drozin
5 - Automation Enthusiast
5 - Automation Enthusiast

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?

23 Replies 23

Is there a particular reason you are creating a new record and then immediately updating the record, instead of passing the field values when the record is first created?

What is the obj variable and why doesn’t it already include the values for your ID, Discipline, ... fields?

Here is the full script! It basically takes one record from one table, and just copies some of the fields over. Then the other fields are filled in with ‘new’ info.

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", "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)
                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
                })
            }
        }
    }
}

Does the script fail to run, or does it run but not produce the expected results?

If the script fails to run, it should give you a line number and some other indication of the error.

I also suggest you liberally sprinkle console.log() throughout your script to see what the values are for each variable as the script is running.

It runs but the update sync part does not work. All those are blank. Two weeks ago they filled in properly.

You’re not await'ing the update call.

Again, I would recommend passing all the fields in one go. Its inefficient to pass some values, then pass more immediately afterward.

let newRecord = await main.createRecordAsync({
   ...obj,
   "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
})

Also, your code could be more efficient still by not excluding "Discipline", "Experience", "Duration", "Activity Type" from the fields to copy over since those values don’t appear to change.

I had to do those fields that don’t change since they were multiple selection. Found that out in another thread. It was throwing an error that they didn’t match.

Alright so I tried your code and it threw this error:

j: Can’t create records: invalid cell value for field ‘Discipline’.
Could not find a choice with that ID or name at main on line 67*

Does that mean the original field was a multiselect, the new field is a multiselect, or both?

If the field you’re copying into is a multiselect as opposed to a single select, then you should be feeding it an array not a single object.

What value are you trying to pass to “Discipline” and is there in fact a matching select option for the destination field?

i can’t seem to get the formatting right for an array. Can you show how to do that?

Yeah the fields have the exact same options, but maybe it’s like not the same ID for each option?

No option from Field A can have the same ID as an option from Field B. You’re passing the name so that doesn’t matter.

The format for the array is what you have already, wrapped in brackets:

[{name: record.getCellValueAsString("Discipline")}]