Update the RITM approvers as the current Assignment Group Members

Rajesh Bandila
Tera Contributor

Hi,

 

I have an Requirement, if user changes the RITM assignment group, I need to automatically insert the assignment group members as an RITM approvers with the state "Requested".

 

I have written the below Before Update Business rule and it's not working as expected. Could you please help me on this?

Before Update BR:

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

    // Check if the assignment group has been changed
    if (current.assignment_group.changes()) {
        // Get the sys_id of the new assignment group
        var newGroupSysId = current.assignment_group.toString();

        // Query the sys_user_grmember table to get all users in the new group
        var grMember = new GlideRecord('sys_user_grmember');
        grMember.addQuery('group', newGroupSysId);
        grMember.query();

        // Loop through all users in the group
        while (grMember.next()) {
            // Create a new approval for each user
            var approval = new GlideRecord('sysapproval_approver');
            approval.initialize();
            approval.state = 'requested'; // Set the state to 'requested'
            approval.source_table = 'sc_req_item'; // Set the source table
            approval.document_id = current.sys_id; // Link the approval to the current RITM
            approval.approver = grMember.user; // Set the approver to the current user in the loop
            approval.insert(); // Insert the new approval record
        }
    }

})(current, previous);

 

 

Thanks & Regards,

Rajesh B




1 ACCEPTED SOLUTION

Hi @Rajesh Bandila ,

add this line :  approval.sysapproval = current.sys_id; 

This should start creating the new Approvals under your RITM.

You need to set Approval for[sysapproval] field in the Request as well. Then only your request will be linked to your RITM.

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

    // Check if the assignment group has been changed
    if (current.assignment_group.changes()) {
        // Get the sys_id of the new assignment group
        var newGroupSysId = current.assignment_group.toString();

        // Query the sys_user_grmember table to get all users in the new group
        var grMember = new GlideRecord('sys_user_grmember');
        grMember.addQuery('group', newGroupSysId);
        grMember.query();

        // Loop through all users in the group
        while (grMember.next()) {
            // Create a new approval for each user
            var approval = new GlideRecord('sysapproval_approver');
            approval.initialize();
            approval.state = 'requested'; // Set the state to 'requested'
            approval.source_table = 'sc_req_item'; // Set the source table
            approval.document_id = current.sys_id; // Link the approval to the current RITM
            //===========================================
            approval.sysapproval = current.sys_id; // ADD THIS LINE
           //============================================
            approval.approver = grMember.user; // Set the approver to the current user in the loop
            approval.insert(); // Insert the new approval record
        }
    }

})(current, previous);

 

 Modification:

Instead of using current.assignment_group.changes(), you can add this condition in your Business Rules When To Run Condition : i.e Assignment group changes

 

Thanks,

Hope this helps.

If my response turns useful please mark it helpful and accept it as solution.😊

 

 

View solution in original post

4 REPLIES 4

HrishabhKumar
Kilo Sage

Hi @Rajesh Bandila ,

I've modified your code little bit, mainly deleted the old approvals before adding the new approvers and added a try catch logic in adding the approvers. Try this code and see if it works:

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

    // Check if the assignment group has been changed
    if (current.assignment_group.changes()) {
        var newGroupSysId = current.assignment_group.toString();

        // Remove existing approvers for the RITM
        var existingApprovers = new GlideRecord('sysapproval_approver');
        existingApprovers.addQuery('source_table', 'sc_req_item');
        existingApprovers.addQuery('document_id', current.sys_id);
        existingApprovers.deleteMultiple();

        // Query the sys_user_grmember table to get all users in the new group
        var grMember = new GlideRecord('sys_user_grmember');
        grMember.addQuery('group', newGroupSysId);
        grMember.query();

        // Loop through all users in the group
        while (grMember.next()) {
            // Create a new approval for each user
            var approval = new GlideRecord('sysapproval_approver');
            approval.initialize();
            approval.state = 'requested'; // Set the state to 'requested'
            approval.source_table = 'sc_req_item'; // Set the source table
            approval.document_id = current.sys_id; // Link the approval to the current RITM
            approval.approver = grMember.user; // Set the approver to the current user in the loop

            try {
                approval.insert(); // Insert the new approval record
            } catch (ex) {
                gs.error('Error inserting approval record: ' + ex);
            }
        }
    }

})(current, previous);

 

Thanks,

Hope this helps.

If my response turns useful please mark it helpful and accept it as solution.

Thanks @HrishabhKumar 

 

I have tried the above script and it's not working. Could you please help me on this? 

Or let me know if any other possible ways we can complete this requirement?

 

Thanks,

B Rajesh

Hi @Rajesh Bandila ,

add this line :  approval.sysapproval = current.sys_id; 

This should start creating the new Approvals under your RITM.

You need to set Approval for[sysapproval] field in the Request as well. Then only your request will be linked to your RITM.

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

    // Check if the assignment group has been changed
    if (current.assignment_group.changes()) {
        // Get the sys_id of the new assignment group
        var newGroupSysId = current.assignment_group.toString();

        // Query the sys_user_grmember table to get all users in the new group
        var grMember = new GlideRecord('sys_user_grmember');
        grMember.addQuery('group', newGroupSysId);
        grMember.query();

        // Loop through all users in the group
        while (grMember.next()) {
            // Create a new approval for each user
            var approval = new GlideRecord('sysapproval_approver');
            approval.initialize();
            approval.state = 'requested'; // Set the state to 'requested'
            approval.source_table = 'sc_req_item'; // Set the source table
            approval.document_id = current.sys_id; // Link the approval to the current RITM
            //===========================================
            approval.sysapproval = current.sys_id; // ADD THIS LINE
           //============================================
            approval.approver = grMember.user; // Set the approver to the current user in the loop
            approval.insert(); // Insert the new approval record
        }
    }

})(current, previous);

 

 Modification:

Instead of using current.assignment_group.changes(), you can add this condition in your Business Rules When To Run Condition : i.e Assignment group changes

 

Thanks,

Hope this helps.

If my response turns useful please mark it helpful and accept it as solution.😊

 

 

Hi @HrishabhKumar 

 

Script working as expected. Thank you!!

 

Thanks,

Rajesh B