Hey can anyone help me find my error ?
// ----- Start OF Script : Fetching country code on change of country ------ //
function FetchCountryCode()
{
//getting country from citizen
var Country = Xrm.Page.getAttribute("new_country").getValue();
if (Country != null && Country != "")
{
var AreaCode;
var serverUrl = Xrm.Page.context.getClientUrl();
if (serverUrl.match(/\/$/))
{
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
//query to fetch
// To be updated as required
var fetchXml = "<fetch mapping='logical' version='1.0'>"
+ "<entity name='new_country'>"
+ "<attribute name='new_countryid'/>"
+ "<attribute name='new_areacode'/>"
+ "<filter type='and'>"
+ "<condition attribute='new_name' operator='eq' value='" + new_country[0].id + "'/>"
+ "</filter>"
+ "</entity>"
+ "</fetch>";
var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soapenv:Envelope xmlns:soapenv=\"schemas.xmlsoap.org/.../envelope\">" +
"<soapenv:Body>" +
"<RetrieveMultiple xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">" +
"<query i:type=\"a:FetchExpression\" xmlns:a=\"schemas.microsoft.com/.../Contracts\">" +
"<a:Query>" + fetchXml.replace(/\&/g, '&' + 'amp;').replace(/</g, '&' + 'lt;').replace(/>/g, '&' + 'gt;').replace(/\'/g, '&' + 'apos;').replace(/\"/g, '&' + 'quot;') + "</a:Query>" +
"</query>" +
"</RetrieveMultiple>" +
"</soapenv:Body>" +
"</soapenv:Envelope>";
var xmlHttpRequest;
var doc;
var result;
if (window.XMLHttpRequest)
{
// code for IE7, IE8, IE9 , IE10 , Firefox, Chrome, Opera, Safari
xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("POST", serverUrl + "/XRMServices/2011/Organization.svc/web", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
result = xmlHttpRequest.responseXML.xml;
}
else
{
// code for IE6, IE5
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
result = xmlHttpRequest.responseXML.xml;
}
if (window.DOMParser)
{
parser = new DOMParser();
doc = parser.parseFromString(xmlHttpRequest.responseText, "text/xml");
}
// Internet Explorer
else
{
doc = new ActiveXObject("MSXML2.DOMDocument");
doc.async = false;
doc.loadXML(result);
}
if (navigator.userAgent.toLowerCase().indexOf("chrome") > - 1)
{
var vals = doc.getElementsByTagName("KeyValuePairOfstringanyType");
for (var j = 0; j < vals.length; j ++ )
{
if(vals[j].getElementsByTagName("key")[0].firstChild.nodeValue == "new_areacode")
{
AreaCode = vals[j].getElementsByTagName("value")[0].textContent;
}
if( ! CheckFetchedValueExistance('new_areacode', vals, 'key'))
{
alert("Country Not Found.");
}
}
}
else
{
var vals = doc.getElementsByTagName("a:KeyValuePairOfstringanyType");
for (var j = 0; j < vals.length; j ++ )
{
if(vals[j].getElementsByTagName("b:key")[0].firstChild.nodeValue == "new_areacode")
{
AreaCode = vals[j].getElementsByTagName("b:value")[0].textContent;
}
if( ! CheckFetchedValueExistance('new_areacode', vals, 'b:key'))
{
alert("Country Not Found.");
}
}
}
//set fetched country code in country code on citizen
Xrm.Page.setAttribute("new_countrycode").setValue();
}
// ----- End OF Script : Fetch Country code on change of Country------ //
// ---------------------------------------------------------------------------------------------------------------------------- //