How can I display only current approvers on ticket page of Service Portal?

Kou1
Tera Expert

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

1 ACCEPTED SOLUTION

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

View solution in original post

10 REPLIES 10

Mark Manders
Mega Patron

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

Hi @Mark Manders 

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);

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

Hi @Mark Manders 

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);