How to get query sys_id of current.sys_id Service Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2021 04:34 AM
Hi SNOW Community,
I have a question regarding a small issue that I'm having. I've created a widget that will live on the Service Portal to allow an admin to Accept or Reject requests.
The data for the widget is pulling from the Approvals (approval_approver) table. Under my GlideRecord, I have a query that checks for the state as requested. (Ex. addQuery('state', 'requested'))
To narrow down the search, I tried entering addQuery('sys_id', current.sys_id). When I use this query, my script breaks and I get an error on the Service Portal end.
Here's a sample of the GlideRecord script I've written to Accept.
//Accept Request
if(input && input.action=="acceptApproval") {
var inRec1 = new GlideRecord('sysapproval_approver');
inRec1.addQuery('state', 'requested');
//inRec1.get('sys_id', current.sys_id);
inRec1.query();
if(inRec1.next()) {
inRec1.setValue('state', 'Approved');
inRec1.setValue('approver', gs.getUserID());
gs.addInfoMessage("Accept Approval Processed");
inRec1.update();
}
}
I've research the web, tried using $sp.getParameter() as a work-around and no change.
I would really appreciate any help or insight on what I can do different to get script to work and filter the right records.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2021 04:58 AM
The widget is a custom table, no clone. Thank you for your help.
Here's my Server Script code:
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
data.Ctrl = [];
var inRec = new GlideRecord('sysapproval_approver');
inRec.addQuery('state', 'Requested');
inRec.query();
while(inRec.next()) {
var temp = {};
temp.number = inRec.getDisplayValue("number");
temp.approval = inRec.getDisplayValue("sysapproval");
temp.state = inRec.getDisplayValue("state");
//temp.sys_id = inRec.getValue("sys_id");
//temp.sys_id = inRec.getUniqueValue();
temp.opened = inRec.getDisplayValue("sys_created_on");
temp.comments = inRec.getDisplayValue("comments");
data.Ctrl.push(temp);
}
//Accept Request
//data.sys_id = (input && input.sys_id) || options.sys_id || $sp.getParameter("sys_id");
if(input && input.action=="acceptApproval") {
var inRec1 = new GlideRecord('sysapproval_approver');
inRec1.addQuery('state', 'requested');
//inRec1.get('sys_id', data.sys_id);
inRec1.query();
if(inRec1.next()) {
inRec1.setValue('state', 'Approved');
inRec1.setValue('approver', gs.getUserID());
gs.addInfoMessage("Accept Approval Processed");
inRec1.update();
}
}
//Reject Request
if(input && input.action=="rejectApproval") {
var inRec2 = new GlideRecord('sysapproval_approver');
inRec2.addQuery('state', 'requested');
inRec2.query();
if(inRec2.next()) {
inRec2.setValue('state', 'Rejected');
inRec2.setValue('approver', gs.getUserID());
gs.addInfoMessage("Reject Approval Processed");
inRec2.update();
}
}
})();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2021 04:51 AM
include log and check what exactly below script is giving you, see if you are getting sys_id or not.
Added log please check and let me know
data.sys_id = (input && input.sys_id) || options.sys_id || $sp.getParameter("sys_id");
gs.log('sys ID Value '+ data.sys_id);
if(input && input.action=="acceptApproval") {
var inRec1 = new GlideRecord('sysapproval_approver');
inRec1.addQuery('state', 'requested');
inRec1.addQuery('sys_id', data.sys_id);
inRec1.query();
if(inRec1.next()) {
inRec1.setValue('state', 'Approved');
inRec1.setValue('approver', gs.getUserID());
gs.addInfoMessage("Accept Approval Processed");
inRec1.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2021 05:02 AM
ServiceNow says I'm not able to run a log test.
"Function log is not allowed in scope. Use gs.debug() or gs.info() instead"

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2021 05:03 AM
use console.log or gs.info, validate the log
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2021 05:21 AM
This is what I'm getting.