Creating a mandatory Reason Lost field for lost opportunities using a validation rule
In this recipe, we will create a conditional validation rule that requires the entry of the reason of why an opportunity has been lost.
The reason lost is a custom field (which we created as Reason_Lost__c) and is first created using the steps of this recipe.
Getting ready
Creating a custom Reason Lost field on the opportunity:
Navigate to the Opportunity Fields setup page, by going to Your Name | Setup | Customize | Opportunities | Fields.
Note
Scroll down to the Opportunities Custom Fields & Relationships section.
Click on New.
Note
You will find the Step 1. Choose the field type page.
Choose Picklist from the Data Type options.
Click on Next.
Note
The Step 2. Enter the details page is presented.
Type Reason Lost in the Field Label textbox (as shown in the screenshot).
Accept the default Reason_Lost in the Name field.
Type these example values into the pick list field:
No Budget
Missing Product Features
Better Price
Cost / Value
Click on Next.
Note
Next, you will see the Step 3. Establish field-level security page.
Click on Next.
Select the profiles to which you want to grant edit or view access to this field via field-level security. The field will be hidden from all profiles if you do not add it to field-level security.
Click on Save.
Note
Next, you will see the Step 4. Add to page layouts page.
Click on Save.
To create the composite validation rule, we need to ensure that the custom field (Reason_Lost__c) has been completed whenever the opportunity has been set to Closed Lost by following these steps:
How to do it...
Carry out the following step to create a validation rule:
Navigate to the opportunity customization setup page, by going to Your Name | Setup | Customize | Opportunities | Validation Rules.
Click on New.
Note
Here, we are presented with the Opportunity Validation Rules edit page.
Type Opportunity Reason Lost in the Field Label textbox (upon clicking out of the text box, the name changes to Opportunity_Reason_Lost).
Leave the Active checkbox ticked.
Optionally, set the Description field to This validation rule is for ensuring Reason Lost is completed when Opportunity is Lost.
Note
Navigate to the Error Condition Formula section (as shown in the upcoming screenshot).
In the Error Message section, enter the text Please select an option for the Opportunity Reason Lost field.
In the Error Location option, select Field.
Note
Here, we are presented with the Field selection pick list.
Select the field Reason Lost (as shown):
Finally, click on Save.
How it works...
You can see how the setting of the opportunity sales stage changes to Closed Lost and the attempted save then fires the validation rule.
Here, the user must enter the reason that the opportunity was lost in the custom field created in this recipe called Reason Lost as shown in the following screenshot: