- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2022 08:35 PM
Hi everyone!
I was referring to this post to try to get the approvers to show up on the Service Portal ticket page.
https://community.servicenow.com/community?id=community_question&sys_id=5ff9520bdb518d10019ac2230596...
I was able to display the approvers.
However, if, for example, approval record B is created after approval record A is approved, the approvers for the previous record are still displayed and the approvers for the next record are added to the list.
In this case, when approval record B is created, we want only that approver to be displayed.
I have added code to the business rule to initialize the field for display to solve this problem, but it does not work.
How can I achieve my requirements?
Table Name: sysapproval_approver
When: After Insert
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id', current.sysapproval);
gr.query();
// Show current approvers on glide_list field "u_current_approvers"
if (gr.u_current_approvers == '') {
if (gr.next()) {
gr.u_current_approvers += ',' + current.approver.toString();
}
// Initialize if there is a value in glide_list field "u_current_approvers"
} else {
gr.u_current_approvers = '';
if (gr.next()) {
gr.u_current_approvers += ',' + current.approver.toString();
}
}
gr.update();
})(current, previous);
Best Regards,
Kou
Solved! Go to Solution.
- Labels:
-
Service Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-22-2022 02:02 AM
Can you turn the logic around and create this BR on the sc_req_item table?
(function executeRule(current, previous /*null when async*/ ) {
current.u_current_approvers = '';
var appr = new GlideRecord('sysapproval_approver');
appr.addQuery('sysapproval',current.getUniqueValue());
appr.addQuery('state','requested')
appr.query();
while(appr.next()){
current.u_current_approvers += appr.approver.toString() + ', ';
current.update();
}
})(current, previous);
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2022 10:51 PM
I think you should add a condition to the BR to only run on approvals that are 'requested', so you only pick up the actual approvals that are still open and start your BR with clearing the u_current_approvers field, so if there are already users in the array, you remove them.
If my answer helped you in any way, please then mark it as helpful. If it resolved the issue, please mark it as correct.
Mark
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-22-2022 01:14 AM
Hi
I added a conditional expression to pick up only those Approval records that are 'requested', but no improvement was seen.
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id', current.sysapproval);
gr.query();
// Only pick up the actual approvals
if (current.state == 'requested') {
gr.u_current_approvers == '';
// Show current approvers on glide_list field "u_current_approvers"
if (gr.u_current_approvers == '') {
if (gr.next()) {
gr.u_current_approvers += ',' + current.approver.toString();
}
}
// Initialize if there is a value in glide_list field "u_current_approvers"
} else {
gr.u_current_approvers = '';
if (gr.next()) {
gr.u_current_approvers += ',' + current.approver.toString();
}
}
gr.update();
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-22-2022 02:02 AM
Can you turn the logic around and create this BR on the sc_req_item table?
(function executeRule(current, previous /*null when async*/ ) {
current.u_current_approvers = '';
var appr = new GlideRecord('sysapproval_approver');
appr.addQuery('sysapproval',current.getUniqueValue());
appr.addQuery('state','requested')
appr.query();
while(appr.next()){
current.u_current_approvers += appr.approver.toString() + ', ';
current.update();
}
})(current, previous);
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2022 12:05 AM
Hi
I just tried your code and the current approver display works fine!
However, another problem was uncovered.
After approving a request, a user with "No Longer Required" status is displayed.
I have updated the code as follows, but it does not work.
(function executeRule(current, previous /*null when async*/ ) {
current.u_current_approvers = '';
var appr = new GlideRecord('sysapproval_approver');
appr.addQuery('sysapproval', current.getUniqueValue());
appr.addQuery('state', 'requested');
appr.query();
while (appr.next()) {
current.u_current_approvers += ',' + appr.approver.toString();
current.update();
}
if (appr.state == 'approved' || appr.state == 'cancelled' || appr.state == 'not_required') {
current.u_current_approvers = '';
current.update();
}
})(current, previous);