Business rule don't work as expected.

JohnDF
Mega Sage

Hi everyone, 

 

i have wrote a business rule. And it should always assign the assigment group of the current logged in user where it starts with "SN-CC" but its still take the first group he found from the user as a assignment group.
Where is my error? Thanks for help.

 

after insert br:

 

 

 

(function executeRule(current, previous /*null when async*/) {
    var isCCMember = false;
    var ccGroup = null;
    var uID = gs.getUserID();
    var groupArr = [];

    // Get all active groups where the user is a member
    var grmember = new GlideRecord('sys_user_grmember');
    grmember.addQuery('user', uID);
    grmember.addQuery('group.active', true);
    grmember.query();

    while (grmember.next()) {
        // Push the group sys_id and name into the group array
        var groupInfo = {
            sys_id: grmember.group.toString(),
            name: grmember.group.name.toString()
        };
        groupArr.push(groupInfo);

       //gs.info("Group Info: " + JSON.stringify(groupInfo));
    }

    for (var i = 0; i < groupArr.length; i++) {
        if (groupArr[i].name.indexOf('SN-CC') !== -1) {
            isCCMember = true;
            ccGroup = groupArr[i].sys_id; // Use sys_id instead of the name
           // gs.addInfoMessage("assignedGroups+ I am" + groupArr[i].name);
        }
    }

    // Determine the assigned group based on conditions
    var assignedGroup = isCCMember ? ccGroup : (groupArr.length > 0 ? groupArr[0].sys_id : null);
    //gs.addInfoMessage("Assigned Group: " + assignedGroup);

    // Make sure 'current' is referring to the correct record
    var currentRecord = new GlideRecord('sn_customerservice_case'); // Replace 'your_table_name' with the actual table name
    if (currentRecord.get(current.sys_id)) {
        currentRecord.assignment_group = assignedGroup;
        currentRecord.update();
       // gs.addInfoMessage("Assignment Group set to: " + assignedGroup + currentRecord.assignment_group);
    } else {
        gs.addErrorMessage("Unable to find the current record.");
    }
    // Set the assigned group to the current record
    //current.assignment_group = assignedGroup;

})(current, previous);

 

 

 

2 ACCEPTED SOLUTIONS

Brad Bowman
Kilo Patron
Kilo Patron

Are you getting expected values in the logs throughout the script, or which one is not correct?  Get rid of the last GlideRecord - you don't need or want to query for the record that is getting created.  This BR should run before insert since you are affecting the same record that is getting created.  This way you won't need another update in the script.  If your assigned group log is incorrect, I would first off not assign null to any variable value, rather an empty value ''.  If this log is incorrect the if statement is not being evaluated correctly, so try changing isCCMember = 'false' then assigning 'true' if the name matches, then use isCCMember = 'true'

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@JohnDF 

you should use before insert BR

Try this script

var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("user", gs.getUserID());
gr.addEncodedQuery("group.nameLIKESN-CC");
gr.setLimit(1);
gr.query();
if (gr.next()) {
	current.assignment_group = gr.getValue('group');
}

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

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Are you getting expected values in the logs throughout the script, or which one is not correct?  Get rid of the last GlideRecord - you don't need or want to query for the record that is getting created.  This BR should run before insert since you are affecting the same record that is getting created.  This way you won't need another update in the script.  If your assigned group log is incorrect, I would first off not assign null to any variable value, rather an empty value ''.  If this log is incorrect the if statement is not being evaluated correctly, so try changing isCCMember = 'false' then assigning 'true' if the name matches, then use isCCMember = 'true'

Luxo Nadappan
Tera Guru

Hi, 

 

There are couple things you can correct.  

1.Create before business rule against  case table

2.Use the same script logic to populate the current assignment group of "case table " (You don't need to glide case table again in this case ).

 

Regards,

Luxo

Maik Skoddow
Tera Patron
Tera Patron

Hi @JohnDF 

I guess the following line of code is the problem as a dot-walk to a referenced record is not possible

 name: grmember.group.name.toString()

 Try instead:

 name: grmember.group.getRefRecord().getValue('name')

Maik

Ankur Bawiskar
Tera Patron
Tera Patron

@JohnDF 

you should use before insert BR

Try this script

var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("user", gs.getUserID());
gr.addEncodedQuery("group.nameLIKESN-CC");
gr.setLimit(1);
gr.query();
if (gr.next()) {
	current.assignment_group = gr.getValue('group');
}

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