Requiring Fields at a Certain Opportunity Stage

Throughout the sales cycle, there may be certain fields that you would like to required by stage.  There are different ways to do this, for example changing record types by stage and having required fields or using validation rules to require fields if the stage is equal to a certain value.  Each way to do this has its own benefits and also its own negatives.  I had this need come up with a specific caveat, that it should be very easy for someone who is an admin be able to manage it with ease, as well as show users what fields would be required when the stage was selected.  My first thought was to use a Flow that would walk the users through the fields that would be required at that stage and at the end of the Flow, update the stage to the new value.  Although this sounded great, it would require someone to update a Flow to add new fields to the requirement.  Flows are great for requiring fields to be entered through required fields on a screen, but managing a change in a picklist value, with record types, as well as adding or removing required fields as the business dictates, makes a Flow a lot to manage.  Enter another level of Salesforce adminvelopment.  (Yes, I saw on the community someone posted, adminveloper, and adminvelopment is a byproduct of adminveloper.)

I didn’t want to have to use Apex code (test classes are no fun!) but I knew that using Flows wouldn’t be the right answer as a lot of changes to the Flow would be needed, such as adding a new field to the Fast Lookup, adding a new Required field to the Screen element, updating the picklist values of a Dropdown field in the Flow, and writing the appropriate assignment of new or removed fields.  So, I dabbled with Visualforce and a little bit of Javascript. (I promise, you can do this!!!)

I started with the first requirement, quickly and easily add or remove fields that were required by stage.  Field Sets are perfect for this!  I also knew that if a certain stage was selected, it should fail if the required fields weren’t selected.  Although Validation Rules are a great way to do this, it’s not very intuitive for the User, and they might save the record a few times and see the Validation Rule message each time until they have all the required fields filled in.  Although it works, it is not very User friendly.  So what is an Awesome Admin to do?!  Enter a hidden field, a Visualforce Page, and a little bit of Javascript!  I know, the answer isn’t a Flow.  I know, I’m a Flownatic.  I know, I’m not a developer.  But that’s ok, because this solution is perfect for an Adminveloper and helps a business to continue to develop their business flow!

First, let’s start with a simple field.  Create a field, visible from a field level security perspective, but hidden from a page layout.  In this case, we’ll call it “Closed Won Required Fields”.  It should be a checkbox field, visible to all profiles, that is hidden from a page layout.  Next, we’ll create a validation rule that says, “[Closed_Won_Required_Fields__c = False && TEXT(StageName) = “Closed Won”].  This will prevent a User from setting the Stage to Closed Won unless Closed_Won_Required_Fields__c is True.  Since the User can’t click the box (not visible in the page layout), the User will have to follow the required process.  For the Error Message of the Validation Rule write, [You can only set an Opportunity as Closed Won by clicking the Closed Won button.] and set it at the “Top of the Page”.  This way, if a User tries to set the Stage to Closed Won, it will fire the Validation Rule and let the User know they must click a button, “Closed Won”, to actually save the record as Closed Won.

Opportunity Validation Rule

This is where the fun begins!

First, create a new field set.  In this case, go to Setup, Customize, Opportunities, Field Sets.  Click New, set the name to “Closed Won Required Fields”, and enter information into the “Where is this used?” section, such as, “These fields will be required for Closed Won.”.  Click Save.  Next, add in the fields you would like into the Field Set, in this example, we’ll use Amount, Description, and Type.

Field Set

Click Save.

Now that we have our Field Set, lets create a new Visualforce Page.  Go to Setup, Develop, Visualforce Pages.  Click New.  Set the page name as “Required Closed Won Fields”  and make sure to change the spaces in the “name” section to “_” so that the Label reads “Required Closed Won Fields” and the Name reads “Required_Closed_Won_Fields”.

Required Closed Won Fields Start

Next, enter the following information (Sorry I couldn’t input the code directly, wordpress was giving me issues!):

Visualforce Page Required Fields Closed Won

The comments (everything between <!--  and -->) should explain what the page is doing, but to recap: The page is setting the Javascript function when the stage is changed, setting the hidden field to true if the stage is “Closed Won” and false if it is not.  This allows for the Validation Rule to be true if the User selects “Closed Won” since it will check the box and all the fields on the page will be required.  These are the same fields in the Field Set and can dynamically be added or removed through the field set without issue to the code.

Click Save.  Make sure to set the security settings so that every profile can see the page!

Now, let’s create a button.  Go to Setup, Customize, Opportunities, “Buttons, Links, and Actions”.  Click “New Button or Link”.  Set the name as “Closed Won”, the Display Type as Detail Page Button, Behavior as “Display in existing window without sidebar or header”, Content Source as “URL”,  and the formula to be “{!URLFOR(“/apex/Required_Closed_Won_Fields?id=”&Opportunity.Id,Opportunity.Id)}”.

Required Closed Won Fields Button

Click Save.  Add the button to your Page Layout.

Now, if your User tries to save the Opportunity as Closed Won through the normal page, they will receive an error, “You can only set an Opportunity as Closed Won by clicking the Closed Won button.”.  If they click the Closed Won button, it will take them to the Visualforce page with the fields that are required for setting the stage to Closed Won.  When the User moves the stage to “Closed Won” the Javascript will update the hidden field to True which will pass the validation rule and if the user changes the field from Closed Won to something else, it will set the field to False.

Any Admin can update the Field Set to include or remove fields that should be part of the required fields to save the Opportunity as “Closed Won”.

This is your full solution, requiring no Apex coding or test classes, to have a User be required to enter information in a certain stage, in this case “Closed Won”, in order to save a record.  Adminveloper for the win!!!

Let me know how this is working for you in the comments!

One thought on “Requiring Fields at a Certain Opportunity Stage

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s