Help with BR

Salomao_
Tera Contributor
I need help with this BR. This code is limiting the list view to users with specific roles, however, when I switch to an external user and make a request via the portal, he does not have visibility of the REQ. How can I solve?
(function executeRule(current, previous /*null when async*/ ) {
    //Preciso que essas regras só funcionem no "olhar" para a tabela;


    var checkUserItil = gs.getUser().hasRole('itil');
    var checkUseruserAdmin = gs.getUser().hasRole('admin');
    var checkUserSncExternal = gs.getUser().hasRole('snc_external');
    var user = gs.getUser();

    var query = '';
    if (checkUseruserAdmin) {
        query = '';
    }
    if (checkUserItil == true && user.isMemberOf("Financeiro - ASAP") || user.isMemberOf("Financeiro - Abordin")) {
        //gs.addinfoMessage('Regra aplicada: Usuario ITIL');
        // Codigo para usuarios ITIL do grupo Financeiro - ASAP ou Financeiro - Abordin
    } else if (checkUseruserAdmin == false) { //somente user itil true
        //gs.addInfoMessage('TESTE PEDRO ACL: ver quase nada'); //assignment_groupDYNAMICd6435e965f510100a9ad2572f2b47744^EQ
        query = 'assignment_groupDYNAMICd6435e965f510100a9ad2572f2b47744^ORsys_created_onONCurrent minute@javascript:gs.beginningOfCurrentMinute()@javascript:gs.endOfCurrentMinute()^ORsys_created_onONLast minute@javascript:gs.beginningOfLastMinute()@javascript:gs.endOfLastMinute()^ORsys_created_onONCurrent hour@javascript:gs.beginningOfCurrentHour()@javascript:gs.endOfCurrentHour()^ORDERBYDESCnumber';

    } else if (checkUserSncExternal && !checkUseruserAdmin) { //usuario com a role snc_external
        query = 'requested_for=' + user;
    }

    current.addEncodedQuery(query);


})(current, previous);

 

2 REPLIES 2

Muhammad Hamza
Kilo Sage

Hey @Salomao_ ,

I recommend you to change some lines of code:

 

(function executeRule(current, previous /*null when async*/ ) {
    //Preciso que essas regras só funcionem no "olhar" para a tabela;


    var checkUserItil = gs.getUser().hasRole('itil');
    var checkUseruserAdmin = gs.getUser().hasRole('admin');
    var checkUserSncExternal = gs.getUser().hasRole('snc_external');
    var user = gs.getUser();
    // add this line for ID, the above line gives us the User Object
    var userID = gs.getUserID();  // gives us the sys_id of current user

    var query = '';
    if (checkUseruserAdmin) {
        query = '';
    }
    //if (checkUserItil == true && user.isMemberOf("Financeiro - ASAP") || user.isMemberOf("Financeiro - Abordin")) 
    
    if (checkUserItil == true && (user.isMemberOf("Financeiro - ASAP") || user.isMemberOf("Financeiro - Abordin"))) 
    {
        //gs.addinfoMessage('Regra aplicada: Usuario ITIL');
        // Codigo para usuarios ITIL do grupo Financeiro - ASAP ou Financeiro - Abordin
        
       // add a query here(if you have) - if not, this is an empty IF statement which can be removed
   
    } else if (checkUseruserAdmin == false) { //somente user itil true
        //gs.addInfoMessage('TESTE PEDRO ACL: ver quase nada'); //assignment_groupDYNAMICd6435e965f510100a9ad2572f2b47744^EQ
        query = 'assignment_groupDYNAMICd6435e965f510100a9ad2572f2b47744^ORsys_created_onONCurrent minute@javascript:gs.beginningOfCurrentMinute()@javascript:gs.endOfCurrentMinute()^ORsys_created_onONLast minute@javascript:gs.beginningOfLastMinute()@javascript:gs.endOfLastMinute()^ORsys_created_onONCurrent hour@javascript:gs.beginningOfCurrentHour()@javascript:gs.endOfCurrentHour()^ORDERBYDESCnumber';

    } 
    // else if (checkUserSncExternal && !checkUseruserAdmin) { //usuario com a role snc_external
    else if (checkUserSncExternal && (!(checkUseruserAdmin))) { //usuario com a role snc_external

        //query = 'requested_for=' + user;
        // use this instead for query the user
        query = 'requested_for=' + userID;
    }

    current.addEncodedQuery(query);


})(current, previous);

 

Key Points:

  • You have 2 If and 2 Else If statements.
  • If your 1st IF statement is true, the 1st IF statement block will be executed, and your code will also check for the 2nd IF statement, and if its true, then the 2nd IF statement block will also be executed. The other ELSE IF statements won't be checked.
  • If your first two conditions are not true, then the 3rd condition ELSE IF is true, and your 4th condition ELSE IF won't be executed.

This is as far as I can assist you. You can also provide me with the complete scenario for this BR use case. It will be helpful.

 

Kindly appreciate the efforts of community contributors by marking appropriate response as the correct solution and helpful, this may help other community users to follow the right solution in the future.

Thanks,

Hamza

Amit Gujarathi
Giga Sage
Giga Sage

HI @Salomao_ ,
I trust you are doing great.
Here is the revised code with comments explaining the changes:

(function executeRule(current, previous /*null when async*/ ) {
    var user = gs.getUser();
    var checkUserItil = user.hasRole('itil');
    var checkUserAdmin = user.hasRole('admin');
    var checkUserSncExternal = user.hasRole('snc_external');
    
    var query = '';

    if (checkUserAdmin) {
        // If user is an admin, no restrictions applied
        query = '';
    } else if (checkUserItil && (user.isMemberOf("Financeiro - ASAP") || user.isMemberOf("Financeiro - Abordin"))) {
        // ITIL user logic for specific groups goes here
        // Adjust the query based on your specific needs
    } else if (checkUserSncExternal) {
        // Logic for external users
        // Set the query to filter records based on the external user's relationship to the record
        // For instance, if the external user is the 'requested_for', we filter on that
        query = 'requested_for=' + user.getID();
    } else {
        // Other users, possibly with ITIL role but not admin or external
        // Adjust this query to suit your requirements
    }

    // Apply the query to the current list view
    current.addEncodedQuery(query);
})();

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi