- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2019 09:10 PM
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.
Thanks.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2019 05:09 AM
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
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2019 04:39 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2019 04:46 AM
Hi
In client script i dont see you setting values to your form. user g_form.setValue('your_value',ans.street);//example
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2019 02:51 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2019 05:09 AM
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
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2019 02:51 AM
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.