Help

Re: Script - multiple select update error

Solved
Jump to Solution
1463 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Hannah_Beekman
5 - Automation Enthusiast
5 - Automation Enthusiast

Could someone share the correct way to update a multiple select field using a script?

I have one table where I receive data from form submissions. When a submission is received, I use data from the submission to update a multiple select field in a record in another table. When I manually type my field update, as below, the script accepts the value:

    await table.updateRecordAsync(id,{
        'Multiple Select Field': [{ name:example1 }, { name: example2 }]
    })

However, when I use JSON.parse() to create an array of objects with name keys and values using my submission data I receive a 'cannot accept provided value' error:

//My raw data is brought into an automation script as an input. Here is an example of the format:
let rawData = ["example1", "example2"]

//Here, I convert my rawData into objects with keys and values
let data = rawData.map(datum => JSON.parse(JSON.parse('{ "name":"'+datum+'" }'));
    
//Here, I attempt to update my table using the parsed data
await table.updateRecordAsync(id,{
        'Communications Preferences': data
    })

Shouldn't the parsed data be equivalent to the array I've written out above? When I console log it, it gives me an array of objects just like that array. Can anyone explain why I am getting an error, or what I need to do differently? Thanks in advance!

1 Solution

Accepted Solutions
Rupert_Hoffsch1
10 - Mercury
10 - Mercury

Your first example would work (array of objects with key name). Keep in mind though that these need to exist as options first. So potentially your multi select doesn't have these options yet?

You can programmatically update options via updateOptionsAsync on the field. 

const multipleSelectField = table.getField('My multiple select field');
await multipleSelectField.updateOptionsAsync({
    choices: [
        ...multipleSelectField.options.choices,
        {name: 'My new choice'},
    ],
});

 

See Solution in Thread

4 Replies 4
Rupert_Hoffsch1
10 - Mercury
10 - Mercury

Your first example would work (array of objects with key name). Keep in mind though that these need to exist as options first. So potentially your multi select doesn't have these options yet?

You can programmatically update options via updateOptionsAsync on the field. 

const multipleSelectField = table.getField('My multiple select field');
await multipleSelectField.updateOptionsAsync({
    choices: [
        ...multipleSelectField.options.choices,
        {name: 'My new choice'},
    ],
});

 


@Hannah_Beekman wrote:

Shouldn't the parsed data be equivalent to the array I've written out above? When I console log it, it gives me an array of objects just like that array. Can anyone explain why I am getting an error, or what I need to do differently? Thanks in advance!


Can you share screen shots?
- The configuration of the select field(s) in question

- The result of console log for the values of in your first script.

- The result of console log for the variables in your second script.

This bit of your code looks weird.

let data = rawData.map(datum => JSON.parse(JSON.parse('{ "name":"'+datum+'" }'));

You have two nested JSON.parse() that I would not expect. The inner JSON.parse() inputs a text string and outputs an object. Thus your outter JSON.parse() is getting an object as input, when it expects a text string.

Try something like this instead.

let data = rawData.map(datum => ({ "name": datum }));

 


@kuovonne wrote:

This bit of your code looks weird.

 

let data = rawData.map(datum => JSON.parse(JSON.parse('{ "name":"'+datum+'" }'));

 


Thank you for catching this! I think this was just a case of sloppy copy/paste. I should have written this:

 

let data = rawData.map(datum => JSON.parse('{ "name":"'+datum+'" }'));

 

 

Thank you! I think this was the cause of the issue. I had edited one of my multiple select options, but had not reset my test record, so ended up trying update with the unedited option!