Adding approval email and name as a comment on RITM

vamshi2
Tera Contributor

Hi Team

 

i created async BR with condition as addition of comment is equal to false and with below code

 

(function executeRule(current, previous /*null when async*/ ) {

    // Wait for 2 minutes
    //gs.sleep(120000); // Delay for 2 minutes
    var seconds = parseInt(120, 10) * 1000;
    var start = parseInt(new Date().getTime()) + seconds;
    while (start > parseInt(new Date().getTime())) {
        // do nothing

    }
    // Retrieve the associated RITM
    var ritmSysId = current.sysapproval.sys_id;

    // Collect all approver details
    var approvalGr = new GlideRecord('sysapproval_approver');
    approvalGr.addQuery('sysapproval', ritmSysId); // Match approvals related to the RITM
    approvalGr.addQuery('u_addition_of_comment', false); // Only unprocessed records
    approvalGr.query();

    var individualApprovals = [];
    while (approvalGr.next()) {

        if (approvalGr.approver) {
            // Individual-level approval
            individualApprovals.push(approvalGr.approver.getDisplayValue() + ' (' + approvalGr.approver.email + ')');
        }

        // Mark the approval as processed
        approvalGr.u_addition_of_comment = true;
        approvalGr.update();
    }

    // var groupapproval = new GlideRecord('sysapproval_group');
    // if (groupapproval.get(ritmSysId)) {
    //     // Construct the consolidated comment

    //     var comment = '';
    //     if (individualApprovals.length > 0) {
    //         comment += 'Your service request is currently awaiting approval from the group. To proceed, it requires approval from one of the designated approvers listed below: \n' + individualApprovals.join(',\n') + '\n';
    //     }
    // } else {

    // Construct the consolidated comment

    var comment = '';
    if (individualApprovals.length > 0) {
        comment += 'Your service request is currently awaiting approval from\n' + individualApprovals.join(',\n') + '\n';
    }
    //}



    // Update the RITM with the consolidated comment
    if (comment) {
        var ritmGr = new GlideRecord('sc_req_item');
        //ritmGr.addQuery('sys_id',ritmSysId);

        if (ritmGr.get(ritmSysId)) {

            var lastcomment = ritmGr.comment.getJournalEntry(1);
            if (lastcomment != comment) {
                ritmGr.comments = comment; // Add the comment to the RITM
                ritmGr.update();
            }
        }
    }


})(current, previous);
 
 
but it is working first time when group of approval triggered  updating only in one comment with all the list of approval and it is not working for next time it is updating comment with 'n' times as per 'n' approval records creation
4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

What are the Filter conditions on the When to run tab?  Is there a Condition on the Advanced tab?

Hi @Brad Bowman   it is custom field which is used as filter condition in the above script 

 

vamshi2_0-1734698873214.png

 

Uncle Rob
Kilo Patron

I'd build this in flow designer instead.
Easier to articulate, and WAY easier to walk through each transaction.

Mark Manders
Mega Patron

https://www.servicenow.com/community/developer-forum/how-to-add-list-of-approver-details-on-ritm/td-...


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark