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

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);

For before script, you don't need current.update as its already running on current and update happens internally.

For after insert, i think current.update() does not work as its already inserted. you have to gliderecord and do update.

yes for before-insert/update current.update() is not required because it's going to update. It work but it will work but It's also not a good practice to use current.update()

For after insert/update current.update() works and it's required to update any field on any existing record. Gliderecord and update is required for the field update on other table and records.

Yes, correct. As per this link, it is not a recommended practice to use current.update() in before or after BR.

https://hi.service-now.com/kb_view.do?sysparm_article=KB0715782

Mark the comment as helpful if it helps.

Regards,
Asif
2020 ServiceNow Community MVP

Yes current.update() is not recommended to be used in before-insert/update BR.

But to update current record in After- Insert/Update BR you have to use current.update() because it's already updated and if current.update() is not used it will not update current record.