Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Ribbon button visibility issue

(3) ShareShare
ReportReport
Posted on by 28,967 Moderator

I have created custom action to generate sor code on Account entity in dynamics CRM. I am calling this custom action from ribbon button using XRM.webapi.execute . Ribbon button has display rule javascript code to hide the button when SOR code field contains data...we are updating the account sor code in custom action. We are able to refresh the form but when ribbon button is still displayed even though SOR code field has data populated from custom action plugin code...how to fix this?

  • Suggested answer
    Daivat Vartak (v-9davar) Profile Picture
    5,229 Super User 2025 Season 1 on at
    Ribbon button visibility issue
    Hello Bipin D365,
     

    The Core Issue:

    • Asynchronous Nature: Xrm.WebApi.execute is asynchronous. Your ribbon display rule JavaScript runs before the custom action updates the SOR code field.
    • Form Refresh vs. Ribbon Re-evaluation: Refreshing the form data doesn't automatically re-evaluate the ribbon's display rules.

    •  

     

    The Solution: Force Ribbon Re-evaluation

    The most reliable way to solve this is to force a re-evaluation of the ribbon after your custom action completes and the form is refreshed.

     

    Here's the JavaScript you should use:

    function generateSORCode(primaryControl) {
        var formContext = primaryControl;
        var accountId = formContext.data.entity.getId().replace('{', '').replace('}', '');
        var actionName = "new_GenerateAccountSORCode"; // Replace with your action's logical name
        var data = {
            "AccountId": {
                "@odata.type": "Microsoft.Dynamics.CRM.account",
                "accountid": accountId
            }
        };
        Xrm.WebApi.execute(actionName, data).then(
            function success(result) {
                if (result.ok) {
                    formContext.data.refresh().then(function () {
                        // Force ribbon re-evaluation AFTER form refresh
                        Xrm.Utility.forceControlRefresh();
                    });
                }
            },
            function (error) {
                console.log(error.message);
            }
        );
    }

     

    Key Points:

    • Xrm.Utility.forceControlRefresh();: This is the crucial line. It tells Dynamics 365 to re-evaluate all ribbon button display rules.
    • Placement: Ensure Xrm.Utility.forceControlRefresh(); is called after the formContext.data.refresh() promise resolves. This ensures the ribbon is re-evaluated with the updated data.

    •  

    Why This Works:

    1. Custom Action Execution: The custom action generates the SOR code.
    2. Form Refresh: The form is refreshed to display the updated SOR code.
    3. Ribbon Re-evaluation: Xrm.Utility.forceControlRefresh(); forces Dynamics 365 to run the display rule's JavaScript again.
    4. Display Rule Logic: The display rule now correctly sees that the SOR code field has data and hides the button.

    5.  

    Troubleshooting:

    • Correct Field Name: Double-check that your display rule JavaScript uses the correct logical name of the SOR code field.
    • Error Handling: Add more robust error handling to your JavaScript to catch any potential issues.
    • Caching: Clear your browser cache or try an incognito window, in case the previous version of the javascript is cached.
    • Publishing: Make sure you have published all the changes in the solution, including the javascript web resource, and the ribbon customization.

    •  

    If you are still experiencing issues, please provide the following information:

    • The exact JavaScript code of your ribbon display rule.
    • The exact logical name of your SOR code field.
    • Any error messages you are seeing.

    •  

    This additional information will help me provide more specific troubleshooting guidance.

     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more. If you have further questions, please feel free to contact me.
     
    My response was crafted with AI assistance and tailored to provide detailed and actionable guidance for your Microsoft Dynamics 365 query.
     
    Regards,
    Daivat Vartak
  • ArchitectMadhan Profile Picture
    391 on at
    Ribbon button visibility issue
    Hi,
     
    The issue you're facing with the ribbon button visibility is likely due to the timing of the form refresh and the evaluation of the display rule. Here's how you can address it:
    1. Ensure Proper Field Update:
      • Verify that the SOR code field is being updated correctly in your custom action. If the field is updated asynchronously, the display rule might not detect the change immediately.
    2. Force Re-Evaluation of Display Rules:
      • After updating the SOR code field in your custom action, you need to explicitly refresh the ribbon to re-evaluate the display rules. Use the following JavaScript code to refresh the ribbon:
      • Xrm.Page.ui.refreshRibbon();
    Add this code after the form refresh logic in your custom action call.
    1. Check Display Rule Logic:
      • Ensure that your JavaScript code for the display rule is correctly checking the SOR code field value. For example:
    function isSORCodeEmpty() {
        var sorCode = Xrm.Page.getAttribute("sorcode").getValue();
        return !sorCode; // Returns true if SOR code is empty
    }
    Make sure this function is properly linked to the display rule in the Ribbon Workbench.
    1. Publish and Test:
      • After making these changes, publish your customizations and test the functionality. Ensure that the ribbon button hides as expected when the SOR code field contains data.
     
    If you find this solution useful, please like it and accept it as answer.
    -ArchitectMadhan

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,639 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,663 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans