web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Custom ribbon button not getting hidden while using WebAPI calls

(0) ShareShare
ReportReport
Posted on by

I have a custom button which should be hidden until the below requirement is satisfied. But when i use Xrm.WebApi.retrieveRecord this particular button will be visible on load of the form.
Is there any solution for this?

enableSendMailForAssignSEButton = function (primaryControl) {
debugger;
try {
var executionContext = primaryControl;
if (Helperfile == null || Helperfile == undefined) {
setTimeout("enableSendMailForAssignSEButton(executionContext);", 3000);
return;
}
var OpportunityId = executionContext.data.entity.getId().replace("{", "").replace("}", "");
Xrm.WebApi.retrieveRecord("opportunity", OpportunityId, "?$select=new_isquotecreated").then(
function success(result) {
var stage = executionContext.data.process.getActiveStage().getName();
var opportunityType = executionContext.data.entity.attributes.get("new_opportunitytype").getText().toLowerCase();
var hasPSRole = Helperfile._hasPandSEngineerRole(executionContext);
if (opportunityType != null && opportunityType == "master") {
if (stage != null || stage != undefined || stage != "") {
if (stage.trim() == "Plan & Spec" && hasPSRole) {
return true;
}
else
return false;
}
else {
return false;
}
}
});
}
catch (error) {
console.log("Error in Opportunity Js, Method: enableSendMailForAssignSEButton(), Error: " + error.message);
}
}

Thanks,

Hima

*This post is locked for comments

I have the same question (1)
  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Did you setup the  enable and display rule in the ribbon workbench correctly?

  • Community Member Profile Picture
    on at

    Yes,

    It works well if i do not use WebAPI.

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    This is because of async call , see here how do you handle async call for enable disable rule -

    butenko.pro/.../showing-ribbon-button-based-on-the-result-of-async-operation

  • Community Member Profile Picture
    on at

    Thank you Goutam! :)

  • Community Member Profile Picture
    on at

    Hello Goutam,

    I used the above code i was able to hide the button but it does not appear when the condition is satisfied.

    enableSendMailForAssignSEButton = function (primaryControl) {

       debugger;

       try {

           var isAsyncOperationCompleted = false;

           var isButtonEnabled = false;

           var executionContext = primaryControl;

           function IsButtonEnabled(formContext) {

               if (isAsyncOperationCompleted) {

                   return isButtonEnabled;

               }

               if (UTC.UtilitiesT == null || UTC.UtilitiesT == undefined) {

                   setTimeout("enableSendMailForAssignSEButton(executionContext);", 3000);

                   return;

               }

               var OpportunityId = executionContext.data.entity.getId().replace("{", "").replace("}", "");

               Xrm.WebApi.retrieveRecord("opportunity", OpportunityId, "?$select=new_isquotecreated").then(

                   function success(result) {

                       debugger;

                       isAsyncOperationCompleted = true;

                       var stage = executionContext.data.process.getActiveStage().getName();

                       var opportunityType = executionContext.data.entity.attributes.get("new_opportunitytype").getText().toLowerCase();

                       var hasPSRole = Helperfile._hasPandSEngineerRole(executionContext);

                       if (opportunityType != null && opportunityType == "master") {

                           if (stage != null || stage != undefined || stage != "") {

                               if (stage.trim() == "Plan & Spec" || hasPSRole) {

                                   isButtonEnabled = true;

                               }

                           }

                       }

                       if (isButtonEnabled) {

                           formContext.ui.refreshRibbon();

                       }

                   },

                   function (error) {

                       isAsyncOperationCompleted = true;

                       Xrm.Navigation.openAlertDialog({ text: error.message });

                   });

               return false;

           }

           return {

               IsButtonEnabled: IsButtonEnabled

           };

       }

       catch (error) {

           console.log("Error in Opportunity Js, Method: enableSendMailForAssignSEButton(), Error: " + error.message);

       }

    }

    520350585.Capture.PNG

    Thanks,

    Hima

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Hima ,

    Seems you have added another async call here in below line , might be this is the reason its not returning the correct result.

    var hasPSRole = Helperfile._hasPandSEngineerRole(executionContext);

    Try to put hard code value here to find the root cause  and give a try. Its should work the way in the article mentioned.

  • Community Member Profile Picture
    on at

    Hello Goutam,

    Yes, As suggested i removed that line and while debugging it goes till formContext.ui.refreshRibbon(); but then i get formContext undefined and the button is still hidden.

    Can you please suggest a solution? I think I'm missing something.

    try {

           var isAsyncOperationCompleted = false;

           var isButtonEnabled = false;

           function IsButtonEnabled(formContext) {

               debugger;

               if (isAsyncOperationCompleted) {

                   return isButtonEnabled;

               }

               var OpportunityId = formContext.data.entity.getId().replace("{", "").replace("}", "");

               Xrm.WebApi.retrieveRecord("opportunity", OpportunityId, "?$select=new_isquotecreated").then(

                   function success(result) {

                       debugger;

                       isAsyncOperationCompleted = true;

                       var stage = Xrm.Page.data.process.getActiveStage().getName();

                       var opportunityType = Xrm.Page.data.entity.attributes.get("new_opportunitytype").getText().toLowerCase();

                       isButtonEnabled = true;

                       if (opportunityType != null && opportunityType == "master") {

                           if (stage != null || stage != undefined || stage != "") {

                               if (stage.trim() == "Plan & Spec") {

                                   isButtonEnabled = true;

                               }

                           }

                       }

                       if (isButtonEnabled) {

                           formContext.ui.refreshRibbon();

                       }

                   },

                   function (error) {

                       isAsyncOperationCompleted = true;

                       Xrm.Navigation.openAlertDialog({ text: error.message });

                   }

               );

               return false;

           }

           return {

               IsButtonEnabled: IsButtonEnabled()

           };

       }

    Thanks,

    Hima

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans