Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

display approved groups and rejected groups in notification

AJAYKUMAR G
Tera Contributor

One record went approval on particular state to some groups(like A,B,C and D)- in the group any one can approve
A- approved
B- approved
C-rejected
D-rejected

if any thing rejected found in the approval it went previous state
notification Sent 
"As rejected
Approved Groups
A
B
Rejected Groups
C
D
" first time it is working fine

same process
then again went approval on same state and same groups
A- Rejected
B-Approved
C- Approved
D- Approved
same notification triggered
But in notification 
approved groups both are coming(1st time apprvoed and 2 nd time apprvoed)


Rejected groups both are coming(1st time apprvoed and 2 nd time apprvoed)

I don't want that 
2 nd time what groups given that should be 
Please advise thank you.

var gr = new GlideRecord('sysapproval_approver');
        gr.addQuery('document_id', current.sys_id);
        gr.query();
       
        var approved = [];
        var rejected = [];
        while (gr.next()) {
            var groupName = gr.group.assignment_group.getDisplayValue();
            var status = gr.state.getDisplayValue();
            if (status == 'Approved') {
                approved.push(groupName);
            } else if (status == 'Rejected') {
                rejected.push(groupName);
            }
        }
        var message = "";
        message += "<b>Approved Groups:</b><br>";
        message += approved.length > 0 ? approved.join("<br>") : "None";
        message += "<br><br>";

        message += "<b>Rejected Groups:</b><br>";
        message += rejected.length > 0 ? rejected.join("<br>") : "None";
        message += "<br><br>";

        template.print(message);

    })();




2 REPLIES 2

vaishali231
Tera Guru

hey @AJAYKUMAR G 

 

This behavior is expected because your script is querying all approval records from sysapproval_approver for that document. When the record re-enters the same approval state, ServiceNow creates new approval records, but the old ones still exist. Since you are not filtering by approval cycle, both cycles are being included in the notification.

To ensure that only the latest approval cycle is considered, you can restrict the query to the most recently created approval batch.

script:

(function () {

var approved = [];
var rejected = [];
var latestCreatedOn = '';

// Get latest approval record timestamp
var latestGR = new GlideRecord('sysapproval_approver');
latestGR.addQuery('document_id', current.sys_id);
latestGR.orderByDesc('sys_created_on');
latestGR.setLimit(1);
latestGR.query();

if (latestGR.next()) {
latestCreatedOn = latestGR.sys_created_on;
}

// Fetch only approvals from the latest cycle
if (latestCreatedOn) {

var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id', current.sys_id);
gr.addQuery('sys_created_on', '>=', latestCreatedOn);
gr.query();

while (gr.next()) {

var groupName = gr.group.assignment_group.getDisplayValue();
var state = gr.state.toString();

if (state == 'approved') {
approved.push(groupName);
} else if (state == 'rejected') {
rejected.push(groupName);
}
}
}

var message = '';
message += '<b>Approved Groups:</b><br>';
message += approved.length ? approved.join('<br>') : 'None';
message += '<br><br>';
message += '<b>Rejected Groups:</b><br>';
message += rejected.length ? rejected.join('<br>') : 'None';

template.print(message);

})();

*************************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

 

Regards

Vaishali Singh

 

 

hey @AJAYKUMAR G 

Hope you are doing well.

Did my previous reply answer your question?

If it was helpful, please mark it as correct ✓ and close the thread . This will help other readers find the solution more easily.

Regards,
Vaishali Singh