In managing customer relationships with Dynamics CRM, it’s important to capture the right information at the right time. Sometimes, we need to make sure certain details are filled in before we close an Opportunity, like the Budget amount. However, simply making fields mandatory can be tricky because users might not know the info until they’re ready to close the Opportunity. Let’s explore how we can solve this problem effectively within Dynamics CRM.
Problem Statement
The Challenge of Making Fields Mandatory
- Customizing Fields: In Dynamics CRM, we can customize fields and make them mandatory. But when we mark a field as “required,” users have to fill it in before they can save any changes. This might not work well for fields like Budget amount, which users might not know until they’re ready to close the Opportunity.
- Issues with Business Process Flows: We can guide users through different stages with Business Process Flows. However, these flows might not cover all scenarios, and users could close an Opportunity without going through the flow, skipping required field prompts.
Need for Specific Information
- Capturing Important Details: When closing an Opportunity as won, it’s crucial to capture specific information, like the Budget amount. This data helps with analysis and forecasting. But asking for it too early could disrupt the sales process.
Budget amount is not required at the moment but we want the user to enter the value when the opportunity is closing as won.
To address the challenge of making specific fields required only when closing an Opportunity. It will show the error only when we are closing the opportunity as won or lost.
We will do this on the ribbon workbench. So, lets start from the start, First of all, I created a solution with opportunity entity without any other component. You may include the webresource in your solution but that is not required.
I created the solution opportunity ribbon and added the opportunity entity. If you want to learn more about the Ribbon work bench then you can see the tutorials here from the Scott Durow.
After creating the solution, I will connect the xrmToolbox with the environment and will load the solution.
Select the solution opportunity ribbon.
Right click the Close As Won button and copy
After copying, a new button will appear. A new button with the same name but a different ID will be displayed. You may also notice that the new button properties are not grayed out and we can open that for editing.
NOTE: We are copying the button because we are making changes to the out of the box button and by copying it will keep the original customization as backup.
Now, right click the copied button and click on Custom Command. It will take us to the command screen.
You can see here by copying the button, a new command is also copied. I have renamed that according to my requirements. You can also rename that according to your business.
When we browse to the command, we notice that a Custom Javascript Action is present with Library, Function Name and a Boolean Parameter. Take of note of these as we would need this again in our custom webresource later.
I also added the CRM parameter as PrimaryControl and removed the Boolean Parameter.
This Boolean value we will pass from JS code.
Now, I renamed the Id as well. Then added JavaScript action and function.
Now, go to the button and select the correct command. In our case it will be the new command that we have added. Please also correct the command core.
Once done Publish the changes.
After publishing the solution and refresh open your app and Open the Opportunity entity. Here we can see that two Close as Won buttons are on the ribbon (One copied and another OOB). Click on the copied button and my alert is working.
As you can see that there are two button on the ribbon and the field Budget Amount is not requited at the moment.
When we clicked the button, it showed us the field required error as expected.
Now, we need to hide one button from the ribbon. So, we will go back to the Ribbon Work Bench and will hide the OOB button.
After hiding the button, publish h the solution and refresh the opportunity record.
You can implement the logic on the close as Lost button as well in the same way.
Code:
Conclusion
By implementing targeted JavaScript logic, we effectively address the challenge of making specific fields mandatory when closing Opportunities in Dynamics CRM. This solution optimizes data capture processes, ensuring essential information is captured accurately while maintaining user flexibility during Opportunity management.