- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 01:10 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 09:52 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 10:10 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 09:11 PM
@Ankur Bawiskar It is on the cloud application table. It should run on insert and update before the managed by changes.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 10:10 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2023 11:05 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 09:52 PM
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