Populate or Clear 'Country' Field in Incident Table Based on User's Assigned Countries
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 09:28 PM
Hi Team,
I have a custom field called country in incident table, If user is having one country we need populate that country in country field in incident table. If user is having more than one country, then country field should be empty in incident table.
for this I have created relationship for groups and locations, that relationship is added in the groups table as a related list
Note: Table and field name are not same, will change.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 09:56 PM
Yes You can achieve using script include and onchange client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 10:40 PM
hi @Gopal14
Yes you can use script include as bellow
Script include:
var IncidentCountryHelper = Class.create();
IncidentCountryHelper.prototype = {
initialize: function() {
},
// Function to get the country of a user and return the appropriate value
getUserCountry: function(userSysId) {
var userCountries = new GlideRecord('user_country_table'); // Replace with actual table name
userCountries.addQuery('user', userSysId); // Replace with the actual field if needed
userCountries.query();
var countryCount = 0;
var countryToPopulate = '';
// Loop through the countries and determine how many countries the user has
while (userCountries.next()) {
countryCount++;
countryToPopulate = userCountries.country; // Replace with the correct country field
}
// If only one country, return it; otherwise, return an empty string
if (countryCount == 1) {
return countryToPopulate;
} else {
return ''; // Multiple countries, return empty
}
},
type: 'IncidentCountryHelper'
};
Now, you can call this Script Include from a Business Rule to populate the country field on the incident record.
Business rule:
(function executeRule(current, previous /*null when async*/) {
// Create an instance of the Script Include
var incidentHelper = new IncidentCountryHelper();
// Get the assigned user's country (replace 'assigned_to' with the actual user field if different)
var userCountry = incidentHelper.getUserCountry(current.assigned_to); // Pass the assigned user Sys ID
// If the user has one country, populate it on the incident; otherwise, clear the field
current.country = userCountry; // Will be empty if there are multiple countries or one country
})(current, previous);
I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.
thank you
Rajesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 10:20 PM
Hello @Gopal14 ,
You can try below script include and onChange client script.
Script Include -
make sure client callable checkbox is true.
var GetCallerCountry = Class.create();
GetCallerCountry.prototype = {
initialize: function() {},
getCountryForCaller: function() {
// Fetch the caller ID from the passed parameters
var callerId = this.getParameter('sysparm_callerId');
if (!callerId) {
return ''; // Return empty if no caller ID is provided
}
// Query the user's country information
var userGR = new GlideRecord('sys_user');
if (userGR.get(callerId)) {
// Assuming `country` field exists on the user table
var countryField = userGR.getValue('country');
if (countryField) {
var countries = countryField.split(','); // Split for multiple values
if (countries.length === 1) {
return countries[0]; // Return the single country
}
}
}
return ''; // Return empty if no country or more than one country
},
type: 'GetCallerCountry'
};
OnChange Client script -
when caller <adjust as per your requirement> changes.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var callerId = g_form.getValue('caller_id');
if (!callerId) {
g_form.setValue('country', '');
return;
}
// Call the Script Include to fetch the single country or empty value
var ga = new GlideAjax('GetCallerCountry');
ga.addParam('sysparm_name', 'getCountryForCaller');
ga.addParam('sysparm_callerId', callerId); // Pass caller_id
ga.getXMLAnswer(function(response) {
var country = response; // Response will be the country name or an empty string
g_form.setValue('country', country);
});
}
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 01:43 AM
Hi @Ashish Parab ,
Below is my script include: