Business rule not working as expected

Charan123
Tera Expert

Hi

Created a Business rule on incident table, where it should insert/update the records based on assignment group and active field.
And it should allow maximum 5 records with same assignment group, if more than that it should abort the action. 

BR will trigger on insert/ update operation
Script:

    var recordCount;
    var ga = new GlideAggregate('incident');
    ga.addQuery('active', true);
    ga.addQuery('assignment_group', current.getValue('assignment_group'));
    ga.addAggregate('count');
    ga.query();
    if (ga.next()) {
        recordCount = ga.getAggregate('count');
    }
 
    if (recordCount >=5) {
        gs.addErrorMessage('Maximum 5 record allowed per Assignment group');
        current.setAbortAction(true);
    }

It's working as expected on Insert operation, but
Update Operation - when record count is 5 if we update any fields ( expect AG)  on the incident form it's not allowing ( not updating).

Note: If recount count is 4 or less than that no issue it's updating

Please suggest on this

Thanks!!
1 ACCEPTED SOLUTION

Charan123
Tera Expert

Thanks @Pavankumar_1  for the response, We got the solution for the above issue
Need to include the addQuery( 'sys_id', '!=' ,current.sys_id); it will update the record with out any error.

View solution in original post

2 REPLIES 2

Pavankumar_1
Mega Patron

Hi @Charan123 ,

On your before business rule you need to change the if condition like if count greater than 5 then need to abort the action.

Not sure why you have given 15 it should be > =5 like below.

var recordCount;
    var ga = new GlideAggregate('incident');
    ga.addQuery('active', true);
    ga.addQuery('assignment_group', current.getValue('assignment_group'));
    ga.addAggregate('count');
    ga.query();
    if (ga.next()) {
        recordCount = ga.getAggregate('count');
    }
    if (recordCount >= 5) { //if we have 5 incidents with current asignment group then it should stop action.
        gs.addErrorMessage('Maximum 5 record allowed per Assignment group'));
        current.setAbortAction(true);
    }

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Charan123
Tera Expert

Thanks @Pavankumar_1  for the response, We got the solution for the above issue
Need to include the addQuery( 'sys_id', '!=' ,current.sys_id); it will update the record with out any error.