We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to populate field value from when one field is filled

Mehar Naaz
Tera Contributor

I have a requirement where 'Onsite Facilty ' is reference field and 'Onsite Facility Location' is read only fields.

'Onsite Facility ' refer to 'u_location' table. When 'Onsite Facility' field is filled with 'location name' from 'u_location' table then 'Onsite Facility Location' should auto-populate 'Location code'  from 'u_location' table.

Eg: Bengaluru- East Campus  is Onsite Facility  then Onsite Facility Location would be Bengaluru.

 

MeharNaaz_0-1706102736039.png

 

3 REPLIES 3

JoeyOV1
Tera Contributor

New to this so bear with me -- I had something similar where they wanted affected user and affected user location on the incident form. 

I just simply created a business rule that when affected user changes -- the action would set affected user location to affected user.location. 

Anand Kumar P
Tera Patron

Hi @Mehar Naaz ,

Try with below script in reference qualifier

javascript: "onsitefacility_location_backendname="+current.onsitefacilitybackend_name.locationcode_backendname;

 If above one not working then go with onchange client script on onsitefacility field and script include

//Onchangeclientscript:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
			if (isLoading || newValue === '') {
				return;
			}
			var ga = new GlideAjax('getlocationdetails');
			ga.addParam('sysparm_name', 'getregion');
			ga.addParam('sysparm_location', g_form.getValue('onsite_facility_backendname'));
			ga.getXML(RegionCountryStateCityParse);

			function RegionCountryStateCityParse(response) {
				var answerJSON = response.responseXML.documentElement.getAttribute("answer");
				var answer = JSON.parse(answerJSON);
				g_form.setValue('u_onsitefacility_location_backendname', answer.region.toString());
			}
		}
 
//Script Include:
var getlocationdetails = Class.create();
getlocationdetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
			getregion: function() {
				var location = this.getParameter("sysparm_location");
				var usrobj = {};
				var grlocation = new GlideRecord('cmn_location');//give your location table backend name
				grlocation.get(location);
				usrobj.region = grlocation.location_code_backendname.toString();			
				return JSON.stringify(usrobj);
			},
            type: 'getlocationdetails'
};

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

Shoheb_IbaaBoss
Tera Guru

Hi, 

Try below BR:

(function executeRule(current, previous /*null when async*/) {
if (current.u_onsite_facility.changes()) {
var locationRecord = new GlideRecord('u_location');
if (locationRecord.get(current.u_onsite_facility)) {
current.u_onsite_facility_location = locationRecord.u_location_code;
}
}
})(current, previous);

Regards,

Shoheb