serving the solutions day and night

Pages

Thursday, April 25, 2013

CRM Feed Parse - JSON

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://<SERVER_NAME>/<ORG>/XRMServices/2011/OrganizationData.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">ContactSet</title>
  <id>http://<SERVER_NAME>/<ORG>/XRMServices/2011/OrganizationData.svc/ContactSet</id>
  <updated>2013-03-27T22:56:13Z</updated>
  <link rel="self" title="ContactSet" href="ContactSet" />
  <entry>
    <id>http://<SERVER_NAME>/<ORG>/XRMServices/2011/OrganizationData.svc/ContactSet(guid'a1034be1-0000-e211-a0c1-0050568c5ad0')</id>
    <title type="text">DAY NIGHT</title>
    <updated>2009-03-27T22:56:13Z</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="Contact" href="ContactSet(guid'a1034be1-0000-e211-a0c1-0050568c5ad0')" />
    <category term="Microsoft.Crm.Sdk.Data.Services.Contact" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:FirstName>DAY</d:FirstName>
        <d:ContactId m:type="Edm.Guid">a1034be1-0000-e211-a0c1-0050568c5ad0</d:ContactId>
        <d:LastName>NIGHT</d:LastName>
      </m:properties>
    </content>
  </entry>
</feed>
     var xmlhttp;
     if (window.XMLHttpRequest) {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
     } else {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     var url ="/<ORG>/XRMServices/2011/OrganizationData.svc/ContactSet()?$select=ContactId,FirstName,LastName&orderby=fullname&$filter=employeeid eq '" + eid+ "'";
     xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
       var jsonDoc = JSON.parse(xmlhttp.responseText).d;
       var contatid;
       for (var i = 0; i < jsonDoc.results.length; i++) {
        contatid = jsonDoc.results[i].ContactId; //can get this node
       }
       var erURL = "/<ORG>/main.aspx?etn=contact&pagetype=entityrecord&id=%7B"+ contatid + "%7D";
       window.open(erURL,'erURL','directories=0,location=0,menubar=0,status=0,toolbar=0');
       }
     }
     xmlhttp.open("GET",url,true);
     xmlhttp.setRequestHeader("Accept", "application/json");
     xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
     xmlhttp.send(null);
 

 The jquery1.4.1.min.js and json2.js files can be found in the most recent MS CRM SDK. 
sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts. 

var odataSelect = "Your OData Query";

$.ajax({
       type: "GET",
       contentType: "application/json; charset=utf-8",
       datatype: "json",
       url: odataSelect,
       beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
       success: function (data, textStatus, XmlHttpRequest) 
           { 
               // Use only one of these two methods

               // Use for a selection that may return multiple entities
               ProcessReturnedEntities(data.d.results); 

               // Use for a single selected entity
               ProcessReturnedEntity(data.d);
           
           },
       error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
   });
   

function ProcessReturnedEntities(ManyEntities)
{
  for( i=0; i< ManyEntities.length; i++)
  {
     var oneEntity = ManyEntities[i];
     var accountNumberAttribute = oneEntity.AccountNumber;    
     
     var accountNumberValue = eval(oneEntity.AccountNumber);
  }
}

function ProcessReturnedEntity(OneEntity)
{  
     var oneEntity = OneEntity;
     var accountNumber = oneEntity.AccountNumber;
     
     var accountNumberValue = eval(oneEntity.AccountNumber);     
}   

You can find out the type of a form control as follows:
var attrType = Xrm.Page.getAttribute("accountnumber").getAttributeType();

string, memo fields:
Xrm.Page.getAttribute("accountnumber").setValue(eval(oneEntity.AccountNumber));

decimal, double fields:
Xrm.Page.getAttribute("new_float").setValue(parseFloat(eval(oneEntity.new_Float)));

integer fields
Xrm.Page.getAttribute("new_integer").setValue(parseInt(eval(oneEntity.new_Integer)));

money fields
Xrm.Page.getAttribute("new_moneyattribute").setValue(parseFloat(eval(oneEntity.new_MoneyAttribute.Value)));

optionset fields
Xrm.Page.getAttribute("new_optionset").setValue(eval(oneEntity.new_OptionSet.Value));

date fields
var fieldValue = eval(oneEntity.new_DateTime);                       
var dateValue = new Date(parseInt(fieldValue.replace("/Date(", "").replace(")/", ""), 10));
Xrm.Page.getAttribute("new_datetime").setValue(dateValue);

Lookup 
var olookup = new Object();
olookup.id = ManyEntities[0].Id;
olookup.entityType = "account";
olookup.name = ManyEntities[0].Name;
var olookupValue = new Array();
olookupValue[0] = olookup;
Xrm.Page.getAttribute("account").setValue(olookupValue); 

 

2 comments:

Unknown said...

Hello Mak!

I am not too familiar with CRM.
Can you please elaborate on this post with a few lines describing the requirement and where to put the code in?

Manu.

makdns said...

var url ="//XRMServices/2011/OrganizationData.svc/ContactSet()?$select=ContactId,FirstName,LastName&orderby=fullname&$filter=employeeid eq '" + eid+ "'";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var jsonDoc = JSON.parse(xmlhttp.responseText).d;
var contatid;
for (var i = 0; i < jsonDoc.results.length; i++) {
contatid = jsonDoc.results[i].ContactId; //can get this node
}
var erURL = "//main.aspx?etn=contact&pagetype=entityrecord&id=%7B"+ contatid + "%7D";
window.open(erURL,'erURL','directories=0,location=0,menubar=0,status=0,toolbar=0');
}
}

put the above code in your form onload evenet, response will be json format (xmlhttp.responseText), that was i mentioned on the top of the blog.