- 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 02:18 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2023 02:42 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2023 07:53 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2023 08:26 PM
business rule is on which table? what type of BR it is?
Please share some details around it
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader