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

nameisnani
Mega Sage

Hi @Nani18 

 

Please write a OnLoad CS . 

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;
    }
  }
}

 

Please mark the answer as correct solution and helpful if helped.

 

Hello @nameisnani 

 

Thanks for your help.

 

I tried with Business rule it is working only on insert or update .. I am new to scripting , is there any possibility to change this script to onload client script with the same fields..?

(function executeRule(current, previous /*null when async*/ ) {
 
    var Ltype = current.cmn_location_type.getDisplayValue();
 
    if (Ltype == 'Country') {
        current.country = current.name;
    }
    var pn = current.parent;
    var lcn = new GlideRecord('cmn_location');
    lcn.addQuery('sys_id', pn);
 
    lcn.addQuery('cmn_location_type', 'Country');
 
    lcn.query();
    if (lcn.next()) {
        current.country = pn.country;
 
 
    }
 
 
 
})(current, previous);

Hi @Nani18 

 

I have already given a Onload Client script . Please check . 

 

Please mark the answer as correct solution and helpful if helped.

Hello @nameisnani 

 

When i am trying to save client script an error message is showing

Nani18_0-1691081277655.png