RE: Hide add new button of subgrid when there are a records in the subgrid
Hello,
You can do this by customizing the Enabled Rules with RibbonWorkbench.
First, create a new JS web-resource (library) with this code.
This code is an enable rule that checks if a record contains "tech support" in a field. It needs the opportunity id of the form that will be provided by the PrimaryControl (see
bellow).
formatId(id) { if (id == null) return null; return id.replace(/{|}/g, '); } async function isAddButtonEnabled(primaryCtrl) { const formCtx
= primaryControl; const opportunityId = formatId(formCtx.data.entity.getId()); const response = await Xrm.WebApi.retrieveMultipleRecords( "target_entity", `?$select=target_entityid&$filter=_prefix_opportunityid_value eq '${opportunityId}' and prefix_customfield
eq 'tech support'` ); return response.entities.length > 0; }
Replace:
1. target_entity with the entity logical name of your custom entity.
2. _prefix_opportunityid_value by the lookup name to opportunity.
3. prefix_customfield by the custom field which can contain "tech support".
You can obtains the names from the tool Metadata Browser of XrmToolBox. To get the name of a field for the web API, right click => Copy name for web api.&type=information
You can test the retrieval in your browser by executing in the browser console (F12) the command Xrm.WebApi.retrieveMultipleRecord(...). Don't forget to set
opportunityId and add await before the call to get the results.
Then customize the button with RibbonWorkbench by creating a solution containing the subgrid entity.
1. Click on the Add button in SubGrid section.
2. Right click => Customize Command.
3. Click on the command associated to the Add button.
4. Add Enable Rule => Add New Enable Rule
5. Add Step => CustomRule
6. Set the
library and the function to call, here isAddButtonEnabled.
7. Add Parameter => CRM Parameter.
8. Set Value to PrimaryControl.
9. Publish