Thursday, August 5, 2010

CRM 4.0 : Retrieve Values using FetchXML.

Hi All,
Last week i have implemented to retrieve the values from an entity using fetchxml. hope this will be useful for our other development.


function populateTopicSubTopic(premise)
{
var authenticationHeader = GenerateAuthenticationHeader();
// Prepare the SOAP message.
var xml = ""+
"
authenticationHeader+ ""+
"
<fetch mapping='logical'>"+
"<entity name='new_subtopic'>"+
"<attribute name='new_name'/>"+
"<attribute name='new_subtopicid'/>"+
"<attribute name='new_subtopicsid'/>"+
"<attribute name='new_subtopicsidname'/>"+
"<filter type='and'>"+
"<condition attribute='new_premisetype' operator='eq' value='"+premise+"'/>"+
"</filter>"+
"</entity>"+
"</fetch>
"+
"
"+
"
"+
"
";
// Prepare the xmlHttpObject and send the request.
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);
// 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);
}
// Process and display the results.
else
{

// Capture the result and UnEncode it.
var resultSet = new String();
resultSet = resultXml.text;
resultSet.replace('<','<'); resultSet.replace('>','>');

// Create an XML document that you can parse.
var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false;
// Load the XML document that has the UnEncoded results.
oXmlDoc.loadXML(resultSet);
// Display the results.
var results = oXmlDoc.getElementsByTagName('result');
if (results.length>0)
{
var tlookupItem=new Object();
tlookupItem.id=results.item(0).selectSingleNode("new_subtopicid").text;
tlookupItem.typename='new_subtopic';
tlookupItem.name=results.item(0).selectSingleNode("new_name").text;
crmForm.all.new_subtopicid.DataValue=new Array(tlookupItem);

var stlookupItem=new Object();
stlookupItem.id=results.item(0).selectSingleNode("new_subtopicsid").text;
stlookupItem.typename='new_topic';

var topicname='';
if (results.item(0).getElementsByTagName('new_subtopicsid').item(0).attributes[0].name=='name')
{
topicname=results.item(0).getElementsByTagName('new_subtopicsid').item(0).attributes[0].text;
}
else if(results.item(0).getElementsByTagName('new_subtopicsid').item(0).attributes[1].name=='name')
{
topicname=results.item(0).getElementsByTagName('new_subtopicsid').item(0).attributes[1].text;
}
//stlookupItem.name=results.item(0).getElementsByTagName('new_subtopicsid').item(0).attributes[0].text;
stlookupItem.name=topicname;

crmForm.all.new_topicid.DataValue=new Array(stlookupItem);

return true;
}
else return false;
}
return false;
}


Cheers..!

No comments:

Post a Comment