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 05:56 AM
You need to start fixing this in the client controller where the "Accept" button click is handled. Where property action
is added to the payload to be sent to the server the approval's id should be added also. Maybe the information is added already. Could you show/post the client controller code where the Accept button click is implemented?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2021 06:02 AM
Ok. Also, when you say "approval's id should be added", what would that look like?
api.controller=function($scope, spUtil) {
/* widget controller */
var c = this;
var r = false;
$scope.accept = function() {
r = confirm("Are you sure you want to Accept this Approval?");
if (r == true) {
$scope.data.action = "acceptApproval";
//confirm("confirm");
spUtil.update($scope);
} else {
alert("Closing dialog box.");
}
}
var x = false
$scope.reject = function() {
x = confirm("Are you sure you want to Reject this Approval?");
if (x == true) {
$scope.data.action = "rejectApproval";
//confirm("confirm");
spUtil.update($scope);
} else {
alert("Closing dialog box.");
}
}
spUtil.recordWatch($scope, 'sysapproval_approver', 'state=requested', function(name, data) {
spUtil.update($scope);
});
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2021 06:33 AM
You should add one more line in the client controller, after
$scope.data.action = "acceptApproval";
where you also add the sys_id of the record to be approved, something like:
$scope.data.id = <the id of the approval for which the button has been approved>;
Of course that means that the button has to know for which approval it is being pressed so that you can retrieve that id.
Then "on the other side" (server script) you will have id available on input:
if (input && input.action == "acceptApproval") {
var inRec1 = new GlideRecord('sysapproval_approver');
if (inRec1.get(input.id)) {
// Update the approval record
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2021 06:35 AM
Think of spUtil.update($scope);
as if it was an XMLHttpRequest transaction (well, it really is just that, but nicely dressed) - you will have available on input
whatever you have sent in (whatever you have added to data
in the client controller).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2021 04:23 AM
Thanks for your help all!!! I used the information you mentioned and was able to arrive at this solution:
While still in the ng-repeat block, I used the variable holding my data and placed it in my accept() function call like below. (Ex: ng-repeat="item in data.tableGroup")
By doing this, I'm technically able to call the 'sys_id' like this: "item.sys_id"
HTML Page:
<button type="button" class="btn btn-success" ng-click="accept(item)">Accept</button>
Client Script:
var r = false;
$scope.accept = function(item) {
r = confirm (Are you sure you want to Accept this approval?);
if (r == true) {
$scope.server.get ({
action: 'acceptApproval',
asset_sys_id: item.sys_id
})
spUtil.update($scope);
} else {
alert('Test 123');
}
}
Server Script:
if(input) {
if(input && input.action == 'acceptApproval') {
var inRec1 = new GlideRecord('sys_approval_approver');
inRec1.addQuery('state', 'requested');
inRec1.addQuery('sys_id', input.asset_sys_id);
inRec1.query();
if(inRec1.next()) {
inRec1.setValue('state', 'Approved');
inRec1.setValue('approver', gs.getUserID());
gs.addInfoMessage("Acceptance of Request Processed);
inRec1.update();
}
}
}