how would you populate city and state fields on a form just by entering the zip code?

helenscheck
Kilo Contributor

I would like to auto populate the City and State field just by entering the zip code.   Any suggestions on how to accomplish that?

1 ACCEPTED SOLUTION

BALAJI40
Mega Sage

script include:


var MyFavoritesAjax = Class.create();


MyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getFavorites : function() {



  var id=this.getParameter('sysparm_zipcode');// to get parameter from client side


  gs.log('callidis'+id);// it will show log message in LOGS APPLICATION


  var inc=new GlideRecord('cmn_location');


  inc.addQuery('zip',id);


  inc.query();


  if(inc.next()){


  this._addFavorite("state", inc.email);


  this._addFavorite("city", inc.mobile_phone);


  }


  },


  _addFavorite : function(name, value) {


  var favs = this.newItem("favorite");


  favs.setAttribute("name", name);


  favs.setAttribute("value", value);


  },



  type : "MyFavoritesAjax"



});



client script:


write a onchange script on zipcode field on the form,


//please change field names with your form fields


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading )


  return;


  if (newValue == '') {


  g_form.setValue('u_state', '');


  g_form.setValue('u_city', '');


  return;



  }


  var ga = new GlideAjax("MyFavoritesAjax");


  ga.addParam("sysparm_name", "getFavorites");


  ga.addParam("sysparm_caller", g_form.getValue('u_zipcode'));



  ga.getXML(ajaxResponse);



  function ajaxResponse(serverResponse) {


  var favorites = serverResponse.responseXML.getElementsByTagName("favorite");


  for(var i = 0; i < favorites.length; i++) {


  var name = favorites[i].getAttribute("name");


  var value = favorites[i].getAttribute("value");


  if(name=='state'){


  g_form.setValue('u_state',value);


  }


  if(name=='city'){


  g_form.setValue('u_city',value);


  }


  }


  }


}


View solution in original post

1 REPLY 1

BALAJI40
Mega Sage

script include:


var MyFavoritesAjax = Class.create();


MyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getFavorites : function() {



  var id=this.getParameter('sysparm_zipcode');// to get parameter from client side


  gs.log('callidis'+id);// it will show log message in LOGS APPLICATION


  var inc=new GlideRecord('cmn_location');


  inc.addQuery('zip',id);


  inc.query();


  if(inc.next()){


  this._addFavorite("state", inc.email);


  this._addFavorite("city", inc.mobile_phone);


  }


  },


  _addFavorite : function(name, value) {


  var favs = this.newItem("favorite");


  favs.setAttribute("name", name);


  favs.setAttribute("value", value);


  },



  type : "MyFavoritesAjax"



});



client script:


write a onchange script on zipcode field on the form,


//please change field names with your form fields


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading )


  return;


  if (newValue == '') {


  g_form.setValue('u_state', '');


  g_form.setValue('u_city', '');


  return;



  }


  var ga = new GlideAjax("MyFavoritesAjax");


  ga.addParam("sysparm_name", "getFavorites");


  ga.addParam("sysparm_caller", g_form.getValue('u_zipcode'));



  ga.getXML(ajaxResponse);



  function ajaxResponse(serverResponse) {


  var favorites = serverResponse.responseXML.getElementsByTagName("favorite");


  for(var i = 0; i < favorites.length; i++) {


  var name = favorites[i].getAttribute("name");


  var value = favorites[i].getAttribute("value");


  if(name=='state'){


  g_form.setValue('u_state',value);


  }


  if(name=='city'){


  g_form.setValue('u_city',value);


  }


  }


  }


}