This sample would link to Client Address Zip Code field (onchange event) to find the State that corresponds to the zip code entered.
function Address1_PostalCode_OnChange() {
var xmlHttp;
function SoapCall() {
// Get zip code from screen field
var oField = event.srcElement;
var zipCode = "";
if (typeof (oField) != "undefined" && oField != null) {
zipCode = oField.value;
}
// creatng the xmlHttp object
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
// Calling the web service using post and true means asynchronous call
xmlHttp.open("post", "http://mysite.com/ZipCountyWS/ZipCountyWS.asmx", true);
// Setting the request header to let the web service identify the soap request we would be sending
xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
// http://mynamespace.com/GetStateAndCountyByZipCode – name of the webmethod
//[WebService(Namespace = "http://mynamespace.com/")] which we had applied to our web service class
xmlHttp.setRequestHeader("SOAPAction", "http://mynamespace.com/GetStateAndCountyByZipCode");
xmlHttp.onreadystatechange = doUpdate;
// setting the soap request body
var soapRequest = "<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
" xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +
"<soap:Body>" +
"<GetStateAndCountyByZipCode xmlns='http://mynamespace.com/'>" +
"<ZipCode>" + zipCode + "</ZipCode> " +
"</GetStateAndCountyByZipCode>" +
" </soap:Body>" +
"</soap:Envelope>";
// send the request
xmlHttp.send(soapRequest);
return false;
}
function doUpdate() {
debugger;
if (xmlHttp.readyState == 4) {
var responseXMLResult = xmlHttp.responseXML;
var result = responseXMLResult.lastChild.nodeTypedValue;
crmForm.address1_stateorprovince.value = '';
crmForm.address1_county.value = '';
if (result.indexOf('UNKNOWN') == -1) {
crmForm.address1_country.value = 'USA';
}
else {
alert('Please confirm zip code: '
+ crmForm.address1_postalcode.value
+ ' is valid, no county record was found.'
+ '\r'
+ 'If this is a valid zip code please alert the system administrator.'
);
}
crmForm.address1_stateorprovince.value = result.split(':')[0];
crmForm.address1_county.value = result.split(':')[1];
}
}
SoapCall();
}