[tweetmeme source=”BenHosk” only_single=false]
I found a very interesting blog today called CRMScape. It had a fantastic article on using OData and JSON. I have used OData and JSON to retrieve CustomerAddress entity information.
You can do it if you use the JSON2.js script and modify the example code found in CRM 2011 SDK, which you can find here.
The walkthrough/tutorial below is excellent and will save you some of the frustrations I had.
CRM 2011 OData, JSON and CRM Forms
1. Generating OData queries
OData commands can easily be tested out in IE. To help with this, you will want to turn off the feed reading view in IE.
The first thing that you will want is the name of the Sets that you will be calling on. If you use a path like the following it will provide a listing of the names of your sets.http://crmdev:5555/CRMDEV/XRMServices/2011/OrganizationData.svc
You will see a list of them in the following format.- <collection href="AccountSet"> <atom:title>AccountSet</atom:title> </collection>
Since these names are case sensitive you will want to look at the names of your custom entities. Your stock items like AccountSet and ContactSet will be camel cased, but your custom entities will likely show up as new_myentitySet.
You can get the value from them by simply using eval like shown in the example above.
For the following CRM attributes there is more involved.
For example:var moneyValue = eval( oneEntity.new_MoneyAttribute.Value); var optionSetValue = eval ( oneEntity.new_OptionSet.Value);Setting CRM Form Fields with Queried ValuesThis gets a bit more complex when setting values to CRM form controls.
- The form field names are all lower case, so the retrieved names do not match.
- The form fields have validation and maintain more types than the returned OData values have.
- There is some conversion required between them.
You can find out the type of a form control as follows:var attrType = Xrm.Page.getAttribute("accountnumber").getAttributeType();
With the type you can then use the appropriate means to set form controls.
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 fieldsXrm.Page.getAttribute("new_integer").setValue(parseInt(eval(oneEntity.new_Integer)));money fieldsXrm.Page.getAttribute("new_moneyattribute").setValue(parseFloat(eval(oneEntity.new_MoneyAttribute.Value)));optionset fieldsXrm.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);