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 08:44 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 08:58 AM
No only update when opened by doesn't have snc_internal role
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 04:25 AM
@Community Alums
Then Please use the below script ,
(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()) { // if opened by doesn't have snc_internal role it wil go inside the loop and update the internal user and consumer
current.internal_user = current.opened_by;
current.consumer = current.opened_by;
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 09:07 AM - edited 10-20-2023 09:09 AM
Hello @Community Alums ,
You can use on before business rule.
Please try the below code:-
var userSys;
var user = new GlideRecord('sys_user');
user.addQuery("user_name", current.opened_by);
user.query();
if (user.next()) {
userSys = user.sys_id.toString();
}
var gr = new GlideRecord("sys_user_has_role");
gr.addQuery("user", userSys);
gr.addQuery("role", "7fcaa702933002009c8579b4f47ffbde"); //snc_internal role
gr.query();
if (gr.next()) {
current.internal_user = userSys; // if internal user is not a reference field the replace userSys with current.opened_by
} else {
current.consumer = userSys;
}
Please Mark my answer Helpful & Accepted if I have answered your question.
Thanks,
Alka

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 09:13 AM
@Community Alums As suggested by others you can choose to make your business rule onBefore but if you choose to keep your business rule an onAfter business rule then you can make the following changes.
(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.setWorkflow(false);
current.update();
})(current, previous);