Follow Us:
When customizing an entity form in Dynamic CRM 2011 I found that a common request is for a child entity form to inherent some data from its parent. I noticed people using window.top.opener.parent. It can be efficient to use the retrieve and update methods from SDK.jquery.js or the min.js to acquire the data from the parent entity. Here is a discussion on how to utilize this JavaScript. For coding purpose I enjoy using the XrmPageScriptDevelopmentProjectCS dowloaded from Extension Manager. Also, reference a download of Microsoft Dynamics CRM 2011 JavaScript Development Cheat Sheet from Danial Cai blog.
To retrieve the parent entity you will need to:
· Create a Javascript web resource on CRM under a solution.
· Then in the onload function use the Xrm.Page.context to retrieve the parent id from the QueryString in the URL. Your code needs to check for the Id of the child entity form to determine if it is new or an update. The querystring Id type changes for these different form types.
1: function onload() { 2: var ID = Xrm.Page.context.getQueryStringParameters().partyid; 3: if(ID == null || ID) 4: { 5: var ID = Xrm.Page.context.getQueryStringParameters()._CreateFromId; 6: } 7: }
1: function onload() {
2: var ID = Xrm.Page.context.getQueryStringParameters().partyid;
3: if(ID == null || ID)
4: {
5: var ID = Xrm.Page.context.getQueryStringParameters()._CreateFromId;
6: }
7: }
· Make two separate function for the parameters of retrieveRecord function. You can call them “Success” and “Error” for example.
1: function Success(contact) { 2: 3: Xrm.Page.getAttribute("new_address").setValue(contact.Address1_Line1); 4: 5: }
1: function Success(contact) {
2:
3: Xrm.Page.getAttribute("new_address").setValue(contact.Address1_Line1);
4:
5: }
Function Error can be used to report an exception message by using alert().
1: function Error(error){ 2: alert(error.message); 3: }
1: function Error(error){
2: alert(error.message);
3: }
· Now to utilize the retrieve method from the SDK.
1: function onload() { 2: var ID = Xrm.Page.context.getQueryStringParameters().partyid; 3: if(ID == null || ID) 4: { 5: var ID = Xrm.Page.context.getQueryStringParameters()._CreateFromId; 6: } 7: SDK.JQuery.retrieveRecord(ID, "Contact", null, null, assignBackAccount, errorHandler); 8: }
7: SDK.JQuery.retrieveRecord(ID, "Contact", null, null, assignBackAccount, errorHandler);
8: }
(Tip) when naming the methods to be used use logical names that cannot be confused. Also import the JavaScript files, json2.js and your min.js, to the entity forms.
· Now to update parent entity Contact you need to call the UpdateRecord from the SDK within your function Success. UpdateRecord methods parameters are similar to that of the Retrieve method.
1: function Success(contact) { 2: 3: Xrm.Page.getAttribute("new_address").setValue(contact.Address1_Line1); 4: contact.Address1_Line2 = Xrm.Page.getAttribute("new_address").getValue(); 5: 6: SDK.JQuery.updateRecord( contact.ContactId, contact, "Contact", saved,err); 7: 8: } 9: 10: function saved(){ 11: alert("saved"); 12: } 13: 14: function err(error){ 15: alert(error.message); 16: }
4: contact.Address1_Line2 = Xrm.Page.getAttribute("new_address").getValue();
5:
6: SDK.JQuery.updateRecord( contact.ContactId, contact, "Contact", saved,err);
7:
9:
10: function saved(){
11: alert("saved");
12: }
13:
14: function err(error){
15: alert(error.message);
16: }
The result is that you can update and retrieve data from parent entities using your Javascript.
The complementary paper includes over 12 years of research, recent survey results, and CRM turnaround success stories.
Request Download
This 60-second assessment is designed to evaluate your organization's collaboration readiness.
Learn how you rank compared to organizations typically in years 1 to 5 of implementation - and which areas to focus on to improve.
This is a sandbox solution which can be activated per site collection to allow you to easily collect feedback from users into a custom Feedback list.
Whether you are upgrading to SharePoint Online, 2010, 2013 or the latest 2016, this checklist contains everything you need to know for a successful transition.