So, this week there was a special requirement from the client, a manual N:N recursive relationship for the Territory entity, which is a system entity. The client also wanted that when creating the relationship entity records, CRM could recognize if I was creating a child or a parent record, so the corresponding lookup was disabled and the other cleared up.
Since the "Add new related" button on a subgrid sets both lookups to the original record, a problem arose.
I posted the question in here where it was promptly answered, and with some tweaking, it was completely implemented.
1. After creating the relationship entity, I used the Visual Ribbon Editor tool to create the two new buttons, one to create children, and another one to create parents:
2. Configured the actions to call the specific functions:
3. I had to add custom parameters to the relationship entity form since the Territory entity did not let me passing parameters based on the lookup fields names:
![]() |
| One for the AddChild button |
![]() |
| One for the AddParent button |
The id parameters are of type UniqueIdentifier, the rest are SafeString.
4. Then, the JScript functions were created:
5. Finally, on the onload event of the relationship entity form, the following function is invoked to retrieve and assign the values passed through the custom parameters.
And that's it!, when each button is clicked, it opens the new relationship entity form, with the lookup fields set according to each button.
Happy Coding!
function AddChild() {
var parameters = {};
//Set the Parent field value to the current record.
parameters["parameter_parentid"] = Xrm.Page.data.entity.getId(); //id of primary entity
parameters["parameter_parentname"] = Xrm.Page.getAttribute("name").getValue(); //the name field of primary entity
parameters["parameter_parenttype"] = "territory"; //the primary entity schema name
// Open the window.
Xrm.Utility.openEntityForm("new_zonesubzones", null, parameters);
}
function AddParent() {
//debugger;
var parameters = {};
//Set the Parent field value to the current record.
parameters["parameter_childid"] = Xrm.Page.data.entity.getId(); //id of primary entity
parameters["parameter_childname"] = Xrm.Page.getAttribute("name").getValue(); //the name field of primary entity
parameters["parameter_childtype"] = "territory"; //the primary entity schema name
// Open the window.
Xrm.Utility.openEntityForm("new_zonesubzones", null, parameters);
}
5. Finally, on the onload event of the relationship entity form, the following function is invoked to retrieve and assign the values passed through the custom parameters.
function OnLoad() {
if (Xrm.Page.ui.getFormType() == 1) {
var param = Xrm.Page.context.getQueryStringParameters();
if (param["parameter_parentid"] != null) {
var parentId = param["parameter_parentid"];
var parentName = param["parameter_parentname"];
var parentType = param["parameter_parenttype"];
Xrm.Page.getAttribute("new_zoneid").setValue([{ id: parentId, name: parentName, entityType: parentType}]);
Xrm.Page.getControl("new_zoneid").setDisabled(true);
}
if (param["parameter_childid"] != null) {
var childId = param["parameter_childid"];
var childName = param["parameter_childname"];
var childType = param["parameter_childtype"];
Xrm.Page.getAttribute("new_subzoneid").setValue([{ id: childId, name: childName, entityType: childType}]);
Xrm.Page.getControl("new_subzoneid").setDisabled(true);
}
} else {
Xrm.Page.getControl("new_subzoneid").setDisabled(true);
Xrm.Page.getControl("new_zoneid").setDisabled(true);
}
}
And that's it!, when each button is clicked, it opens the new relationship entity form, with the lookup fields set according to each button.
Happy Coding!




