Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

HCLTech Microsoft Business Applications Practice Blog

for Dynamics 365, Power Platform and the rest of the Microsoft technology stack


How to Show/Hide a Button Using the Business Process Flow Stage

Post Author: HCLTech |

In today’s blog, we’ll discuss how to show and hide a button using the Business Process Flow (BPF) stage. In the example, we are going to hide the Close as Won  button for all BPF stages on the Opportunity entity except for the last stage, Opportunity Close, where we will show the Close as Won button. 

First, we will create one new record in the Opportunity entity. After creating the new Opportunity record, the Close as Won button is displayed.

To achieve the Show/Hide button functionality as per Business Process Flow (BPF) stages: 

Create a custom JavaScript in which we will check the BPF stage and accordingly we are showing and hiding the button. 

Go to the customizations and create one new solution and add an Opportunity form; also, create one new web resource for Javascript. 

JavaScript : 

 function closeAsWonCustomEnable(primaryControl) { 
try { 
var formContext = primaryControl; 
var isShowCloseAsWon = false; // Show/Hide Variable for "Close as won " Button 
//Get active process details 
var activeProcess = formContext.data.process.getActiveProcess(); 
var getActiveProcessName = activeProcess.getName(); 
//Get active Stage details 
var activeStage = formContext.data.process.getActiveStage(); 
var getactivestagename = activeStage.getName(); 
var state = formContext.getAttribute("statecode").getValue(); 
//If Opportunity is already Won then return false 
if (state !== 0) // 0 = Open 
return isShowCloseAsWon; 
//Check the BPF stage name 
if (activeProcess != null && getActiveProcessName == "Opportunity Sales Process") { 
if (getactivestagename === "Opportunity Close") { 
isShowCloseAsWon = true; 
return isShowCloseAsWon; 
} catch (e) { 
alert("An error has occurred : " + e.message) 


 1.2 Creating new web resource (JavaScript) 

Once JavaScript is created, open the ribbon workbench and select the solution that contains the Opportunity form.  

 1.3 Customize the “close as won” button on form. 

Select the button and click Customise command. Next, create a new Enable Rule and add CustomRule inside that. Use below property for CustomRule: 

Default : True , invertResult="" , funcationName: closeAsWonCustomEnable ,Javascriptname : new_ showhidebutton 

Once you create a new Enable Rule, add that rule inside “Close as won” button command and publish.

 1.4 Create new enable rule for “close as won” button and add that to “close as won” button command. 

After publishing the custom rule for Close as Won button, we will go to the Opportunity entity and create one new record. 

Once we create a new Opportunity record, we can see in the below screenshot that the Close as won button is not showing: 


Now, move to the next stage, and when the last BPF stage arrives for this opportunity (BPF last stage: Opportunity Close) then the JavaScript (new_showhidebutton) will return TRUE and Close as Won button will show:

 1.6 “Close as won” button showing on last BPF stage. 

And that’s it! Happy D365’ing!

By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

PowerObjects Recommends