REGION FIELD IN CLEINT SCRIPT
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 12:10 AM
Can somebody help want wrong i am doing in the below on load script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 12:16 AM
Hi @avinashdubey103 ,
You are dot walking second level using client side API , you can only dot walk to fields that are on sys_user table.
Can you check the value you are receiving on the useregion variable by using an alert.
To get the region value you may need to use GlideAjax
Please mark correct/helpful if my response helped you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 01:00 AM
Hi , I have used glide ajax can you please help to identify issue
var GetUserRegion = Class.create();
GetUserRegion.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// This function will fetch the user's region based on their location
getRegion: function() {
var userId = gs.getUserID(); // Get the current user's Sys ID
gs.info('Bhagat Singh: Fetching region for user with Sys ID: ' + userId); // Log user ID
var userGR = new GlideRecord('sys_user'); // Query the User table
if (userGR.get(userId)) {
// Dot-walk to get the region from the user's location
var userRegion = userGR.location.u_region; // Dot-walking to fetch the u_region field directly
// Log the fetched region
gs.info('Bhagat Singh: Region fetched from location: ' + userRegion); // Log the region
// Ensure we return a default value if no region is found
return userRegion || ''; // Return an empty string if no region is found
} else {
gs.error('Bhagat Singh: No user record found for Sys ID: ' + userId); // Log error if no user record is found
}
return ''; // Return an empty string if no region is found
}
});
var GetUserRegion = Class.create();
GetUserRegion.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRegion: function() {
var userId = gs.getUserID();
gs.info('Bhagat Singh: Fetching region for user with Sys ID: ' + userId); // Log user ID
var userGR = new GlideRecord('sys_user');
if (userGR.get(userId)) {
var userRegion = userGR.location.u_region; // Dot-walking to fetch the u_region field directly
// Log the fetched region
gs.info('Bhagat Singh: Region fetched from location: ' + userRegion); // Log the region
// Ensure we return a default value if no region is found
return userRegion || ''; // Return an empty string if no region is found
} else {
gs.error('Bhagat Singh: No user record found for Sys ID: ' + userId); // Log error if no user record is found
}
return '';
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 01:09 AM
Hi @avinashdubey103 ,
For GlideAjax usage you need to first set the parameter of user id in your client script as in below sample client script :
var currentUserId = g_form.getValue('sys_id');
// Create a GlideAjax object
var ga = new GlideAjax('MyScriptInclude');
// Add parameters to the call
ga.addParam('sysparm_name', 'myMethod'); // Name of the method in script include
ga.addParam('sysparm_user_id', currentUserId); // Pass current user ID
// Call the script include and handle the response
ga.getXMLAnswer(parseResponse);
}
// Callback function to handle the response
function parseResponse(response) {
var userName = response; // Response will be the user name returned from the server
// Update a field with the user name
g_form.setValue('user_name', userName);
}
Now in your script include you need to receive the parameter as below :
var MyScriptInclude = Class.create();
MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
myMethod: function() {
// Perform server-side actions here
var gr = new GlideRecord('sys_user');
var userId = this.getParameter('sysparm_user_id'); // Get parameter from client
gr.get(userId);
var userName = gr.user_name;
return userName;
},
type: 'MyScriptInclude' // Required for AbstractAjaxProcessor
});
Please mark correct/helpful if my response helped you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 12:27 AM
I assume you are having location field on form which is reference and that location table has u_region as string field
if yes then update your script as this, but it will work only when location is populated
function onLoad() {
var userRegion = g_form.getReference('location').u_region;
var field1 = g_form.getControl('u_shipping_label');
var field2 = g_form.getControl('u_shipping_return_label');
alert('Region is' + userRegion);
if (userRegion == 'AMERICAS' || userRegion == 'EMEA') {
g_form.setDisplay('field1', true);
g_form.setDisplay('field2', true);
} else {
g_form.setDisplay('field1', false);
g_form.setDisplay('field2', false);
}
}
OR
If you are talking about logged in user's location and then checking u_region field, then use onLoad + GlideAjax
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader