Autopopulate country in child locations

Nani18
Tera Contributor

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....

Nani18_0-1691079240910.png

 

 

How to achive this by using onload client script.

 

 

Thanks in advance,

 

Best Regards,

Nani

9 REPLIES 9

@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.

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.

Nani18_0-1691082289973.png

 

 

HI @Nani18 

 

In your client script 

 

Change UI Type to All instead of Desktop and then try .

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.

HI @Nani18 

 

Change UI Type to All instead of Desktop and then try .