How to issue FetchXML in ONLOAD
- Not sure why it does not work it receives no message
var
buXml = GetMyFetch();
alert(buXml.xml);
if
(buXml != null) {
var buNodes = buXml.selectNodes("//BusinessEntity/q1:statecode"); // CRM 4.0
var icnt = 0;
icnt = buNodes.length;
if (buNodes != null) {
if (document.getElementById('navcontacts') != null && icnt > 0) {
document.getElementById(
'navcontacts').getElementsByTagName('NOBR')[0].innerText = document.getElementById('navcontacts').getElementsByTagName('NOBR')[0].innerText + " (" + icnt + ")";
}
}
}
function GetMyFetch() {
var authenticationHeader = GenerateAuthenticationHeader();
var xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
authenticationHeader +
"<soap:Body>" +
"<fetch mapping='logical'>"+
"<entity name='mdv_notetest'>"+
"<attribute name='mdv_name'/>"+
"<attribute name='mdv_notetestid'/>"+
"</entity>"+
"</fetch>"+
"</soap:Body>" +
"</soap:Envelope>";
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
var resultXml = xHReq.responseXML;
prompt('Code', xml)
return (resultXml);
}
Answers
- Hi, Bill.
You code is definitely wrong.
Errors you have:
1. After soap:Body tag your code must contain something like
"<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<fetchXml>...
instead your code.
2. You have to encode your fetch request like
"<fetch mapping='logical'>"+
"<entity name='mdv_notetest'>"+
"<attribute name='mdv_name'/>"+
"<attribute name='mdv_notetestid'/>"+
"</entity>"+
"</fetch>"+
instead your code.
3. When you parse fetch - you can't use code like
var buNodes = buXml.selectNodes("//BusinessEntity/q1:statecode");
Try toy use following code (I haven't tested it so you can):
var results = GetMyFetch(); if (results != null) { var icnt = results.length; if (document.getElementById('navcontacts') != null && icnt > 0) { document.getElementById('navcontacts').getElementsByTagName('NOBR')[0].innerText = document.getElementById('navcontacts').getElementsByTagName('NOBR')[0].innerText + " (" + icnt + ")"; } } function GetMyFetch() { var authenticationHeader = GenerateAuthenticationHeader(); var xml = "<?xml version='1.0' encoding='utf-8'?>" + "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" + " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" + authenticationHeader + "<soap:Body>" + "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ "<fetchXml><fetch mapping='logical'>"+ "<entity name='mdv_notetest'>"+ "<attribute name='mdv_name'/>"+ "<attribute name='mdv_notetestid'/>"+ "</entity>"+ "</fetch></fetchXml>"+ "</Fetch>"+ "</soap:Body>" + "</soap:Envelope>"; var xHReq = new ActiveXObject("Msxml2.XMLHTTP"); xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch"); xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xHReq.setRequestHeader("Content-Length", xml.length); xHReq.send(xml); var resultXml = xHReq.responseXML; var resultSet = resultXml.text; resultSet.replace('<','<'); resultSet.replace('>','>'); var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM"); oXmlDoc.async = false; oXmlDoc.loadXML(resultSet); var results = oXmlDoc.getElementsByTagName('result'); return results; }
And here is the sample how to work with Fetch requests.
Truth is opened the prepared mind My blog - http://a33ik.blogspot.com- Marked As Answer byBILL_TETRAULT Wednesday, November 04, 2009 7:50 PM
All Replies
- Hi, Bill.
You code is definitely wrong.
Errors you have:
1. After soap:Body tag your code must contain something like
"<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<fetchXml>...
instead your code.
2. You have to encode your fetch request like
"<fetch mapping='logical'>"+
"<entity name='mdv_notetest'>"+
"<attribute name='mdv_name'/>"+
"<attribute name='mdv_notetestid'/>"+
"</entity>"+
"</fetch>"+
instead your code.
3. When you parse fetch - you can't use code like
var buNodes = buXml.selectNodes("//BusinessEntity/q1:statecode");
Try toy use following code (I haven't tested it so you can):
var results = GetMyFetch(); if (results != null) { var icnt = results.length; if (document.getElementById('navcontacts') != null && icnt > 0) { document.getElementById('navcontacts').getElementsByTagName('NOBR')[0].innerText = document.getElementById('navcontacts').getElementsByTagName('NOBR')[0].innerText + " (" + icnt + ")"; } } function GetMyFetch() { var authenticationHeader = GenerateAuthenticationHeader(); var xml = "<?xml version='1.0' encoding='utf-8'?>" + "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" + " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" + authenticationHeader + "<soap:Body>" + "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ "<fetchXml><fetch mapping='logical'>"+ "<entity name='mdv_notetest'>"+ "<attribute name='mdv_name'/>"+ "<attribute name='mdv_notetestid'/>"+ "</entity>"+ "</fetch></fetchXml>"+ "</Fetch>"+ "</soap:Body>" + "</soap:Envelope>"; var xHReq = new ActiveXObject("Msxml2.XMLHTTP"); xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch"); xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xHReq.setRequestHeader("Content-Length", xml.length); xHReq.send(xml); var resultXml = xHReq.responseXML; var resultSet = resultXml.text; resultSet.replace('<','<'); resultSet.replace('>','>'); var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM"); oXmlDoc.async = false; oXmlDoc.loadXML(resultSet); var results = oXmlDoc.getElementsByTagName('result'); return results; }
And here is the sample how to work with Fetch requests.
Truth is opened the prepared mind My blog - http://a33ik.blogspot.com- Marked As Answer byBILL_TETRAULT Wednesday, November 04, 2009 7:50 PM
- Hi Bill,
As a side note, you might want to use a reusable CRM Web Service helper / toolkit to do it. I have a CRM Web Service Helper available at my blog, which will probably make your life a little bit easier.
Cheers,
Daniel Cai | http://danielcai.blogspot.com Thanks, will take a look

