onAfter Business Rule isn't working as expected
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 07:16 AM
Hey Everyone!
I'm trying to figure out what is causing my business rule to not work.
It is an onAfter business that runs when the opened by field changes. If checks to see if the opened_by user has a internal role, and if so, it needs to map to another field internal_user, but it isn't mapping. Both fields are a reference fields consumer and internal_user.
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var gr = new GlideRecord("sys_user_has_role");
gr.addQuery("user", current.opened_by);
gr.addQuery("role", "7fcaa702933002009c8579b4f47ffbde"); //snc_internal role
gr.query();
if (gr.next()) {
gr.internal_user = current.opened_by;
} else {
gr.consumer = current.opened_by;
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 07:26 AM
Hi @Community Alums
You should use current in 'if' condition while setting the value like below..
Assuming you are updating value on current form.
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var gr = new GlideRecord("sys_user_has_role");
gr.addQuery("user", current.opened_by);
gr.addQuery("role", "7fcaa702933002009c8579b4f47ffbde"); //snc_internal role
gr.query();
if (gr.next()) {
current.internal_user = current.opened_by; // here
} else {
current.consumer = current.opened_by; //here
}
})(current, previous);
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 07:53 AM
Hi @Community Alums ,
Here you need to do 2 things,
1. Change the the BR from after to before update BR in when to run( Basically it will run just before the opened by field changes saved in the database) .
2. in your script 'if' and 'else' part , Use current, like below
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var gr = new GlideRecord("sys_user_has_role");
gr.addQuery("user", current.opened_by);
gr.addQuery("role", "7fcaa702933002009c8579b4f47ffbde"); //snc_internal role
gr.query();
if (gr.next()) {
current.internal_user = current.opened_by;
} else {
current.consumer = current.opened_by;
}
})(current, previous);
Please mark my answer as correct based on Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 08:08 AM
So, the consumer field isn't mapping even after that.
But the internal_user field is working as expected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 08:12 AM - edited 10-20-2023 08:16 AM
@Community Alums
Because the script got executed and it went to 'IF' loop, So it just updated the internal_user .
If you want update 'consumer' with 'opened_by' field value when the opened_by user having internal_role. please change the script like below
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var gr = new GlideRecord("sys_user_has_role");
gr.addQuery("user", current.opened_by);
gr.addQuery("role", "7fcaa702933002009c8579b4f47ffbde"); //snc_internal role
gr.query();
if (gr.next()) {
current.internal_user = current.opened_by;
current.consumer = current.opened_by;
}
})(current, previous);