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

Hi 

I am seeing the logs and I also checked the update checkbox , but still assigned to is not populated

(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, previous);

You are using after update BR so you should use current.update() please find the updated script below:

(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

Also, I believe the script which I provided in the very first comment will work but in your original question you mentioned it's before-insert business rule and in the screenshot I can see that it's after-insert business rule.

If you try my very first script in before insert that will also work.

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

Hi @Alok Das ,

Please let me know if it is best practice to use 'current.update'

I have tried with before insert initially bu the business rule was not getting trig erred and logs were not coming .

I have tried it again with before insert now and not seeing logs .

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

Using current.update() in before-insert/update business rule is not a best practice but you can add current.update() in after business rule.

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