Hi, Need to update the order of the sysapproval table to associated sysapproval_group table order

rajeshKongamudi
Tera Contributor
Hi Community, In the script below, we are working with the sysapproval_approver table to update the order field. The goal is to transfer the updated order value from the sysapproval_approver table to the corresponding order field in the sysapproval_group table. This ensures that the correct order value is used for notifications and for triggering approval processes. please help us to achieve this, Thanks in advance.
 
(function executeRule(current, previous /*null when async*/) {
    // Query the sc_req_item to get the RITM based on sysapproval (assuming sysapproval is related to the RITM)
    var gm = new GlideRecord('sc_req_item');
    gm.addEncodedQuery('cat_item=3c1f0743879b9e90dc8b43730cbb3598^sys_id=' + current.sysapproval);
    gm.query();
   
    if (gm.next()) {
        var ritm_id = gm.sys_id; // RITM ID found
        gs.log("The RITM ID is: " + ritm_id);
       
        // Now, query sysapproval_group to get the group order
        var appr = new GlideRecord('sysapproval_approver');
        appr.addEncodedQuery('group.assignment_group=e8e71a671b536110d06b117a3b4bcbd1^ORgroup.assignment_group=09b80f5187ef9a10222aebd90cbb3559^sysapproval=' + ritm_id);
        appr.orderByDesc('order');
        appr.query();
        var count = 0;
       
        // If we find the group in sysapproval_approver, increment the order
        if (appr.next()) {
            var app_order = appr.order; // Get the current approval order
            gs.log("The current approval order is: " + app_order);

 

            count = app_order + 1; // Increment the order
            gs.log("The incremented approval order is: " + count);
           
            // Update the approval group order in sysapproval_approver table
            appr.order = count;
           // Save the updated order in sysapproval_approver
            gs.log("The updated approval group order is: " + count);

 

            // Now, find and update the sysapproval_group table using the sysapproval field from the appr record
            var approvalGroup = new GlideRecord('sysapproval_group');
            if (approvalGroup.get(appr.sysapproval)) {
                approvalGroup.order = count;  // Update the order field in sysapproval_group
                approvalGroup.update();  // Save the updated order in sysapproval_group
                gs.log("The updated sysapproval_group order is: " + count);
            } else {
                gs.log("Sysapproval group record not found for the specified RITM.");
            }

 

            // Trigger an event if needed
            gs.eventQueue('test.sample', current, current.group.assignment_group);
        }
        else {
            gs.log("No matching approval records found for the specified RITM.");
        }
    } else {
        gs.log("No RITM found for the specified sysapproval.");
    }

 

})(current, previous);
6 REPLIES 6

Hi @Ankur Bawiskar,

It is not working as intended; however, I have found an alternative solution. Thank you for your response.

@rajeshKongamudi 

what didn't work and what worked?

Please share

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader