MRVS to String onSubmit Client script

DanielCordick
Mega Patron
Mega Patron

Hi Everyone,

I have a Multi Row Variable set with 1 column and about 75 different possibilities that can be selected. I am trying to parse my MRVS so i can print any number of selections into a string seperated by a ",". This is so i can use the formatted string on my workflow scratchpad. 

current code gives my a JS error OnSubmit 

function onSubmit() {
//Type appropriate comment here, and begin script below
var mrvs = JSON.parse(g_form.getValue('MRVS_internal_name')); //?should this be the internal name or Variable set?


// Join array results to form a string
var mrvsString = mrvs.map(function(mrvs){
return mrvs.u_mrvs_array;
}).join(",");
g_form.setValue('u_mrvs_array',(mrvsString));

}

 

1 ACCEPTED SOLUTION

Well I had used a string variable in the example, which was just concatenated with the environment values and a new line character "\n". You can replace that with a ",";

or if you want to use an array and join the array using a comma, you can use the following code:

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var mrvs = JSON.parse(g_form.getValue("aws_pre_prod_items"));
var parsed = mrvs;

var env = [];

for (var i = 0; i < parsed.length; i++) {
var obj = parsed[i];

env.push(obj.environment);
}

g_form.setValue("u_environment", env.join(", "));

}


Also, I just noticed I had typo in my previous response's last line. It should have been "env" in the second parameter.

Give the above code a try, hopefully it works.

Cheers,

Manish

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Daniel,

Did you try to alert or print the value of that variable through console.log?

Is that a json string?

getValue() takes name of the variable

Regards

Ankur Bawiskar

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur

 

I updated my script and it works, now im just having trouble getting rid of the variable name from the parsed object

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var mrvs = JSON.stringify(g_form.getValue("aws_pre_prod_items"));
console.log(mrvs);

g_form.setValue("u_environment", JSON.parse(mrvs));

}

 

Result : [{"environment":"Aws_1122"}]

 

if i remove g_form.setValue("u_environment", JSON.parse(mrvs)); and add

 

var output = JSON.parse(mrvs);
g_form.setValue("u_environment", output.environment);

 

the Result is blank

 

I would like to get rid of the first part to just have Aws_1122

 

Fixed my own issue, now just wanting it to display every value that is selected by the user

right now, it is just displaying the last value selected instead iterating through each selection, 

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var mrvs = JSON.parse(g_form.getValue("aws_pre_prod_items"));
var parsed = mrvs;

for (var i = 0; i < parsed.length; i++) {
var obj = parsed[i];
g_form.setValue("u_environment", obj.environment);
}

Hi Daniel,

So finally everything is working as expected or stuck at some part?

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader