Answered by:
assigning results of fetch xml....how to ??

Question
-
Hi all,
I have the following fetch xml query:
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'>" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
"<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
" <fetchXml>" +
" <fetch mapping='logical'>" +
" <entity name='account'>" +
" <attribute name='name'/>" +
" <filter type='and'>" +
" <condition attribute='accountnumber' operator='eq' value='something'/>" +
" </filter>" +
" </entity>" +
" </fetch>" +
" </fetchXml>" +
" </Fetch>" +
" </soap:Body>" +
"</soap:Envelope>";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
alert(resultXml.xml);
now i need to assign the result of this query to fields
note that i'm sure it will display one record
any help will be appreciated
thanks in advance :)
Thursday, September 30, 2010 10:07 PM
Answers
-
Here is the code example for assigning to a CRM Attribute on the form
var result = xmlhttp.responseXML.xml;
// Create a new DOM document and load the response XML
var doc = new ActiveXObject("MSXML2.DOMDocument");
doc.async = false;
doc.loadXML(result);// Return the q1:telephone1 node
var telephone1Node = doc.selectSingleNode("//q1:telephone1");
if( telephone1Node != null )
{
// If the node exists, return its value
crmForm.all.<Attributename.DataValue> = telephone1Node.text;
}
else
{
return null;
}
Hope this helps. Amar- Marked as answer by Mostafa Moatassem Friday, October 1, 2010 9:31 PM
Friday, October 1, 2010 9:16 AM
All replies
-
here you will find an example to learn from http://blog.expertsoftware.co.uk/post/2009/03/19/Retrieving-All-Records-with-Fetch-XML.aspx
MSCRM Bing'd - http://bingsoft.wordpress.com
Check out the CRM 4 to CRM 2011 JavaScript Converter ToolThursday, September 30, 2010 10:12 PMModerator -
All you need to use is nodeTypedValue to get the value.
Please find an example here
resultXml.selectSingleNode('//description').nodeTypedValue;
// Capture the result. var resultXml = xHReq.responseXML; // Check for errors. var errorCount = resultXml.selectNodes('//error').length; if (errorCount != 0) { var msg = resultXml.selectSingleNode('//description').nodeTypedValue; alert(msg); } // Display the retrieved value. else { alert(resultXml.selectSingleNode("//q1:fullname").nodeTypedValue); }
More here http://crmentropy.blogspot.com/2009_11_01_archive.html
Hope this helps. Amar- Proposed as answer by Amarsen Vangoor Thursday, September 30, 2010 10:23 PM
Thursday, September 30, 2010 10:18 PM -
Dear Amarsen,
Thanks for your effort, but unfortunately the following error ocurred:
There was an error with this field's customized event.
Field:window
Event:onload
Error:Object required
Any suggestions ??
thanks in advance
Thursday, September 30, 2010 11:20 PM -
In order to use Fetch in Javascript, you must not only encode the XML passed in, but decode the XML passed back out. Amarsen didn't point you to the right place in my blog for a good example of this. I think this post may have something more like what you need . Here's some code tidbits:
function MischiefMayhemSOAP(serviceUrl, xmlSoapBody, soapActionHeader, suppressError) { var xmlReq = "<?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'>" + GenerateAuthenticationHeader() + " <soap:Body>" + xmlSoapBody + " </soap:Body>" + "</soap:Envelope>"; var httpObj = new ActiveXObject("Msxml2.XMLHTTP"); httpObj.open("POST", serviceUrl, false); httpObj.setRequestHeader("SOAPAction", soapActionHeader); httpObj.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); httpObj.setRequestHeader("Content-Length", xmlReq.length); httpObj.send(xmlReq); var resultXml = httpObj.responseXML; var errorCount = resultXml.selectNodes("//error").length; if (errorCount != 0) { var msg = resultXml.selectSingleNode("//description").nodeTypedValue; if (typeof(suppressError) == "undefined" || suppressError == null) { alert("The following error was encountered: " + msg); } return null; } else { return resultXml; } } function Fetch(fetchXml) { var xmlSoapBody = "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" + " <fetchXml>" + FetchEncode(fetchXml) + " </fetchXml>" + "</Fetch>"; var fetchResponse = MischiefMayhemSOAP("/MSCRMServices/2007/CrmService.asmx", xmlSoapBody, "http://schemas.microsoft.com/crm/2007/WebServices/Fetch"); if (fetchResponse != null) { var fetchResults = new ActiveXObject("Msxml2.DOMDocument"); fetchResults.async = false; fetchResults.resolveExternals = false; fetchResults.loadXML(fetchResponse.text); return fetchResults; } else { return null; } }
As you may or may not notice, I interpret the response from the server as a new Msxml2.DOMDocument instance. Use this code as follows:
var fetchXML = " <fetch mapping='logical'>" + " <entity name='account'>" + " <attribute name='name'/>" + " <filter type='and'>" + " <condition attribute='accountnumber' operator='eq' value='something'/>" + " </filter>" + " </entity>" + " </fetch>"; var fetchResults = Fetch(fetchXML); alert(fetchResults.text);
fetchResults represents an XML document which can be traversed with selectSingleNodes() to pick out your value from the resultset/result nodes.
Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com- Edited by DavidBerryMVP, Moderator Friday, October 1, 2010 12:56 AM modified code usage example to prevent confusion
- Proposed as answer by Curt Spanburgh MVP ModeratorMVP, Moderator Friday, October 1, 2010 5:32 AM
Friday, October 1, 2010 12:53 AMModerator -
Here is the code example for assigning to a CRM Attribute on the form
var result = xmlhttp.responseXML.xml;
// Create a new DOM document and load the response XML
var doc = new ActiveXObject("MSXML2.DOMDocument");
doc.async = false;
doc.loadXML(result);// Return the q1:telephone1 node
var telephone1Node = doc.selectSingleNode("//q1:telephone1");
if( telephone1Node != null )
{
// If the node exists, return its value
crmForm.all.<Attributename.DataValue> = telephone1Node.text;
}
else
{
return null;
}
Hope this helps. Amar- Marked as answer by Mostafa Moatassem Friday, October 1, 2010 9:31 PM
Friday, October 1, 2010 9:16 AM