Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Hide add new button of subgrid when there are a records in the subgrid

Posted on by 10

Hi,

I want to hide the '+' (Add New record) button of Subgrid and 'New' button of Custom entity (On ribbon) as well. When we click on '+' button of Subgrid, it open the custom entity form where have some custom fields. If a custom field value is ' tech support', then only allow to add more than one record otherwise want to hide the add button of Subgrid. Subgrid is under a tab on Opportunity form.

I tried to do using JavaScript but not working. Please help.

Thanks in Advance.

  • om_mzp Profile Picture
    om_mzp 10 on at
    RE: Hide add new button of subgrid when there are a records in the subgrid

    Thanks Xavier for detailed explanation.

  • Suggested answer
    RE: Hide add new button of subgrid when there are a records in the subgrid

    Must be customized using RibbornWorkBench.

  • Suggested answer
    XM-22040801-0 Profile Picture
    XM-22040801-0 11 on at
    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

  • om_mzp Profile Picture
    om_mzp 10 on at
    RE: Hide add new button of subgrid when there are a records in the subgrid

    Thanks for your response.

    Yes, want to dispaly add button of subgrid, only if the grid contains records with the value 'tech support' in their custom field?

  • XM-22040801-0 Profile Picture
    XM-22040801-0 11 on at
    RE: Hide add new button of subgrid when there are a records in the subgrid

    Hello,

    I'm not sure I understand what you want to do.

    Do you want to display the Add button only if there are no records or only if the grid contains records with the value 'tech support' in their custom field?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans