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.
Great. Way number 1 worked.
Thank you so much.
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.
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?
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);
}
});
This would only disable the status field. I want to disable all fields on the form, so that no further changes can be done.
Try to replace line
formContext.getControl("Control to disable").setDisabled(true);
with line
formContext.getControl("new_status").setDisabled(true);
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.
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
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.
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.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,151 Super User 2024 Season 2
Martin Dráb 229,963 Most Valuable Professional
nmaenpaa 101,156