The CreatorCon Call for Content is officially open! Get started here.

Script help for custom "Request Fields" widget to show RITM variables

patricklatella
Mega Sage

Hi all,

I'm working on a widget to show the variables and values from a RITM, but in a widget I can show on the "sc_request" page.  The use case is a portal for a specific set of catalog items and requests which will not be using the cart, so every REQ only has one RITM.  I've cloned the "Request Fields", and the behavior I'm looking for is how "Ticket Fields" widget shows on the RITM, where it shows the variable and values, but for it to do this on the REQ page, showing the variables/values from the RITM.

So I've cloned the "Request Fields" widget, and grabbing the RITM.

The line of code that sets the variables and values data object is:

data.variables = $sp.getVariablesArray();

Here's my server script for my cloned widget. 

(function() {
data.isValidRecord = true;
var gr = $sp.getRecord();
var grSysID = gr.sys_id;
if (gr == null || !gr.isValid())
return;

var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', grSysID);
ritm.query();

if(ritm.next()){
data.isValidRecord = true;
var agent = "";
var a = $sp.getField(ritm, 'assigned_to');
if (a != null)
agent = a.display_value;

var fields = $sp.getFields(ritm, 'number,state,priority,sys_created_on');
if (ritm.getValue("sys_mod_count") > 0)
fields.push($sp.getField(ritm, 'sys_updated_on'));

data.tableLabel = ritm.getLabel();
data.fields = fields;
//data.variables = $sp.getVariablesArray();//OOB script...this is where I'm trying to set the variables from the 'ritm'...anyone know the trick for this?
data.agent = agent;
data.agentPossible = ritm.isValidField("assigned_to");
data.table = ritm.getTableName();
data.sys_id = ritm.getUniqueValue();
if (!ritm.due_date.nil())
data.completion = ritm.due_date.getGlideObject().getLocalDate().getDisplayValue();
}
})();

 

1 ACCEPTED SOLUTION

Hi Patrick,

So the code requires a json string; so update code as below

I have created an array of json objects and stored in that array; now when you iterate over each object of json through array it should display

try and let me know

//build array of variables from RITM
var array = [];
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(ritm.sys_id);
set.load();
var vs = set.getFlatQuestions();

for (var i=0; i < vs.size(); i++) {

var obj = {};

var name = vs.get(i).getName();
var value = vs.get(i).getDisplayValue();
if(name != '' && value != '') {

obj.label = name.toString();
obj.display_value = value.toString();

array.push(obj);
}
}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

the below code gives the variable name; so you can use similar in your server side widget script and set that in array and set that array in data.variables; this is untested code; code to get the variable names runs ok; but you need to check whether data.variables is being correctly stored; and the place where you are using data.variables check that as well

var array = [];
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(ritm.sys_id);
set.load();
var vs = set.getFlatQuestions();

for (var i=0; i < vs.size(); i++) {
var name = vs.get(i).getName();  

if(name != '') {
array.push(name.toString());
} 
}

data.variables = array;

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

thanks for the suggestion.  I tried adding that to my script...it appeared to fill the widget with blank space...so something was there.  When I logged "array" to the console, it did list the entire list of all the variables from the catalog item...however it included all variables, including formatters...and it did not log the variable values.  I played with it a bit and could not get it to only show the variables that presented fields on the catalog item and their values.  Here's what the "Options" sectin showed in the widget.  And below is the script I tried last.  any thoughts?

 

find_real_file.png

 

(function() {
data.isValidRecord = true;
var gr = $sp.getRecord();
var grSysID = gr.sys_id;
if (gr == null || !gr.isValid())
return;

//added by PL - get the RITM
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', grSysID);
ritm.query();

if(ritm.next()){
//end added by PL

data.isValidRecord = true;
var agent = "";
var a = $sp.getField(ritm, 'assigned_to');
if (a != null)
agent = a.display_value;

var fields = $sp.getFields(ritm, 'number,state,priority,sys_created_on');
if (ritm.getValue("sys_mod_count") > 0)
fields.push($sp.getField(ritm, 'sys_updated_on'));

//build array of variables from RITM
var array = [];
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(ritm.sys_id);
set.load();
var vs = set.getFlatQuestions();

for (var i=0; i < vs.size(); i++) {
var name = vs.get(i).getName();

if(name != '') {
array.push(name.toString());
}
}
console.log('array is '+array);
data.variables = array;


data.tableLabel = ritm.getLabel();
data.fields = fields;
//data.variables = $sp.getVariablesArray();//OOB script, however the current $sp is the REQ
//data.variables = ritm.variables;//this needs to be the array of variables from the 'ritm'
data.agent = agent;
data.agentPossible = ritm.isValidField("assigned_to");
data.table = ritm.getTableName();
data.sys_id = ritm.getUniqueValue();
if (!ritm.due_date.nil())
data.completion = ritm.due_date.getGlideObject().getLocalDate().getDisplayValue();
}
})();

Hi Patrick,

I don't think you would know in the script which variables are presented; it would give you list of all variables;

to show only those variables which are having value and to block variables with empty value use this

can you try this:

//build array of variables from RITM
var array = [];
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(ritm.sys_id);
set.load();
var vs = set.getFlatQuestions();

for (var i=0; i < vs.size(); i++) {
var name = vs.get(i).getName();
var value = vs.get(i).getDisplayValue();
if(name != '' && value != '') {
array.push(name.toString());
}
}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

that script is setting "array" to just the variables with values, so that's good.  However it's not setting the variable values and it's not showing either in the widget.  It's taking up the space in the widget, but not showing anything.  Anything else I can try?

 

 

find_real_file.png