Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

I am stuck on my For Loop, If i pick 10 choices under the aws_pre_prod_items variable i want it to print each selection to my environment variable, so far right now it prints the last 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);
}

Why don't you append the details in a string variable in the for loop, then finally set the value, once the for loop has finished running?

 

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 = env + obj.environment + "\n";
}

g_form.setValue("u_environment", obj.environment);

The script might need tweaking, it is just an idea. Hope this helps!

Almost there, where would I add the -split (",") or should i be using join (",")

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

Thank you for helping me out, Just what I was looking for,