- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2023 01:08 AM
I want to make a business rule in sys_user table
so in user table there is a field named city
so when I select city country code automatically set
and this country code data fetch from cmn_location table
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2023 08:45 AM
Hi @Affanahmadd
if (current.city.changes()) {
// Logic to set country code
}
var city = current.city.toString(); // Get the selected city
var locationGR = new GlideRecord('cmn_location');
locationGR.addQuery('city', city);
locationGR.query();
if (locationGR.next()) {
current.country_code = locationGR.country_code; // Set the country code
}
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2023 08:45 AM
Hi @Affanahmadd
if (current.city.changes()) {
// Logic to set country code
}
var city = current.city.toString(); // Get the selected city
var locationGR = new GlideRecord('cmn_location');
locationGR.addQuery('city', city);
locationGR.query();
if (locationGR.next()) {
current.country_code = locationGR.country_code; // Set the country code
}
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2023 09:45 AM
@Affanahmadd Would you like to save the country code automatically at the time of saving the record in database or would you like to populate the country field on the form when the city field is selected. If former is true then you need a business rule and if the later is true then you need a Client script + Script Include to achieve the same.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2023 09:56 AM - edited 12-23-2023 03:23 AM
Hello @Affanahmadd
If you want to business rule only then you can refer the below code and modify further as per your req.
Business Rule:
(function executeRule(current, previous /*null when async*/) {
// Check if the city field has changed
if (current.city.changes()) {
// Get the country code from the cmn_location table based on the city
var locationGR = new GlideRecord('cmn_location');
if (locationGR.get('city', current.city)) {
// Set the country code on the user record
current.country_code = locationGR.country;
}
}
})(current, previous);
But if you want to implement the functionality as soon as you change the city then I would suggest to use the onChange client script and script include, Please see the below code for your reference:
Script Include:
var LocationUtils = Class.create();
LocationUtils.prototype = {
initialize: function() {},
getCountryCodeByCity: function(city) {
var country = '';
var locationGR = new GlideRecord('cmn_location');
if (locationGR.get('city', city)) {
country = locationGR.country;
}
return country;
},
type: 'LocationUtils'
};
Client Script:
function onChangeCity(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Get the current value of the city field
var city = g_form.getValue('city');
// Call the script include to get the country code based on the city
var locationUtils = new LocationUtils();
var countryCode = locationUtils.getCountryCodeByCity(city);
// Set the country code field on the form
g_form.setValue('country_code', countryCode);
}
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Regards,
Aniket