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

How to fetch both variable and Multirow variables on RITM using REST api

Nurulaslah Anua
Tera Contributor

Hi All,

Hope you are doing good.
I'm developing a Scripted REST API GET method to retrieve a single RITM from ServiceNow. The RITM form includes both variable and multi-row variables. Please see the attached RITM details for reference. I've managed to retrieve variable information but am unable to get the values for multi-row variables.
Can someone help me combine all the variables into a single Scripted REST API response?

Thank youRITM form.png

3 REPLIES 3

Vrushali  Kolte
Mega Sage

Hello @Nurulaslah Anua ,

 

You can query 'sc_req_item' table and filter it for your catalog item or RITM number. Please refer below script-

 

 

var values = '';
var ritm = 'RITM0010001';
var gr = new GlideRecord('sc_req_item');
gr.addQuery('number', ritm);
gr.query();
var arr = [];
while(gr.next()) {
	values += 'Summary is: ' + gr.variables.requested_for+ ',';
	values += 'Description is: ' + gr.variables.is_this_a_replacement_for_a_lost_or_broken_iphone+ ',';
	values += 'Due Date is: ' + gr.variables.monthly_data_allowance;
        values += 'MRVS Data : ' + gr.variables.mrvs_var_name; //this will give value in the JSON
}

gs.info(values);

 

 

If my answer solves your issue, please mark it as Accepted ✔️& Helpful👍!

Hi,

Thank you for your response.

I'm having trouble with this code. It's returning a status code of 200 OK, but the response body is empty "".

SN_Learn
Kilo Patron
Kilo Patron

Hi @Nurulaslah Anua ,

 

Please try the below:

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var ritm = new GlideRecord('sc_req_item');
//Replace with RITm's sysID
if (ritm.get('524c038083f70210e1c15d10feaad3bf')) {

    var objectArray = [];
    var mrvsSet = ritm.variables.mobile_devices_set; //Replace with mrvs name
    var mvrs = JSON.parse(mrvsSet);

    var varValues = '';

    for (var i = 0; i < mvrs.length; i++) {
        varValues += ritm.variables.division + '\n'; //variables name
        varValues += ritm.variables.requested_by.getDisplayValue() + '\n'; //variables name
        varValues += mvrs[i].storage + '\n'; //variables of mrvs(change the name as per your mrvs)
        varValues += mvrs[i].device_type + '\n'; //variables of mrvs(change the name as per your mrvs)
        objectArray.push(varValues);
    }
}
    return objectArray;

})(request, response);

 

Replace the variable's name as per your item.

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.