Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Adding custom action to a button in Ribbon workbench

Posted on by 240

Hello, 

I created a custom button and added it to the ribbon workbench. I have created a cancel button. So when the 'Cancel Request' button is clicked, it should disable the fields and set status to Cancelled. Can someone please help me in writing the Javascript code for the cancel button. 

Thank you. 

  • Aneeqa Pervaiz Profile Picture
    Aneeqa Pervaiz 240 on at
    RE: Adding custom action to a button in Ribbon workbench

    Great. Way number 1 worked.

    Thank you so much.

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Adding custom action to a button in Ribbon workbench

    I don't think that workflow can disable your fields because workflow works on the background.

    There are 2 ways in order to achieve your goal:

    1. Update State (oob one - statecode) to inactive - that will turn record to read-only.

    2. Add Onload function that will check record's "new_status" field and if it is equal to "Cancelled" - run the same script to disable all of fields.

  • Aneeqa Pervaiz Profile Picture
    Aneeqa Pervaiz 240 on at
    RE: Adding custom action to a button in Ribbon workbench

    Perfect. This works. It disables the fields. And I cannot make any changes. And sends the request to the Cancelled view.

    Now, if I go to my cancelled view and open the request, I am still able to make changes. Should I add a workflow to keep the fields disabled?

    Also: Is it possible to collapse the page when the cancel request button is clicked?

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Adding custom action to a button in Ribbon workbench

    Try to use following code in order to disable all of fields:

           formContext.ui.controls.forEach(function (control, i) {

               if (control && control.getDisabled && !control.getDisabled()) {

                   control.setDisabled(true);

               }

           });

  • Aneeqa Pervaiz Profile Picture
    Aneeqa Pervaiz 240 on at
    RE: Adding custom action to a button in Ribbon workbench

    This would only disable the status field. I want to disable all fields on the form, so that no further changes can be done.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Adding custom action to a button in Ribbon workbench

    Try to replace line

    formContext.getControl("Control to disable").setDisabled(true);

    with line

    formContext.getControl("new_status").setDisabled(true);

  • Aneeqa Pervaiz Profile Picture
    Aneeqa Pervaiz 240 on at
    RE: Adding custom action to a button in Ribbon workbench

    Hello, 

    So I used the code above to set the status to Cancelled. It works. 

    Then I added in the line to disable the fields. This was my final code. 

    function new_CancelRequest(primaryControl)
    {
    var formContext = primaryControl;
    formContext.getAttribute("new_status").setValue("Cancelled");//My status is read only field - reflected from the process flow. 
    formContext.getControl("Control to disable").setDisabled(true);
    }

    Now, when i click on it, it sets status to cancelled, but does not disable the fields. It gives me this error. 

    Can you please tell what I am missing now. 

    Thank you. 

    Cancel-Test.PNG

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Adding custom action to a button in Ribbon workbench

    First try to use following code instead:

    function new_CancelRequest(primaryControl)

    {

    var formContext = primaryControl;

    formContext.getAttribute("statecode").setValue(-1);//replace statecode with schema name of your field (not label), replace -1 with whole number value of your status - it's a number that you can get from optionset customization

    }

    Also please provide a screenshot that demonstrates your command from Ribbon Workbench

  • Aneeqa Pervaiz Profile Picture
    Aneeqa Pervaiz 240 on at
    RE: Adding custom action to a button in Ribbon workbench

    So I added PrimaryControl as the parameter to the function.

    The name of my button is 'Cancel Request'. And this is my jscript code.

    new_CancelRequest = function (primary control)

    {

    var formContext = primaryControl;

    formContext.getAttribute("Status").setValue(Cancelled);

    }

    Nothing happens when I click on it. Do I need to pass execution command? What am I missing? Please help.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Adding custom action to a button in Ribbon workbench

    Hello,

    In order to pass context to your code you should add PrimaryControl as a parameter for your function - www.magnetismsolutions.com/.../getting-dynamics-365-formcontext-from-ribbon-workbench

    To set status to cancelled you can use something like:

    formContext.getAttribute("your status field name").setValue(CancelledStatusWholeNumberValue);

    in order to disable field you can use following code:

    formContext.getControl("Control to disable").setDisabled(true);

    Good luck.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 229,963 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans