We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

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
Mega 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

Sanjay Rallapal
Tera Expert

Hello all,

Thank you for you suggestions. I tried all of them and wanted to report back

1. I couldn't append a double quote because that would mean altering the value of the attribute

2. I used the getvalue approach suggested by @Sandeep Rajput 
3. I also. tried the current.cat_item... approach suggested by @Ankur Bawiskar 

Approach (2) and (3) returned a value but that worked only when string concatenation was done.
In both approaches, I had to append toString() to get the payload to be constructed correctly. I don't know the technical rationale. I found this out by trial and error.
Follow up to above: I tried @SanjivMeher suggestion of appending "" (double quotes without a space) and that worked. It seems to have the same effect as toString()