function FetchCity()
{
//getting city from citizen
var City = Xrm.Page.getAttribute("new_city").getValue();
alert("alert city 1");
if ( City != null && City != "")
{
var Country;
var serverUrl = Xrm.Page.context.getClientUrl();
if (serverUrl.match(/\/$/))
{
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
alert("alert city 2");
alert(City[0].id.toString());
//query to fetch
// To be updated as required
var fetchXml = "<fetch mapping='logical' version='1.0'>"
+ "<entity name='new_city'>"
+ "<attribute name='new_cityid'/>"
+ "<attribute name='new_country'/>"
+ "<filter type='and'>"
+ "<condition attribute='new_cityid' operator='eq' value='" + City[0].id+ "'/>"
+ "</filter>"
+ "</entity>"
+ "</fetch>";
alert("city 3");
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)
{
alert("city4");
var vals = doc.getElementsByTagName("KeyValuePairOfstringanyType");
alert(vals.length);
for (var j = 0; j < vals.length; j ++ )
{
alert("city5");
if(vals[j].getElementsByTagName("key")[0].firstChild.nodeValue =="new_country")
{
alert("city 6");
Country= vals[j].getElementsByTagName("value")[0].getElementsByTagName("Id")[0].textContent;
var CountryName = vals[j].getElementsByTagName("value")[0].getElementsByTagName("Name")[0].textContent;
alert("country check 1");
var value = new Array();
value[0] = new Object();
value[0].id = Country;
value[0].name = CountryName;
value[0].entityType = "new_city";
Xrm.Page.getAttribute("new_country").setValue(value);
alert(CountryName);
}
if( ! CheckFetchedValueExistance('new_country', vals, 'key'))
{
alert("City Not Found.");
// Xrm.Page.getAttribute("new_country").setValue(null);
}
}
Xrm.Page.getAttribute("new_country").setValue(value);
alert(CountryName);
}
else
{
alert(7);
var vals = doc.getElementsByTagName("a:KeyValuePairOfstringanyType");
alert(vals.length);
for (var j = 0; j < vals.length; j ++ )
{
alert(8);
if(vals[j].getElementsByTagName("b:Key")[0].firstChild.nodeValue =="new_country")
{
alert(9);
Country= vals[j].getElementsByTagName("b:value")[0].getElementsByTagName("Id")[0].textContent;
var CountryName = vals[j].getElementsByTagName("b:value")[0].getElementsByTagName("Name")[0].textContent;
alert("country check 2");
var value = new Array();
value[0] = new Object();
value[0].id = Country;
value[0].name = CountryName;
value[0].entityType = "new_city";
Xrm.Page.getAttribute("new_country").setValue(value);
alert(CountryName);
}
if( ! CheckFetchedValueExistance('new_country', vals, 'b:key'))
{
alert("City Not Found.");
}
}
}
Xrm.Page.getAttribute("new_country").setValue(value);
}
}
// function to check if field contains value
function CheckFetchedValueExistance(names, vals, key)
{
for (var j = 0; j < vals.length; j ++ )
{
if(vals[j].getElementsByTagName(key)[0].firstChild.nodeValue == names)
{
return true;
}
}
return false;
}
*This post is locked for comments