web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Fetching a field in JavaScript

(0) ShareShare
ReportReport
Posted on by

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------ //

// ---------------------------------------------------------------------------------------------------------------------------- //

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Swetha Poojary Salaskar Profile Picture
    326 on at

    in your fetchxml you provided filter parameter variable name which is wrong. I believe you have to use Country instead of  new_country[0]

    your code:

    + "<condition attribute='new_name' operator='eq' value='" + new_country[0].id + "'/>"

    You should use

    + "<condition attribute='new_name' operator='eq' value='" + Country[0].id + "'/>"

    you can also refer this link

    arunpotti.wordpress.com/.../get-lookup-id-text-entityname-in-crm-using-javascript

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    What is the CRM version ?

    If you want you can do it with CRM web api.

    Best Regards,

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans