Requirement
Recently, We worked on an interesting requirement in Dynamics 365 CE Sales app where the customer wanted to close an Opportunity as WON directly when the user clicks the final stage in the Business Process Flow (BPF).
However, the requirement was not as simple as just closing the Opportunity.
There were a few important business validations that we had to enforce before allowing the Opportunity to close.
In this blog, I’ll walk you through the requirement, the challenges, and the full solution I implemented.
Details
When the user reaches the final stage of the Opportunity BPF and clicks Finish, the system should:
- Check several combination of conditions before closing the Opportunity:
- No Draft Quotes should exist.
- No Active Quotes should exist.
- At least one Quote must be Won.
- If any validation fails → show an error message and do NOT close the Opportunity.
- If all validations pass → automatically close the Opportunity as WON.
- After the Opportunity is closed → show a friendly message on the form:
“Congratulations! Your opportunity is won.”
To meet this end-to-end behavior, we implemented two components:
Part 1 – Plugin on BPF Stage Change
The first part was handled through a plugin because it needed server-side validation and automatic Opportunity closing, which cannot be trusted to the client side.
Trigger Point
- Message:
Update - Primary Entity:
Opportunity Sales Process (BPF entity)
In the plugin, we added multiple validation checks. If any check failed, the plugin threw an execution exception. When all conditions were met, the plugin automatically closed the opportunity as WON.
Part 2 – JavaScript
Once the plugin automatically closes the Opportunity, we wanted to show a “Congratulations” message to the user on the form.
However, showing a message after the Opportunity is closed is a bit tricky because:
- The BPF updates before the form refreshes.
- We need to ensure the Opportunity is already closed as WON.
- The stage change event fires earlier than the record update.
To solve this, we implemented a simple form script.
JavaScript Logic
- Bind the script to the BPF Stage Change event.
- Used timer, because the stage change event occurs before the Opportunity status updates.
- After a short delay, check the Opportunity:
- If status is “Won”
- And the BPF was just finished
- Then show a message
Summary
This is how we handled this requirement and used two solutions which involved combining Plugin and JavaScript.
Hope it will help someone!!
Keep learning and Keep Sharing!!
