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:  }

· Make two separate function for the parameters of retrieveRecord function.  You can call them “Success” and “Error” for example.

  • Function Success will pass an object for our example “contact” (This object is your retrieve record that will be used for accessing all of the properties for the contact entity).
  • Your code will need to use the content entity, “contact”, to assign data to the fields of the child entity as well as access data from the child entity fields and write back data to the parent contact entity (this is done by using get attribute and set values from XRM page library from Dynamic CRM 2011).
   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:  }

· Now to utilize the retrieve method from the SDK.

  • The first parameter is the parent ID.
  • The second parameter Is the Object (Contact).
  • The third Null.
  • The fourth Null.
  • The fifth is function Success.
  • The sixth is function Error.
   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:  }

(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:  }

The result is that you can update and retrieve data from parent entities using your Javascript.