How to set default value of logged in user region

Gopal14
Tera Contributor

Hi Team,

 

I want to set logged in user region in region field. 

 

joy is an agent, he is part of two groups having same region, need to check two groups region using in group members table. if two groups are part of germany region, germany region autopopulate in the region field in case table. another case, joy is an agent, he is part of three groups having different region, need to check three groups region using in group members table. if three groups are having three different regions, then region field should be empty, so that agent will fill that. For this I want to create a script include and call that script include in default value(dictionary) of region field

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Gopal14 

seems similar question asked by 1 other member

How to set assignment group based on logged in user region 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Runjay Patel
Giga Sage

Hi @Gopal14 ,

 

You can create script include like below and call it from dictionary.

var RegionHelper = Class.create();
RegionHelper.prototype = {
    initialize: function () {},

var userSysId = gs.getUserID();

        var regions = [];
        var groupMemberGR = new GlideRecord('sys_user_grmember');
        groupMemberGR.addQuery('user', userSysId); // Get user's group memberships
        groupMemberGR.query();

        while (groupMemberGR.next()) {
            var groupGR = new GlideRecord('sys_user_group');
            if (groupGR.get(groupMemberGR.group)) {
                if (groupGR.region) { // Ensure the group has a region field
                    regions.push(groupGR.region.toString());
                }
            }
        }

        // Check if all regions are the same
        var uniqueRegions = Array.from(new Set(regions)); // Get unique regions
        if (uniqueRegions.length === 1) {
            return uniqueRegions[0]; // Return the single region
        }

        // Return empty if there are multiple or no regions
        return '';
    },

    type: 'RegionHelper'
};

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

PritamG
Mega Guru

you can create a Script Include to determine the region and call it in the default value of the region field.

Script Include:

Create a Script Include (accessible from Client callable: true) to fetch regions based on group memberships. 

var GetUserRegion = Class.create();
GetUserRegion.prototype = {
    initialize: function() {},

    getRegion: function(userId) {
        var groupRegionMap = {};
        var gr = new GlideRecord('sys_user_grmember'); 
        gr.addQuery('user', userId);
        gr.query();
        while (gr.next()) {
            var group = gr.group.toString();
            var region = new GlideRecord('sys_user_group'); 
            if (region.get(group)) {
                groupRegionMap[region.region.toString()] = true;
            }
        }
        var regions = Object.keys(groupRegionMap);
        return regions.length === 1 ? regions[0] : ''; 
    },

    type: 'GetUserRegion'
};

Set Default Value:

Call the Script Include in the default value of the region field in the Case table.

(function executeRule(current) {
    var userId = gs.getUserID();
    var userRegion = new GetUserRegion().getRegion(userId);
    return userRegion;
})(current);

This ensures the region field is populated with a single region if consistent or left empty for multiple regions.

mike533mike
Tera Contributor

You can create a Script Include that checks the regions of the groups an agent (Joy) belongs to by querying the "Group Members" table. In the Script Include, check if all the groups belong to the same region (e.g., Germany). If they do, set the "Region" field to Germany. If there are different regions, leave the "Region" field empty, allowing the agent to fill it manually. The Script Include can be called in the default value of the "Region" field in the dictionary, ensuring it auto-populates or remains empty based on the group regions.