- 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-10-2019 09:16 PM
Hello Sana,
You could configure your form layout and directly add these fields on form by dot walking on the Location field. Try it once and then you wont need any scripts to do it.
Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2019 09:20 PM
Hi Sana,
You can achieve this without using script.
- R.click on Form header > configure > Form Layout
- find Location[+] field which is a reference field, click on it and then click on the Expand selected reference field, which will show the fields such as, country, street etc, in your available box.
- Pull them in your Selected box.
Now, when you select the location field, it will automatically fill other location related fields on the form.
Mark If Correct/Helpful.
Regards,
Ajay

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2019 10:09 PM
Hi
The solutions provided by Ali and Ajay are correct, But here again it is not recommended to get the dot walked fields on the table as it may cause you problems.
Going ahead i can see you have created your own fields for the purpose. I suggest you to write a Script include or getReference method which will get the details from the location table to your table.
Write an onChange client script and send the selected location :-
Something like this :-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax("GetLocationDetails");
ga.addParam("sysparm_name",'locDet');
ga.addParam("sysparm_loc_det",g_form.getValue("your_loc_field"));
ga.getXML(testLoc);
function testLoc(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
//Now this answer has youur values.
}
}
In client callable Script include :-
var GetLocationDetails = Class.create();
GetLocationDetails .prototype = Object.extendsObject(AbstractAjaxProcessor, {
locDet: function(){
var arr = {};
var loc = this.getParameter("sysparm_loc_det");
var gr = new GlideRecord("cmn_location");
gr.addQuery("sys_id",loc);
gr .query();
if(gr .next()){
}
return JSON.stringify(arr);
var obj={};
obj.street ='';
obj.city ='';
obj.state ='';
obj.zip ='';
obj.country ='';
var id=this.getParameter('sysparm_loc_det');
var gr= new GlideRecord('cmn_location');
if(gr.get(id)){
obj.street = gr.street.toString();
obj.city = gr.location.toString();
obj.state = gr.state.toString();
obj.zip = gr.zip.toString();
obj.country = gr.country.toString();
}
return JSON.stringify(obj);
},
type: 'GetLocationDetails'
});
Regards,
Omkar Mone.
www.dxsherpa.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2019 04:25 AM
Hi Omkar,
Thank you all.
I wrote below script include and client script.But no Luck.
Script Include:
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();
gs.addInfoMessage('looping 2');
var obj={};
obj.street ='';
obj.city ='';
obj.state ='';
obj.zip ='';
obj.country ='';
if(gr.next()){
obj.street=gr.name;
obj.city=gr.parent.name;
obj.state=gr.parent.parent.name;
obj.country=gr.parent.parent.parent.name;
gs.addInfoMessage(obj.street+obj.state);
}
result.setAttribute("street",obj.street);
//return JSON.stringify(arr);
gs.addInfoMessage('looping 3');
// var id=this.getParameter('sysparm_loc_det');
// var gr= new GlideRecord('cmn_location');
// if(gr.get(id)){
// gs.addInfoMessage('looping 4');
// obj.street = gr.u_area_street.toString();
// obj.city = gr.u_building_room.toString();
// obj.state = gr.u_state.toString();
// obj.zip = gr.u_city.toString();
// obj.country = gr.u_country.toString();
// }
return JSON.stringify(obj);
},
type:'GetLocationDetails'
});
Client Scrpit:
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 ans = response.responseXML.documentElement.getAttribute("answer");
alert('test'+ans);
//Now this answer has youur values.
}
}
I'm not able to set the value is the respective fields.
Please help me with this.
Thank you.