Hey, can anyone help me find the error ?
I want the "country code" entity to be fetched from "country" field automatically
entity name " citizen"
and there is another entity "country" that is a lookup field in "citizen" includes 2 fields: name and country code.
here is my code :
// ----- Start OF Script : Fetch country code on change of country------ //
function Fetchcountrycode()
{
var country = Xrm.Page.getAttribute("Country").getValue();
if (country!= null && country != "")
{
var countrycode;
var serverUrl = Xrm.Page.context.getClientUrl();
if (serverUrl.match(/\/$/))
{
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
// To be updated as required
var fetchXml = "<fetch mapping='logical' version='1.0'>"
+ "<entity name='Citizen'>"
+ "<attribute name='Country'/>"
+ "<attribute name='new_countrycode'/>"
+ "<filter type='and'>"
+ "<condition attribute='Country' operator='eq' value='" + 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_countrycode")
{
countrycode = vals[j].getElementsByTagName("value")[0].textContent;
}
if( ! CheckFetchedValueExistance('new_countrycode', vals, 'key'))
{
alert("country mode 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_countrycode")
{
countrycode = vals[j].getElementsByTagName("b:value")[0].textContent;
}
if( ! CheckFetchedValueExistance('new_countrycode', vals, 'b:key'))
{
alert("country mode Not Found.");
}
}
}
if (countrycode == "true")
{
Xrm.Page.getAttribute("new_countrycode").setValue(1);
}
else if (countrycode == "false")
{
Xrm.Page.getAttribute("new_countrycode").setValue(0);
}
else
{
Xrm.Page.getAttribute("new_countrycode").setValue(null);
}
}
else
{
Xrm.Page.getAttribute("new_countrycode").setValue(null);
}
}
// ----- End OF Script : Fetch country code on change of country ------ //
// ---------------------------------------------------------------------------------------------------------------------------- //
Thanks