CRM 2011 Javascript Retrieve Record using jQuery REST Endpoints.
Using jQuery and REST Endpoints is one of the easiest way to get record details in CRM 2011.
To do this you just need to include:
1. jquery-1.6.2.min.js (or just get the latest version)
2. json2.js
The SDK provides some functions for retrieving records:
Use retrieveRecord if you know the ID of the record. Otherwise, use retrieveMultiple if you want to get the record(s) based on some filters as it will return an array of result objects.
Example:
In a custom entity form, I have an Account lookup field and an Account Number field. I want to auto-populate one of the field based on the other field and vice versa. Therefore:
Hope this helps,
Andreas
To do this you just need to include:
1. jquery-1.6.2.min.js (or just get the latest version)
2. json2.js
The SDK provides some functions for retrieving records:
Use retrieveRecord if you know the ID of the record. Otherwise, use retrieveMultiple if you want to get the record(s) based on some filters as it will return an array of result objects.
Example:
In a custom entity form, I have an Account lookup field and an Account Number field. I want to auto-populate one of the field based on the other field and vice versa. Therefore:
Hope this helps,
Andreas
This is really helpful, I do have one question in the function retrieveAccountNumberCompleted how to I get the Lookup Field data? like getting the GUID of account.ParentAccountId.
ReplyDeleteThanks
I couldn't get retrieveMultiple to work with your filter before removing the $. When I did it worked fine.
ReplyDeleteHi i am having seanario , i have a look up in wich i want to fetch a field value. can any one help with suitable example.
ReplyDeleteHi, sorry what is your exact requirement?
DeleteDo you want to get a lookup value from JavaScript or from c# code?
Hi Andreas, this is a great article! I'm hoping that you might be able to help me out. At my job I am the local computer guy and they are looking to me for configuring the new CRM 2011 online system. I have a very limited knowledge of programming and have spent the last couple months researching blogs, videos, and books to see if I can configure the system for our needs. Unfortunately, it seems that everything I look at assumes that the reader already has a programming background and I get lost. I was hoping that you might be able to help out with a script to get me started which I can then use as a reference in any future customizations.
ReplyDeleteAs I said before, it is an online CRM system with only the basic entities. In this situation we are using the Account and Case entities. Both entities have a custom Option Set field called Key Customer. Each Account record will have this filled out. When a case is created we use the Customer field (which is a lookup field) that references the accounts that we have. When an account is selected we want it to auto-populate the Key Customer field with the same value that is selected for that customer in the account entity. I was looking at your post on this page and it seems to be very close to what I am looking for. I'm just not well enough versed with the language to modify it appropriately. Any help that you can provide would be greatly appreciated.
Thanks in advance!
Hi,
DeleteYes you came to the right section.
Basically you just need the retrieveRecord function (put that on the Entity OnLoad event)
Then in the OnChange event of your Customer field, put this code:
var accountLookup = new Array();
accountLookup = Xrm.Page.getAttribute("new_accountid").getValue();
if (accountLookup == null) {
Xrm.Page.getAttribute("keycustomer").setValue(null);
}
else {
var accountId = accountLookup[0].id;
retrieveRecord(accountId, "AccountSet", retrieveAccountCompleted, null);
}
function retrieveAccountCompleted(data, textStatus, XmlHttpRequest) {
//Get back the Account JSON object
var account = data;
//Get the KeyCustomer optionset value (can't remember the syntax on top of my head) then assign it to the KeyCustomer field for the Case entity.
}
else {
Xrm.Page.getAttribute("keycustomer").setValue(null);
}
}
Hope this helps.
Regards,
Andreas
Hi,
DeleteYes, that was very helpful thank you!
I'm still working on trying to get the optionset value so I can assign it to the KeyCustomer field. From what I have read it I need to pull the value from metadata. It's a work in progress. Thanks again for your help, it helped quite a bit with my understanding of retrieving information.
Andreas,
ReplyDeleteI have found this article very useful, but I have encountered a problem. When a user clicks save and exit, the callback does not get called in enough time before the windows closes. Is there a way we can modify the call so that it will wait for the response?
Thanks
Hi,
Deleteyou can make the call synchronously. Try adding async: false option on the ajax call.
Regards,
Andreas
That worked great Andreas thanks. But as it goes in programming everyone wants more so now I have been given the duty to use the retrieve multiple. The only problem is I am trying to filter on a custom field and I think it is causing some trouble. The field is a lookup, and here is an example of the filter I am passing:
Deletevar filterOptions = "?$select=new_category,new_name,new_custom_id&$filter=new_custom_id/Id eq guid'"+ customGuid + "'";
retrieveMultiple("new_customSet", filterOptions, customCallback, errorCallback)
Where here I know the new_custom_id is a lookup to another custom entity.
Hi it is probably a syntax problem. Have you tried using this tool? http://crm2011odatatool.codeplex.com/
DeleteIt is really useful to get your odata query right :)
Regards,
Andreas
hi,
ReplyDeletei hope you can help me, after i retrieve a coustome entity and when i try to get a value of an attribute i get that its undifiend (the field has value).
thanx
ezra
Hi,
DeleteYou can debug your success callback function and see if your property is there.
var customentity = data;
alert(data);
Regards,
Andreas