Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

updating the field value using business rule

2022_ServiceNow
Tera Expert

Hi, I have a business rule which runs on one of the table.

This script is updating the department based on the managed by.

But not updating the 'u_managed' based on the condition.

The condition should be if department contains 'IT' then update 'u_managed' to 'IT' otherwise 'xyz'

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

    var ao = current.managed_by;
    if (ao) {
        var userGr = new GlideRecord('sys_user');
        userGr.get(ao );
        current.department= userGr.department;
    }
    if (current.department && department.toLowerCase().indexOf('it') != -1) {
        current.u_managed = 'IT';
    } else {
        current.u_managed = 'xyz';
    }
})(current, previous);

 @Ankur Bawiskar @Amit Gujarathi 

2 ACCEPTED SOLUTIONS

Amit Gujarathi
Giga Sage
Giga Sage

HI @2022_ServiceNow ,
I trust you are doing great.
Please find below updated code for the same

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

    var ao = current.managed_by;
    if (ao) {
        var userGr = new GlideRecord('sys_user');
        userGr.get(ao);
        current.department = userGr.department;
    }
    
    // Ensure that the department value is not null before performing string operations
    if (current.department) {
        // Convert the department value to lowercase and check if it contains 'it'
        if (current.department.toLowerCase().indexOf('it') != -1) {
            current.u_managed = 'IT';
        } else {
            current.u_managed = 'xyz';
        }
    }

})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

@2022_ServiceNow 

update as this and it will work fine

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

	var ao = current.managed_by;
	if (ao) {
		var userGr = new GlideRecord('sys_user');
		userGr.addQuery('sys_id', ao);
		userGr.addQuery('department.name', 'LIKE', 'it');
		userGr.query();
		if(userGr.hasNext())
			current.u_managed = 'IT';
		else 
			current.u_managed = 'xyz';
	}

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Danish Bhairag2
Tera Sage

Hi @2022_ServiceNow ,

 

It looks like there's a minor issue with the script provided. In the second condition, you're trying to access the `department` variable directly, which should be accessed using `current.department`. Here's the corrected version of the script:

 

 

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

    var ao = current.managed_by;

    if (ao) {

        var userGr = new GlideRecord('sys_user');

        if (userGr.get(ao)) {

            current.department = userGr.department;

            if (current.department && current.department.toLowerCase().indexOf('it') !== -1) {

                current.u_managed = 'IT';

            } else {

                current.u_managed = 'xyz';

            }

        }

    }

})(current, previous);

 

 

In this corrected script:

 

- We first check if `ao` (managed_by) is not null and retrieve the corresponding `sys_user` record.

- If the `sys_user` record is found, we update the `department` field in the current record based on the `sys_user` record's department.

- Then, we check if the updated `department` contains 'IT' (case insensitive) and update the `u_managed` field accordingly.

 

Please make sure that the `managed_by` field in your table references the `sys_user` table correctly, and the `department` field is a string field that can contain the department information. 

 

Thanks,

Danish

 

@Danish Bhairag2 

Department is getting updated based on the managed by. But based on the department, if the department contains 'IT' then it should update 'u_managed' to 'IT' otherwise to 'xyz'. This is not happening.

Hi @2022_ServiceNow ,

 

Can u apply logs in both if else n let me know till where the code is going. I think some problem with if condition.

 

Thanks,

Danish

 

Ankur Bawiskar
Tera Patron
Tera Patron

@2022_ServiceNow 

business rule is on which table? what type of BR it is?

Please share some details around it

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader