onAfter Business Rule isn't working as expected

Community Alums
Not applicable

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);
10 REPLIES 10

Community Alums
Not applicable

TEST.PNG

Community Alums
Not applicable

No only update when opened by doesn't have snc_internal role

@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);

 

Alka_Chaudhary
Mega Sage
Mega Sage

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

Sandeep Rajput
Tera Patron
Tera Patron

@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);