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 365 | Integration, Dataverse...
Answered

Adding custom action to a button in Ribbon workbench

(0) ShareShare
ReportReport
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. 

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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.

  • Aneeqa Pervaiz Profile Picture
    240 on at

    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.

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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
    240 on at

    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

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Try to replace line

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

    with line

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

  • Aneeqa Pervaiz Profile Picture
    240 on at

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

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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
    240 on at

    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
    84,331 Most Valuable Professional on at

    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
    240 on at

    Great. Way number 1 worked.

    Thank you so much.

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 365 | Integration, Dataverse, and general topics

#1
iampranjal Profile Picture

iampranjal 41

#2
Martin Dráb Profile Picture

Martin Dráb 36 Most Valuable Professional

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans