Business Rule not updating the required fields
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 06:01 AM
I'm trying to write a business rule that is running from the Position (sn_hr_core_position) table and I'm wanting it to run on the following triggers:
If 'Job Profile' -> 'Job Family' CHANGES
OR
If 'Job Profile' -> 'Job Family Group' CHANGES
The script for my business rule is:
If somebody could let me know what I'm doing wrong of if it's even possible to dot walk through fields, that would be great.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 07:56 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 08:19 AM
in the BR script search the sn_hr_core_position table with query as this and then update
like this
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var gr = new GlideRecord("sn_hr_core_position");
gr.addQuery("u_job_profile", current.sys_id);
gr.query();
if (gr.next()) {
var userRec = new GlideRecord('sys_user');
userGR.addQuery('sys_id', gr.u_hr_profile.user);
userGR.query();
if (userGR.next()) {
if (current.u_job_family.changes())
userGR.u_job_family = current.u_job_family;
else if (current.u_job_family_group.changes())
userGR.u_job_family_group = current.u_job_family_group;
userGR.update();
}
}
}
})(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-06-2024 12:26 AM
It works when the fields are changed separately. However, what I've noticed is that if I change both of the fields at the same time, it doesn't work. What code would I need to add to cover if both fields are changed at the same time?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2024 12:37 AM
Glad to know that my script worked.
For that you can have another BR with condition as Both changes and make this script
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var gr = new GlideRecord("sn_hr_core_position");
gr.addQuery("u_job_profile", current.sys_id);
gr.query();
if (gr.next()) {
var userRec = new GlideRecord('sys_user');
userGR.addQuery('sys_id', gr.u_hr_profile.user);
userGR.query();
if (userGR.next()) {
userGR.u_job_family = current.u_job_family;
userGR.u_job_family_group = current.u_job_family_group;
userGR.update();
}
}
}
})(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-06-2024 01:44 AM
Hi @Ankur Bawiskar Thanks for your help. It'll update on some records, but not on others and I don't know why.