Autopopulate country in child locations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2023 09:16 AM
Hello Experts,
In locations(cmn_location) table if location type(dropdown) = country then i want to populate name(string) value into Country(string)field then at the same time I want to update Country field in child records as well.
Child records means state ,city,site,etc....
How to achive this by using onload client script.
Thanks in advance,
Best Regards,
Nani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2023 09:52 AM
@Nani18 please check
function onload() { // Get the current location record. var location = cmn_location.getCurrentLocation(); // Check if the location type is country. if (location.location_type == "Country") { // Get the country name. var countryName = location.name; // Update the country field in the child records. var childRecords = cmn_location.getRelatedRecords("cmn_location", "child_locations"); for (var i = 0; i < childRecords.length; i++) { childRecords[i].country = countryName; } } }
The onload function is a JavaScript function that is executed when the page is loaded. The function takes no arguments and does not return any value.
The cmn_location object is a ServiceNow object that provides access to location data. The getCurrentLocation() method returns the current location record.
The location_type field of the location record specifies the type of location. The Country value indicates that the location is a country.
The getRelatedRecords() method returns a list of related records. The child_locations related record is a list of child locations.
The country field of the location record specifies the country of the location.
The for loop iterates through the list of child locations and updates the country field of each child location with the value of the countryName variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2023 10:05 AM
Hello @nameisnani
Thanks
I saved client script.
But above script is not working for me.
Please refer the attached screenshot , In the child locations country is still empty it is not copying name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2023 10:12 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2023 10:17 AM
Hello @nameisnani
I will appreciate your patience
I changed UI type to all but no luck.
Please test it from your end once. That will helpful for me.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2023 10:11 AM