Business rule to set assigned to as logged in user -Scoped application

Mrman
Tera Guru

Hi All,

I am trying to set the assigned to of a HR case after submitting from create new case in HR case management.

below is the business rule created however it is not working in scoped application.

Before Insert 

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




    var Cusr = gs.getUserID();
	var Cag = current.assignment_group.getDisplayValue();
    if (Cusr.isMemberOf(Cag)){
        current.assigned_to = Cusr;
    }
1 ACCEPTED SOLUTION

You can use the script provide in your after-insert BR, and yes it follows best practice to use current.update() in after-insert Business rule.

Use the script below and it will work.

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

    var Cusr = gs.getUserID();
	//var Cop = current.opened_by;
	gs.info('the logged in user is :' +Cusr);
	var Cag = current.assignment_group.getDisplayValue();
	gs.info('the assignment group is :'+Cag);
    if (gs.getUser().isMemberOf(current.assignment_group.getDisplayValue())){
		gs.info('into my if loop');
        current.assigned_to = Cusr;
		current.update();
    }

})(current, previous);

View solution in original post

30 REPLIES 30

Yes what you said is right, but it has to be avoided. Because using current.update in after update will trigger again all before BRs as its a new update and it can go into recursive loop.

Yup I agree, and that's the reason before-insert/update is always recommended while updating the current record because current.update()is not required in it.

asifnoor
Kilo Patron

Hi,

Try this code.

function executeRule(current, previous /*null when async*/ ) {
    var Cag = current.assignment_group.getDisplayValue();
    if (gs.getUser().isMemberOf(Cag)){
        current.assigned_to = gs.getUserID();
    }
})(current, previous);

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

Hi@asifnoor ,

Tried the code you provide but did not work , assigned to is still blank.

please let where am I doing wrong.

Hi,

If you are running this in scoped application, then instead of isMemberOf, use isMemberOfForScopedApp and check once.

Also place logs inside if condtion to check if its entering the if condition or not.

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP