Required After Update Business Rule Script Modification

Rajesh Bandila
Tera Contributor

Hi,

 

I have an reference field called "Assignment Group" on RITM form, which refers the Groups table. when I select the assignment group filed with the group, RITM approvers automatically updated with the Group users.

Here my requirement is When User changes the Assignment Group filed, I need to make it Old approvers state to "No longer Required" and automatically added the new assignment group users to approvers .

 

I have written the below After Update Business rule and it's working fine for old approvers "no longer required" and it's not working properly for adding the new group users as an approvers. Please correct my script if any thing goes wrong?

After Update BR Script:

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

    // Check if the 'requested_for' field has been changed and if the state is 'open' or 'work in progress'
    if (current.assignment_group.changes() && (current.state == 1 || current.state == 2)) {
        // Query the 'sysapproval_approver' table to find related approval records
        var approvalGR = new GlideRecord('sysapproval_approver');
        approvalGR.addQuery('documentId', current.sys_id);
        approvalGR.query();

        // Loop through the records and set them to not be required
        while (approvalGR.next()) {
            approvalGR.state = 'not_required';
            approvalGR.update();
        }

        // Get the updated approver group from 'sys_user_group'
        var groupGR = new GlideRecord('sys_user_group');
        groupGR.addQuery('sys_id', current.assignment_group);
        groupGR.query();

        // If the group exists
        if (groupGR.next()) {
            // Get the users in the current 'Requested For' group from 'sys_user_grmember'
            var userGR = new GlideRecord('sys_user_grmember');
            userGR.addQuery('group', groupGR.sys_id);
            userGR.query();

            // Loop through the users and add them as approvers
            while (userGR.next()) {
                var approverGR = new GlideRecord('sysapproval_approver');
                approverGR.initialize();
                approverGR.documentId = current.sys_id;
                approverGR.approver = userGR.user;
                approverGR.state = 'requested';
                approverGR.insert();
            }
        }
    }

})(current, previous);


Error Message:

RajeshBandila_0-1719838646498.png

 

Thanks & Regards

Bandila Rajesh

 



3 REPLIES 3

Dnyaneshwaree
Mega Sage

Hello @Rajesh Bandila,

Have You checked below Guidelines?:

Your script generally looks good, but there are a few areas to adjust for ensuring it works as expected. Here are the key points to address:

  1. Field References: Ensure you use the correct field references for the sysapproval_approver table and the sys_user_grmember table.
  2. Data Types and Conditions: Check if the conditions on the state and group are being evaluated correctly.
  3. GlideRecord Query and Update Operations: Make sure you're handling the GlideRecord operations correctly and checking for any potential errors in the process.

Here’s a refined version of your script:

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

// Check if the 'assignment_group' field has been changed and if the state is 'open' or 'work in progress'
if (current.assignment_group.changes() && (current.state == 1 || current.state == 2)) {

// Query the 'sysapproval_approver' table to find related approval records
var approvalGR = new GlideRecord('sysapproval_approver');
approvalGR.addQuery('document_id', current.sys_id);
approvalGR.query();

// Loop through the records and set them to 'not_required'
while (approvalGR.next()) {
approvalGR.state = 'not_required';
approvalGR.update();
}

// Get the updated assignment group from 'sys_user_group'
var groupGR = new GlideRecord('sys_user_group');
groupGR.get(current.assignment_group);

// If the group exists
if (groupGR.isValidRecord()) {

// Get the users in the new 'assignment_group' from 'sys_user_grmember'
var userGR = new GlideRecord('sys_user_grmember');
userGR.addQuery('group', groupGR.sys_id);
userGR.query();

// Loop through the users and add them as approvers
while (userGR.next()) {
var approverGR = new GlideRecord('sysapproval_approver');
approverGR.initialize();
approverGR.document_id = current.sys_id;
approverGR.approver = userGR.user;
approverGR.state = 'requested';
approverGR.insert();
}
}
}

})(current, previous);

Key Adjustments:

  1. Field Reference Correction: Ensure you use document_id instead of documentId in the sysapproval_approver GlideRecord query.
  2. Group Record Fetching: Use groupGR.get(current.assignment_group); to directly get the group record.
  3. Validation Check: Use groupGR.isValidRecord() to check if the group record exists.
  4. Field Reference for sys_user_grmember: Ensure group and user fields are correctly referenced.
  5. Commenting and Conditions: Make sure conditions like (current.state == 1 || current.state == 2) are correctly based on your state values.

This revised script should update the state of old approvers to "not required" and add the new assignment group users as approvers.


If my solution helps you any way then mark it as accepted and helpful.

Thank You!!


Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

Thanks @Dnyaneshwaree 

 

I have tried the script in my PDI that you have provided and it's not working as expected. Could you please let me  know what action that I need to perform to make it script worked?

Thanks & Regards,

B Rajesh 

Dnyaneshwaree
Mega Sage

Instead of copy past my code only check instruction, compare the existing and my code and update the required things.

Thank you!!

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru