Introduction
A Quick post, just want to post how to Modify Subgrid button behaviour to always “Create New child record”, instead of “Add Existing record”.
This will use the similar method like described in the
previous post before, but it does not call the custom funtion, it just will “redirect” the function to your “Create new record”.
Note: This is an unsupported way, for the supported way you can do two things:
1. Make the parent lookup field name to ‘Required’
2. Modify the Ribbon of the subgrid and Hide the ‘Add Existing’ button
*For your reference – > Nice post by Gareth:
https://garethtuckercrm.com/2013/11/19/fixing-the-sub-grid-user-experience-in-crm-2013/
But, those will apply to the subgrid regardless the Condition, probably for the second method, you could apply some Enable/Display Rule on the subgrid ribbon.
But, if you want to do it only for specific entity and based on Parent Form condition (can be based on parent fields or even other entity values), then there is another way to do this that I will put the code in this post.
The Code
Ok, so back to the code.
Here is the code:
//function to insist create new record form instead of Add Existing
function openNewRecordFormFromSubgridButton(subgridName, passGridControl) {
debugger;
try {
//to store the original function ones
var originalFunctionAddNewStandard = Mscrm.GridRibbonActions.addNewFromSubGridStandard;
var originalFunctionAddExistingStandard = Mscrm.GridRibbonActions.addExistingFromSubGridStandard;
var originalFunctionAssociated = Mscrm.GridRibbonActions.addExistingFromSubGridAssociated;
var primaryControl = document.getElementById('crmForm');
var parentEntityId = Xrm.Page.data.entity.getId();
var parentEntityTypeCode = Xrm.Page.data.entity.getEntityName();
//add existing standard subgrid
Mscrm.GridRibbonActions.addExistingFromSubGridStandard = function (gridTypeCode, gridControl) {
if (gridControl.get_id() != subgridName) {
originalFunctionAddExistingStandard(gridTypeCode, gridControl);
}
else {
var primaryControl = document.getElementById('crmForm');
originalFunctionAddNewStandard(gridTypeCode, parentEntityTypeCode, parentEntityId, null, gridControl);
}
}
//and even possible for the N:N (but this is a rare scenario)
//add associate subgrid (N:N)
Mscrm.GridRibbonActions.addExistingFromSubGridAssociated = function (gridTypeCode, gridControl) {
if (gridControl.get_id() != subgridName) {
originalFunctionAssociated(gridTypeCode, gridControl);
}
else {
var primaryControl = document.getElementById('crmForm');
originalFunctionAddNewStandard(gridTypeCode, parentEntityTypeCode, parentEntityId, null, gridControl);
}
}
}
catch (e) {
}
}
How to call:
openNewRecordFormFromSubgridButton("mysubgrid_name", true);
Result
*Previous
As we can see it opens lookup to look to the Existing record, it is 1:N, the record is almost possible already linked to another record, so it will make the users confused when they open this lookup.
*After
*And I never set any Required field to the parent lookup value, because this field is NOT always mandatory field, so it is conditional only.
Note: Use this method if you want to control the subgrid plus button based on some conditions and using the Form on Load script (easier), and it only works for the subgrid inside the form that load the function, other subgrid from another parent entity that does not have this function will not be affected, that is the advantage of using this method, to only affect subgrid of the child based on some parent entities and the function will be placed in the Parent Entity Form OnLoad and this is unsupported way.
“This is for dynamically control the subgrid behaviour through parent form onload functions based on some conditions logic, especially for 1:N relationship that usually already linked to another parent record, so that it is supposed to insist to open new Form, instead of asking the user to choose another existing record.
Hope this helps!
Thanks