Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

User Criteria Script for catalog item

priyanka1028
Tera Contributor

Hello All 

I am working on User Criteria requirement for a Catalog Item. 

We have a country field on sys_user table (Note: Country field is reference field type and table is core_country).

I only want users whose country is Brazil to see the catalog item.  

How I would be able to script in the Advanced script box of the User Criteria record for this. 

I want to filter it for users whose country is Brazil.

Thanks!!

1 REPLY 1

Gangadhar Ravi
Giga Sage

I think you can achieve this without script too. please check below 

https://www.servicenow.com/community/developer-blog/servicenow-things-to-know-59-extend-servicenow-u...

 

if you want to use advanced script then it will be something like below.

 

// Advanced Script to filter users whose country is Brazil
(function() {
    // Get the current user's country reference field value
    var userCountry = gs.getUser().getRecord().getValue('country');

    // Check if the country reference field is pointing to Brazil
    if (userCountry) {
        var countryGR = new GlideRecord('core_country');
        if (countryGR.get(userCountry) && countryGR.name == 'Brazil') {
            return true;
        }
    }

    // If the country is not Brazil, return false
    return false;
})();