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

In SP onSubmit scripts with getXML do not always run completely as it is happening at the same time as the submit.  Does the same work in the native UI?  Are you able to change this to run onChange of a variable?

It does not work either in Native UI, the string text where I am passing the value is empty.

Ramel_0-1673527551981.png

 

Looks like, something is wrong in my script, but when I am doing an alert, I can see finalStr, but the reference field (current and new owner) remains empty.

 

Making it onchange, there is no variable name applicable as this is MRVS.

When you said your alert was returning the current_owner and the new_owner, I thought you meant you saw the expected values.  Looking at your scripts again, I don't see how this could be the case since you are sending the entire MRVS as the user_name to the SI, then using that value twice in the GRs as a user sys_id.  Beyond that, it makes more sense to handle all of the MRVS parsing and manipulation into a string within the SI, so try it like this instead:

function onSubmit() {
	if (g_form.getValue('request_type') == 'Splunk Knowledge Objects - Change of Ownership') {
		var ga = new GlideAjax('SetStringValueUtils');
		ga.addParam('sysparm_name', 'setStringValue');
		ga.addParam('sysparm_mrvs', g_form.getValue("splunk_knowledge_object"));
		ga.getXML(updateSplunkData);

		function updateSplunkData(response) {
			var answer = response.responseXML.documentElement.getAttribute("answer");
			alert("Answer: " + answer);
			g_form.setValue("splunk_text", answer);
		}
    }
}
var SetStringValueUtils = Class.create();
SetStringValueUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    setStringValue: function() {
        var mrvs = this.getParameter('sysparm_mrvs');
		gs.addInfoMessage('KOSI mrvs = ' + mrvs)
		var str = JSON.parse(mrvs);
		var finalStrArr = [];
		for (var i=0; i<str.length; i++) {
			var num = i + 1;
			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;
			}
			gs.addInfoMessage('KOSI owners = ' + current_owner + ' ' + new_owner)

			finalStrArr.push(num + ". Knowledge Object Type : " + str[i].knowledge_object_type + "\n" + " Knowledge Object Name : " + str[i].knowledge_object_name + "\n" + " Current Owner : " + current_owner + "\n" + " New Owner : " + new_owner + "\n" + " Remark : " + str[i].remark_ko + "\n\n");
		}
			gs.addInfoMessage('KOSI finalStrArr = ' + finalStrArr.join(','))
        
		return finalStrArr.join(',');
    },
    type: 'SetStringValueUtils'
});

The info messages are to help troubleshoot.  In some scenarios they don't show on the form, so if you're not seeing them, change them to gs.info then search the system log.

Thanks @Brad Bowman  , appreciate your help. I will test this out and will update you.