Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

how to get user sub region if no variable name 'subregion ' available in given record producer

suryateja07
Tera Contributor
 
4 REPLIES 4

Tanushree Maiti
Tera Patron

Hi @suryateja07 

 

To get a user's sub-region by dot-walking, you must traverse through the user's Location (cmn_location) table, as the standard sys_user record typically only holds top-level location references rather than a direct "sub-region" field

 

e.g if you are looking for user city to be keep in sub region , you can take help from this sample script.

 

var gr = new GlideRecord('sys_user');
if (gr.get('USER_SYS_ID_HERE'))// Replace with actual user sys_id
{
var userCity = gr.location.city.toString();
gs.info('The user\'s city is: ' + userCity);
}

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Yes this will work but my question is I want to get sub region in client script to hide some of the options based on the condition.

For that you may use Client Script along with a Client Callable Script Include

Example:

Client Script

function onLoad() {
    var userId = g_user.userID;

    if (userId) {
        var ga = new GlideAjax("UserLocationUtil");
        ga.addParam("sysparm_name", "getUserCity");
        ga.addParam("sysparm_user", userId);
        ga.getXMLAnswer(processRegionResponse);
    }
}

function processRegionResponse(answer) {
    if (answer) {
        if (answer == "New York") {
            g_form.removeOption("your_variable_name", "option_value_to_hide");
        }
    }
}


Script Include:

 

 

var UserLocationUtil = Class.create();
UserLocationUtil.prototype = Object.extendsObject(AbstractScriptProcessor, {

    getUserCity: function() {
        var userId = this.getParameter("sysparm_user");
        var userGR = new GlideRecord("sys_user");
        
        if (userGR.get(userId)) {
            return userGR.location.city.toString(); 
        }
        return "";
    },

    type: 'UserLocationUtil'
});

 

Make Sure that the Script Includes Client Callable checkbox is toggled on 

Nishant8
Tera Sage

Hello @suryateja07 , If you want to pull the region details for the logged-in user, you can store them in the g_scratchpad using a Display Business Rule and access them elsewhere via a Client Script. However, if you need to find the region details for other users dynamically, you should use the GlideAjax approach (a Script Include and Client Script combination). I have listed both approaches below.

 

GlideScratchpad approach

Business rule (When: display)

(function executeRule(current, previous /*null when async*/) {
    
    var userId = gs.getUserID();
    var userGR = new GlideRecord('sys_user');

    if (userGR.get(userId)) {
        var locationId = userGR.getValue('location');
        if (locationId) {
            g_scratchpad.subregion = findRegionInHierarchy(locationId);
        } else {
            g_scratchpad.subregion = '';
        }
    } else {
        g_scratchpad.subregion = '';
    }

    // Helper function moved from your script include
    function findRegionInHierarchy(locationId) {
        var maxDepth = 10;
        var currentId = locationId;

        while (currentId && maxDepth > 0) {
            var locGR = new GlideRecord('cmn_location');
            if (!locGR.get(currentId)) {
                break;
            }

            var locationType = locGR.getValue('cmn_location_type');
            if (locationType === 'region') {
                return locGR.getValue('name');
            }

            currentId = locGR.getValue('parent');
            maxDepth--;
        }
        return '';
    }
})(current, previous);

Client Script:

function onLoad() {
    var subregion = g_scratchpad.subregion;

    if (subregion) {
        g_form.addInfoMessage('Your subregion: ' + subregion);
    } else {
        g_form.addInfoMessage('No subregion found for the current user.');
    }
}

 

- GlideAjax Approach:

Script Include:

var UserSubregionAjax = Class.create()

UserSubregionAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getSubregion: function () {
        var userId = this.getParameter('sysparm_user_id') || gs.getUserID();
        var userGR = new GlideRecord('sys_user');

        if (!userGR.get(userId)) {
            return JSON.stringify({ subregion: '' });
        }

        var locationId = userGR.getValue('location');

        if (!locationId) {
            return JSON.stringify({ subregion: '' });
        }

        var subregion = this._findRegionInHierarchy(locationId);
        return JSON.stringify({ subregion: subregion });
    },

    _findRegionInHierarchy: function (locationId) {
        var maxDepth = 10;
        var currentId = locationId;

        while (currentId && maxDepth > 0) {
            var locGR = new GlideRecord('cmn_location');
            if (!locGR.get(currentId)) {
                break
            }

            var locationType = locGR.getValue('cmn_location_type')
            if (locationType === 'region') {
                return locGR.getValue('name');
            }

            currentId = locGR.getValue('parent');
            maxDepth--;
        }

        return '';
    },

    type: 'UserSubregionAjax',
})

Client Script:

function onLoad() {
    var ga = new GlideAjax('UserSubregionAjax');
    ga.addParam('sysparm_name', 'getSubregion');
	ga.addParam('sysparm_user_id', g_form.getValue('<Field Name>'));
    ga.getXMLAnswer(function (answer) {
        var result = JSON.parse(answer);
        var subregion = result.subregion;

        if (subregion) {
            g_form.addInfoMessage('Your subregion: ' + subregion);
        } else {
            g_form.addInfoMessage('No subregion found for the current user.');
        }
    })
}

 

Regards,

Nishant