Populate the value of a field of Catalog item into the Multi Row Variable Set
Hello,
As you might be already aware, we cannot capture the data of catalog item into the Multi row Variable set, but many times, we might have a requirement where we need to use a value of catalog item in MRVS, based on the catalog item.
In this article, i will show you how we can achieve this.
This is not the best solution, but a workaround till there is a good permanent solution found.
Here i am using sessions and putting the data of the catalog item in session and then fetching that session value in the MRVS. Using this logic, we can use the data of catalog item in MRVS and vice-versa.
In the below example, i will show you how you can pass the value of a Employee Name Field in catalog item to MRVS and prefill it when clicked on the add form.
Catalog Client Script: OnChange on Employee Name Field
Whenever a name field is changed, i want this to prefill in the MRVS Add form. So i wrote a onChange client script and storing the name field in the session.
To uniquely identify the value, i am using the sys_id of the current record.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var sys_id = g_form.getUniqueValue();
var gaSession = new GlideAjax('sessionData');
gaSession.addParam('sysparm_name','storeInSession');
gaSession.addParam('sysparm_sys_id', sys_id);
gaSession.addParam('sysparm_value', newValue);
gaSession.getXML(callback);
function callback(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
}
}
Script Include is required as GlideSession is available at the server side.
var sessionData = Class.create();
sessionData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
storeInSession: function() {
var name = this.getParameter('sysparm_sys_id') + gs.getUserID();
gs.getSession().clearClientData(name);//clear if earlier value exists.
gs.getSession().putClientData(name, this.getParameter('sysparm_value'));
return 1;
},
FetchFromSession: function() {
var name = this.getParameter('sysparm_sys_id') + gs.getUserID();
var value = gs.getSession().getClientData(name);
gs.getSession().clearClientData(name); //clearing once the value is fetched.
return value;
},
type: 'sessionData'
});
Client Script onLoad of Multi Row Variable set (MRVS) to set the value of Employee Name
function onLoad() {
//Type appropriate comment here, and begin script below
var sUrl =top.location.href;
var pos = sUrl.indexOf("sysparm_id");
pos = pos + 13; //to fetch the exact sys_id from teh URL
var sys_id = sUrl.substr(pos,32);
var gaSession = new GlideAjax('sessionData');
gaSession.addParam('sysparm_name','FetchFromSession');
gaSession.addParam('sysparm_sys_id', sys_id);
gaSession.getXML(callback);
function callback(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue("emp_name", answer);
}
}
Mark the article as helpful and bookmark if you found it useful.
https://www.servicenow.com/community/developer-articles/populate-the-value-of-a-field-of-catalog-item-into-the-multi-row/ta-p/2322359