Weird behavior in script

Sanjay Rallapal
Tera Expert

Xanadu

I have script that is called in the Service Catalog Item Workflow post approval. It is trivial

var details = "Requested Item Details:\n";
// Get the current request item
var grReqItem = new GlideRecord('sc_req_item');
grReqItem.get(current.sys_id);

// Get the corresponding catalog item
var grCatItem = new GlideRecord('sc_cat_item');
grCatItem.get(grReqItem.cat_item);
// 2. Query approvals related to this RITM (sc_req_item) record
var approvalGR = new GlideRecord('sysapproval_approver');
approvalGR.addQuery('sysapproval', current.sys_id);
approvalGR.query();
 
Next it creates a JSON payload 
var payload = {
"requester": current.opened_by.getDisplayValue(),
"approver": approvalGR.approver.getDisplayValue(),
"approverComments": "",
"approvalDate": "2025-01-01",
"itemId": grCatItem.sys_id,
"External Catalog ID"grCatItem.u_external_catalog_id // custom attribute defined in the sc_cat_item table
};
 
Issue:
When I print the payload using gs.log(JSON.stringify(payload),"RITM Debug"); External Catalog ID is null. 
But, if I print just the value gs.log("External Catalog ID" + grCatItem.u_external_catalog_id,"RITM Debug");
the value is logged.

What am I doing wrong?
1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

Can you try below and print? That would convert the id to a string.

 

var payload = {
"requester": current.opened_by.getDisplayValue(),
"approver": approvalGR.approver.getDisplayValue(),
"approverComments""",
"approvalDate""2025-01-01",
"itemId": grCatItem.sys_id,
"External Catalog ID"grCatItem.u_external_catalog_id+"" // custom attribute defined in the sc_cat_item table
};

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

5 REPLIES 5

SanjivMeher
Kilo Patron
Kilo Patron

Can you try below and print? That would convert the id to a string.

 

var payload = {
"requester": current.opened_by.getDisplayValue(),
"approver": approvalGR.approver.getDisplayValue(),
"approverComments""",
"approvalDate""2025-01-01",
"itemId": grCatItem.sys_id,
"External Catalog ID"grCatItem.u_external_catalog_id+"" // custom attribute defined in the sc_cat_item table
};

Please mark this response as correct or helpful if it assisted you with your question.

Sandeep Rajput
Tera Patron
Tera Patron

@Sanjay Rallapal Try updating the payload as follows and see if it works.

 

var payload = {
"requester": current.opened_by.getDisplayValue(),
"approver": approvalGR.approver.getDisplayValue(),
"approverComments": "",
"approvalDate": "2025-01-01",
"itemId": grCatItem.sys_id,
"External Catalog ID": grCatItem.getValue('u_external_catalog_id')// custom attribute defined in the sc_cat_item table
};

Ankur Bawiskar
Tera Patron
Tera Patron

@Sanjay Rallapal 

try this

you need not query RITM as your current object holds reference to RITM table only

var details = "Requested Item Details:\n";
// Get the current request item
// Get the corresponding catalog item

// 2. Query approvals related to this RITM (sc_req_item) record
var approvalGR = new GlideRecord('sysapproval_approver');
approvalGR.addQuery('sysapproval', current.sys_id);
approvalGR.query();
if (approvalGR.next()) {
    var payload = {
        "requester": current.opened_by.getDisplayValue(),
        "approver": approvalGR.approver.getDisplayValue(),
        "approverComments": "",
        "approvalDate": "2025-01-01",
        "itemId": current.cat_item.sys_id,
        "External Catalog ID": current.cat_item.u_external_catalog_id // custom attribute defined in the sc_cat_item table
    };
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Sanjay Rallapal 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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