Hi Team,
We are getting the Automated address suggestions based on country & search term.
for this we have a catalog to enter search term & get User Country and show the response as address options to choose.
But address will be provided in 2 steps.
getting the responswestatus = 200, but script is giving error/null object.
Step 1: URL > https://international-autocomplete.api.smarty.com/v2/lookup
Send Country & Search term as HTTP Parameters & get the rest response1. I'm getting this without any issue.
Step 2: Response will be having partial addresses & Address IDs. Add them as options to a variable.
Send this to Clients to choose from options.
Step 3 : After choosing the address ID in above step, send that id to URL as below in REST Msg:
https://international-autocomplete.api.smarty.com/v2/lookup/${address_id}
The Response 2 should be mapped to respective address variables.
I'm using client script & Script include. But getting the console error as null object & Glideajax error.
Client script:
var int_street_1 = g_form.getValue('international_street'); // Search term
var country_1 = g_form.getValue('country'); //country
var id = g_form.getValue('int_shipping_address'); //address id
var slice = id.split(','); // Splitting the Shipping Address.
var n= slice.length; //length of returned slices.
var add_id = slice[n-1]; //get the address id only
alert(add_id);
alert(country_1);
alert(int_street_1);
var gb = new GlideAjax('SmartyIntegration'); //Script Include
gb.addParam('sysparm_name', 'getInternational_2'); //Method
gb.addParam('sysparm_street2',int_street_1);
gb.addParam('sysparm_country',country_1);
gb.addParam('sysparm_id',add_id);
gb.getXML(ajaxResponse);
function ajaxResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
var obj = JSON.parse(answer); // Parsing
alert(obj); //getting 'null'
var addressArr1 = obj.candidates;
if(obj.candidates.length != 0){
for (var i=0;i<addressArr1.length;i++) {
//alert('for loop');
var ad2 = addressArr1[i].street;
var ct2 = addressArr1[i].locality;
var sp2 = addressArr1[i].administrative_area;
var zip2 = addressArr1[i].postal_code;
alert(zip2);
g_form.setValue('address_1', ad2);
g_form.setValue('city', ct2);
g_form.setValue('state_province', sp2);
g_form.setValue('zip_postal_code', zip2);
} } }
Script Include:
getInternational_2: function() {
gs.log('inside smarty');
var street3 = this.getParameter('sysparm_street2');
var int_country3 = this.getParameter('sysparm_country');
var address_id3 = this.getParameter('sysparm_id');
gs.log('id'+ address_id3);
gs.log('country'+int_country3);
try {
gs.log('inside try');
var r3 = new sn_ws.RESTMessageV2('smaty_test', 'Int complete address LookUp'); //Rest MSG & Method
r3.setStringParameterNoEscape('id', address_id3);
r3.setStringParameterNoEscape('street', street3);
r3.setStringParameterNoEscape('country', int_country3);
//gs.log('response call test');
var response3 = r3.execute();
gs.log("This is topic token" + response3);
var responseBody3 = response3.getBody();
var httpStatus3 = response3.getStatusCode();
gs.log('http'+ httpStatus3);
if(httpStatus3 == 200){
gs.log("smarty success" + responseBody3);
}
//var parsed = JSON.parse(responseBody3);
//var token = responseBody.toString().split('credentials token="')[1].split('" estimated')[0];
} catch (ex) {
gs.log('inside catch');
var message3 = ex.getMessage();
gs.error(message3); }
return responseBody3;
},
type: 'SmartyIntegration'