- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2019 01:26 PM
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));
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2019 09:36 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2019 08:47 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2019 09:50 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2019 03:23 PM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2019 08:39 PM
Hi Daniel,
So finally everything is working as expected or stuck at some part?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader