The CreatorCon Call for Content is officially open! Get started here.

How do I auto populate address based on the location field selection?

Sana10
Kilo Contributor

Hi Community,

 

How do I auto populate address based on the location field selection?

I have custom fields on hardware asset tables like; country,state,city,area and street all read-only and all referencing to cmn_location table. On Location I have set tree_picker=true. I'm in the process of auto populate respective values based on the location.

I guess it can be achieve from script include . Any code would be helpful.

find_real_file.png

Thanks.

1 ACCEPTED SOLUTION

Shweta KHAJAPUR
Tera Guru

Try below code,

var GetLocationDetails = Class.create();
GetLocationDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
locDet: function(){


var arr = {};
 gs.addInfoMessage('looping 1');
var loc = this.getParameter("sysparm_loc_det");
var gr = new GlideRecord("cmn_location");
 gr.addQuery("sys_id",loc);
 gr .query();
 if(gr.next()){
var obj={
"street" : gr.name.toString(),
"city" : gr.parent.toString(),
"state" : gr.parent.parent.toString(),
"country" :gr.parent.parent.parent.toString()

};

var data = new JSON().encode(obj);
return data;
}, 
 type:'GetLocationDetails'
});

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 if (isLoading || newValue === '') {
 return;
 }
 //var location=g_form.getReference('location');
 
 //alert(location.parent.getDisplayName());
 
 var ga = new GlideAjax("GetLocationDetails");
 ga.addParam("sysparm_name",'locDet');
 ga.addParam("sysparm_loc_det",g_form.getValue("location"));
 ga.getXML(testLoc);


function testLoc(response)
 {
 var answer =  response.responseXML.documentElement.getAttribute("answer"); 
var returneddata = JSON.parse(answer);  
 alert('test'+returneddata.street);
alert(returneddata.city);//sys_id will get generated
alert(returneddata.state);//sys_id will get generated
alert(returneddata.country);//sys_id will get generated
 
 }
}

View solution in original post

10 REPLIES 10

Hi

var ans = response.responseXML.documentElement.getAttribute("answer");

you are using attribute is "street"

replace the answer with street

 

or

 

return the array "obj"  in script include 

 

 

 

Hi 

In client script i dont see you setting values to your form. user g_form.setValue('your_value',ans.street);//example

 

Regards.

Hi,

 

I tried with you code but no luck again, 'returneddata' variable is empty. Data is present in obj variable at script include side but when passing to client script 'returneddata variable is not getting that data.

Kindly help me on this.

 

Thanks.

Shweta KHAJAPUR
Tera Guru

Try below code,

var GetLocationDetails = Class.create();
GetLocationDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
locDet: function(){


var arr = {};
 gs.addInfoMessage('looping 1');
var loc = this.getParameter("sysparm_loc_det");
var gr = new GlideRecord("cmn_location");
 gr.addQuery("sys_id",loc);
 gr .query();
 if(gr.next()){
var obj={
"street" : gr.name.toString(),
"city" : gr.parent.toString(),
"state" : gr.parent.parent.toString(),
"country" :gr.parent.parent.parent.toString()

};

var data = new JSON().encode(obj);
return data;
}, 
 type:'GetLocationDetails'
});

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 if (isLoading || newValue === '') {
 return;
 }
 //var location=g_form.getReference('location');
 
 //alert(location.parent.getDisplayName());
 
 var ga = new GlideAjax("GetLocationDetails");
 ga.addParam("sysparm_name",'locDet');
 ga.addParam("sysparm_loc_det",g_form.getValue("location"));
 ga.getXML(testLoc);


function testLoc(response)
 {
 var answer =  response.responseXML.documentElement.getAttribute("answer"); 
var returneddata = JSON.parse(answer);  
 alert('test'+returneddata.street);
alert(returneddata.city);//sys_id will get generated
alert(returneddata.state);//sys_id will get generated
alert(returneddata.country);//sys_id will get generated
 
 }
}

Hi Shweta,

 

I tried with you code but no luck again, 'returneddata' variable is empty. Data is present in obj variable at script include side but when passing to client script 'returneddata variable is not getting that data.

Kindly help me on this.

 

Thanks.