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

@Ankur Bawiskar It is on the cloud application table. It should run on insert and update before the managed by changes.

2022_ServiceNow_0-1698898201274.png

When 'managed_by' changes, it should change the department according to the department of the 'managed_by'. This is working. 

Now if the department contains 'IT', then the field 'u_managed' should change to 'IT' otherwise another value.

@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

@2022_ServiceNow 

Hope you are doing good.

Did my reply answer your question?

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

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