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

Hi @asifnoor ,

I am trying the below script .I am getting all logs but the assigned to is not getting set . 

 

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

Hi,

I suspect that there is some validation which is stopping the user being assigned.

try setting a hardcoded sys_id of the user jsut to confirm if its being set or not. If that is also not getting set, then it means there could be either anothe BR which might be restricting it or any ui policy or client script which is setting the assigment_to field to empty after loaded.

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

Regards,
Asif
2020 ServiceNow Community MVP

Hi,

Did you try the suggestion that i have given above?

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 @Alok Das 

This is my final script which is working to set the assigned to and source on the HR Case.

I am using the current.update() in the after business rule with out which I am able to do it .

Please suggest if this will cause issues

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

    var Cusr = gs.getUserID();
    var Cst = current.state;
    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');
        if (Cst == 1) {
            current.contact_type = 'phone';
            current.assigned_to = Cop;
            current.update();

        }

    }

})(current, previous);

For being safer side use the below script just before the current.update()

current.setWorkflow(false);

Note:- Using setWorkflow(false) will restrict the auditing of any insert or update of record.