Parsing Multi Row Variable Set

Ramel
Mega Guru

Hi Community,

 

I am working on a requirement where I am using Multi Row Variable Set. The data entered in the MRVS will then be parse and will be pushed to a String field that will be shown in the approval summarizer (we are not seeing the MRVS table in the approval summarizer) that is why we decided to go this route.

 

My issue is 2 of the fields in the MRVS are reference field [current owner, new owner], and when we pass the value to the String field, it is showing as sysID instead of the actual display name. Can you please help. Below is my script:

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var server_id = "";
if(g_form.getValue('request_type') == 'KO'){
var jsonString = g_form.getValue("ko");
var str = JSON.parse(jsonString);
var finalStr = [];
for (var i = 0; i < str.length; i++) {
var num = i + 1;
finalStr.push(num + ". Knowledge Type : " + str[i].knowledge_type + "\n" + " Knowledge Name : " + str[i].knowledge_name + "\n" + " Current Owner : " + str[i].current_owner + "\n" + " New Owner : " + str[i].new_owner + "\n" + " Remark : " + str[i].remark_ko + "\n\n");
}
var data = finalStr.join(' ');
g_form.setValue("string_text", data);
}
}

 

Thank you in advance.

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Inside the for loop you'll need to do a GlideRecord for each reference variable to return the name or whatever field:

var usr1 = new GlideRecord('sys_user');
if(usr1.get(str[i].current_owner)) {
    var current_owner = usr1.name;
}
var usr2 = new GlideRecord('sys_user');
if(usr2.get(str[i].new_owner)) {
    var new_owner = usr2.name;
}

then use the script variables current_owner and new_owner in your finalStr push instead of str[i].current_owner...

View solution in original post

8 REPLIES 8

Brad Bowman
Kilo Patron
Kilo Patron

Inside the for loop you'll need to do a GlideRecord for each reference variable to return the name or whatever field:

var usr1 = new GlideRecord('sys_user');
if(usr1.get(str[i].current_owner)) {
    var current_owner = usr1.name;
}
var usr2 = new GlideRecord('sys_user');
if(usr2.get(str[i].new_owner)) {
    var new_owner = usr2.name;
}

then use the script variables current_owner and new_owner in your finalStr push instead of str[i].current_owner...

Ramel
Mega Guru

Thanks a lot  @Brad Bowman

 

This does not seem to work in SP but works fine in Native UI. Maybe it is because that GlideRecord is not recommended to be used in client side.

 

Can somebody help me to covert into script include and call it by client script? Below is my client script that works in Native UI. 

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var server_id = "";
if(g_form.getValue('request_type') == 'KO'){
var jsonString = g_form.getValue("ko");
var str = JSON.parse(jsonString);
//var str = JSON.parse(JSON.stringify(g_form.getValue("ko")));
var finalStr = [];
for (var i = 0; i < str.length; i++) {
var curOwner = new GlideRecord("sys_user");
if (curOwner.get(str[i].current_owner)) {
var current_owner = curOwner.name;
}
var newOwner = new GlideRecord("sys_user");
if (newOwner.get(str[i].new_owner)) {
var new_owner = newOwner.name;
}
var num = i + 1;
finalStr.push(num + ". Knowledge Type : " + str[i].knowledge_type + "\n" + " Knowledge Name : " + str[i].knowledge_name + "\n" + " Current Owner : " + current_owner + "\n" + " New Owner : " + new_owner + "\n" + " Remark : " + str[i].remark_ko + "\n\n");
}
var data = finalStr.join(' ');
g_form.setValue("ko_text", data);
}
}

 

Regards,

Ramel

Right, I forgot that part.  Here's an excellent example showing the GlideAjax call, returning multiple values:

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

So your Script Include will contain the 2 GlideRecords, using the 2 variable values passed in from the client as parameters, then the rest of your client script is within the callback function and finalStr push uses the 2 values from the parsed answer.  Take a shot at it given the example, and I'll help you if you get stuck.

Hi @Brad Bowman  ,

Thanks for the guide. I have tried to do it but I am unsuccessful. I tried to alert in my client script, it is returning the 'current_owner and the new_owner', but I can't make it work to set the Value in my free text field. Can you have a look and check what I am missing.

 

Script Include:

var SetStringValueUtils = Class.create();
SetStringValueUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

setStringValue: function() {

var userName = this.getParameter('sysparm_user_name');
var curOwner = new GlideRecord("sys_user");
if (curOwner.get(userName)) {
var current_owner = curOwner.name;
}

var newOwner = new GlideRecord("sys_user");
if (newOwner.get(userName)) {
var new_owner = newOwner.name;
}

var finalStr = {};
finalStr.current_owner = curOwner.name.toString();
finalStr.new_owner = newOwner.name.toString();
return JSON.stringify(finalStr);

},

type: 'SetStringValueUtils'
});

 

Client Script:

function onSubmit() {
//Type appropriate comment here, and begin script below

var ga = new GlideAjax('SetStringValueUtils');
ga.addParam('sysparm_name', 'setStringValue');
ga.addParam('sysparm_user_name', g_form.getValue("splunk_knowledge_object"));
ga.getXML(updateSplunkData);

function updateSplunkData(response) {
if (g_form.getValue('request_type') == 'Splunk Knowledge Objects - Change of Ownership') {
var jsonString = g_form.getValue("splunk_knowledge_object");
var str = JSON.parse(jsonString);

var finalStr = [];
for (var i = 0; i < str.length; i++) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert("Answer: " + answer);
var result = JSON.parse(answer);
//var server_id = "";

var num = i + 1;

finalStr.push(num + ". Knowledge Object Type : " + str[i].knowledge_object_type + "\n" + " Knowledge Object Name : " + str[i].knowledge_object_name + "\n" + " Current Owner : " + result.current_owner + "\n" + " New Owner : " + result.new_owner + "\n" + " Remark : " + str[i].remark_ko + "\n\n");
}

var data = finalStr.join(' ');
alert("Final: " + data); //here I can print the data but Current owner and New owner are still blank
g_form.setValue("splunk_text", data); // then here it is not printing in the splunk text field
}
}
}