In this example i am trying to retrieve Account entity information in form load script using ODATA and JSON in CRM 2011
To use ODATA service you need two resource files
JSON and JQuery
you can download these popular resource in web...
Now write following java script in the new web resource file
function init()
{
// write required ODATA query
var odataSelect = "http://server/orgname/XRMServices/2011/OrganizationData.svc/AccountSet(guid'6C2EFF37-BD39-E011-91D1-000C2971AF13')";
$.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 this method for a selection that return single entitie
RetrieveEntityData(data.d);
// Use this method for a selection that may return multiple entities
RetrieveMultipleEntities(data.d.results);
},
error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
});
}
function RetrieveEntityData(Entity)
{
// get the fields from the Entity object
var accountNumber = Entity.AccountNumber;
var AccountName = Entity.Name;
alert(Entity.Name);
}
function RetrieveMultipleEntities(ManyEntities)
{
for( i=0; i< ManyEntities.length; i++)
{
// get the fields from the Entity object
var Entity = ManyEntities[i];
var accountNumberAttribute = Entity.AccountId;
var AccountName = Entity.Name;
alert(Entity.Name);
}
}
Just call the init method from the CRM form events, but make sure you have to add JSON and JQuery web resources to the event library before adding this web resource. It has to follow the sequence JSON,JQuery and Custom web Resource.
To get the Required entity ODATA set use the following url
Good Code yaar
ReplyDeletehi i try to implement but it give error
ReplyDeleteHi Ashish,
ReplyDeleteWhat kind of issue you are facing. can you provide me the exact error, so that i can help.
Hi Prasad,
ReplyDeletewaiting for reply. now a days i am developing transaction entity plugin but it not working can you have any sample code for it??
Hi Ashish,
ReplyDeleteI am not clear with your requirement. if you can provide more info, i can try to help..
Hi Guru!
ReplyDeleteGreat article :-D
I've changed the code a bit, trying to get rid of the hardcoded stuf like eg.
var accountNumber = Entity.AccountNumber;
and
alert(Entity.Name);
I did like this in the last of the functions, but I can't make it work (unless I hardcode the attribute name):
RetrieveEntityData(data.d, fromTarget, toTarget); // The call of the 3rd function in the 2nd function
// The fromTarget and toTarget are transferd parameters from the onChange call, containing the attribute names of the fields from/to which I intend to read/write
function RetrieveEntityData(Entity, fromTarget, toTarget){ // In this case the fromTarget contains the text "log_CMDBBeskrivelse"
var from_a = Entity.fromTarget; // this doesn't work
var from_b = Entity.log_CMDBBeskrivelse; // this works fine
Xrm.Page.getAttribute(toTarget).setValue(from_a); // so this doesn't work eighter
Xrm.Page.getAttribute(toTarget).setValue(from_b); // but this works fine...
}
Please help me as I'm going crazy over this...!
Thanks in advance ;-)
Hi Tomas,
ReplyDeleteyou can change the function like this
function RetrieveEntityData(Entity, fromTarget, toTarget){ // In this case the fromTarget contains the text "log_CMDBBeskrivelse"
//var from_a = Entity.fromTarget; // this doesn't work, because here fromTarget won't replaced with filed name, it considers as property of entity
var from_a = Entity[fromTarget]; // fromTarget - this must be Schema Name of the attribute
var from_b = Entity.log_CMDBBeskrivelse; // this works fine
Xrm.Page.getAttribute(toTarget).setValue(from_a);
Xrm.Page.getAttribute(toTarget).setValue(from_b); // but this works fine...
}
Let me know if any issues...
Hi Guru!
DeleteThanks for your reply - I still couldn't make it work (I had already tried with the Schema Name without any luck).
Finally I solved it using the EVAL method like this:
function RetrieveData(Entity, readFrom, writeTo){
eval("Xrm.Page.getAttribute(writeTo).setValue(Entity." + readFrom + ");");
}
I know that it's recommended to use JSON.parse method instead of the eval method (due to possible security vulnerabilities), but as I already use the json2.js script (which also uses the eval method) I guess I'll leave it as it is now ;-)
Thanks again though ;-)
Tomas
Hi Guru
ReplyDeleteThanks for your article.
I have a pb when I load my form. I get an error message which comes from the json2 function. I've just created a web ressource named new_json2 and pasted the sample code 'json' from the sdk. Was it the right way to use the oData restpoint ?
Thank you in advance
Hi Guru,
ReplyDeleteI am getting error on JSON.Parse() function. I have successful added JSON.js file in my html webresource.
Can you please explain me about this where I am wrong
Thank you in advance