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

Hi Patrick,

So variables are getting populated into the data.variables.

It is just that the script is unable to print the variable values

Where is the code to iterate the array present in data.variables?

Regards

Ankur

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

Hi Anku,

my new widget is a clone of the "Request Fields" widget.

so this is the HTML that is pulling the "data.variables", this is the OOB script.

 find_real_file.png

 

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

Hi Ankur,

that's it!  I did change

var name = vs.get(i).getName();

to 

var name = vs.get(i).getLabel();

so that it shows the Question and not the Name of the variable.  But otherwise, perfect...thanks!