Monthly Archives: May 2019

Dynamics 365 Set Parent Child relationship from Correct Sub grid (Relationship)

Let us consider we have two entities. Entity A (Parent entity) and Entity B (child entity). I have created two lookup of entity A in entity B. Now we will have two associated sub grids of Entity B in Entity A. Whenever I tried a new entity B record (child record) from entity A (parent record) using the associated sub grid add button, it’s auto populating both the lookup fields of entity A in entity B record.

For this scenario, the easiest way is just to remove the field mapping. But the problem is when you want to try to delete one of the mapping, CRM platform won’t allow us to do it. Error details are as follows:

Then the only way to achieve our scenario is by customization only and here are my steps to solve it:

It’s not always recommended to customize the OOB button. So, in this case we can achieve this scenario by adding a custom button.

Create a html resource using the below code:

<!DOCTYPE html>
  
 <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta charset="utf-8" />
 <title>Entity B Record</title>
 https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js
 http://../../ClientGlobalContext.js.aspx
 <style>
 .btnSubmit {
 background-color:rgba(0, 0, 0, 1) !important;
 height: 24px;
 font-family: Segoe UI;
 font-size: 12px;
 color: rgb(255,255,255);
 border: 1px solid rgba(0, 120, 215, 1.0);
 background-image: none;
 margin-top: 10px;
 width: auto !important;
 min-width: 80px;
 white-space: nowrap;
 }
 </style>
 
 function openEntityBRecord () {
 debugger;
 var recordId = window.parent.Xrm.Page.data.entity.getId();
 var fullName = window.parent.Xrm.Page.data.entity.attributes.get("fullname").getValue();
 var entityFormOptions = {};
 entityFormOptions["entityName"] = "plc_associatedpriorityservice";
 entityFormOptions["openInNewWindow"] = true;
 var formParameters = {};
 formParameters["EntityA1id"] = recordId;
 formParameters["EntityA1name"] = fullName;
 Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
 function(success) {
 console.log(success);
 },
 function(error) {
 console.log(error);
 });
 }
 
 </head>
 <body>
 <input style="float:right;" type="button" class="btnSubmit" id="btnSubmit" name="btnSubmit" value="Add Entity B Record" onclick="openEntityBRecord();">
 </body>
 </html> 

Then go to form Editor of Entity A and insert a web resource just above the first associated sub grid. When finished, publish all customization and you can try the result.

Now the “Add Entity B Record” button will appear above the first associated sub grid in Entity A(parent) record.

Similar in the same way we need to do it for second associated sub grid.

Also, we need to hide the OOB Add button form both the sub grids by using the below code:

 var hideSubgridAddBtn = function(primaryEntityName) {
         if (primaryEntityName == “entityname”)
             return false;
         else
             return true
     }; 

Add the above method to the one JavaScript library and we need to add a enable rule to the OOB button and call the above method “hideSubgridAddBtn” from the above newly created JavaScript library.

Once done, publish all the customization and you can try the result. It will set only the first lookup of entity A in entity B record.