Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

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);
Kindly mark my answer as Correct and helpful based on the Impact.
Thanks and Regards,
Alok

View solution in original post

30 REPLIES 30

Any reason, why you are not doing this in before insert BR? Doing so, you don't need current.update() as well.

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

Regards,
Asif
2020 ServiceNow Community MVP

Yes, keep the working script as a Workaround and try with 

Before

Insert: True

Update: True

Script:

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

If the above works then please don't forget to mark correct and helpful based on the Impact.

Else, you can keep your workaround with updating setWorkflow(false) just above the current.update().

Regards,

Alok

Kindly mark my answer as Correct and helpful based on the Impact.
Thanks and Regards,
Alok

Thanks Alok,

I had to go with after insert business rule as the before insert was not working for my use case.  I am using current.setWorkflow(false) 

Thanks for all your suggestions @asifnoor .

Yes, there already a script OOB to set the Source on the HR case when case created from Portal . Now I am trying to set the Source and assigned to for HRcases when created from native view . 

Okay. If my comment(s) has helped you to achieve the problem, kindly mark them as helpful.