User Criteria for AMER region

Rahul Raja Sami
Tera Guru

Hi, this is my script

(function() {
   
    var userId = gs.getUserID();
    
    var gr = new GlideRecord('sys_user');
    gr.addQuery('sys_id', userId);
    gr.query();
    
    if (gr.next()) {
        if (gr.u_region == 'AMER') {
           
            return true;
        } else {
           
            return false;
        }
    } else {
      
        return false;
    }
})();

 

not sure why it is not working. Please check and tell.

1 ACCEPTED SOLUTION

(function() {
   
    var userId = gs.getUserID();
    
    var gr = new GlideRecord('sys_user');
    gr.addQuery('sys_id', userId);
    gr.query();
    
    if (gr.next()) {
        if (gr.u_region == 'AMER') {
           
            return true;
        } else {
           
            return false;
        }
    } else {
      
        return false;
    }
})();

 

this is working, thank you for the assistance.

View solution in original post

7 REPLIES 7

Deepak Shaerma
Kilo Sage

Hi @Rahul Raja Sami 

Please explain complete requirement,  what you are trying to achieve for better understanding.  But for now i suggest 

To ensure that u_region is the correct field name, 

custom field names usually start with ‘u_’,

The comparison in if (gr.u_region == 'AMER') is case-sensitive. Ensure that the value stored in the database is exactly ‘AMER’ in all uppercase letters. If there’s a possibility of mixed case values like ‘Amer’ or ‘amer’, consider using a case-insensitive comparison:

if (gr.u_region.toUpperCase() == ‘AMER’)

You can use gs.info(), gs.warn(), or gs.error() for debugging:

 

Please mark it helpful and accepted if this helps you in somehow. This will help both community and me.

Thanks and Regards 

Deepak Sharma 

 

Hi Deepak, My script worked on the background scripts but when added to User criteria it is not allowing any Region users except admins.

(function() {
   
    var userId = gs.getUserID();
    
    var gr = new GlideRecord('sys_user');
    gr.addQuery('sys_id', userId);
    gr.query();
    
    if (gr.next()) {
        if (gr.u_region == 'AMER') {
           
            return true;
        } else {
           
            return false;
        }
    } else {
      
        return false;
    }
})();

 

this is working, thank you for the assistance.

Sandeep Rajput
Tera Patron
Tera Patron

@Rahul Raja Sami Could you please update your script as follows and see if it works.

 

(function() {
    var gr = new GlideRecord('sys_user');
    gr.addQuery('sys_id', user_id); //user_id is a global variable available in user criteria.
    gr.query();
    
    if (gr.next()) {
        if (gr.u_region == 'AMER') {
           
            return true;
        } else {
           
            return false;
        }
    } else {
      
        return false;
    }
})();

 

Here user_id is a global variable available in user criteria. For more information please refer to https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0780775