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:

  1. 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.

  2. Click on New.

    Note

    You will find the Step 1. Choose the field type page.

  3. Choose Picklist from the Data Type options.
  4. Click on Next.

    Note

    The Step 2. Enter the details page is presented.

  5. Type Reason Lost in the Field Label textbox (as shown in the screenshot).
  6. Accept the default Reason_Lost in the Name field.
  7. Type these example values into the pick list field:
    No Budget
    Missing Product Features
    Better Price
    Cost / Value
    Getting ready
  8. Click on Next.

    Note

    Next, you will see the Step 3. Establish field-level security page.

  9. Click on Next.
  10. 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.
  11. Click on Save.

    Note

    Next, you will see the Step 4. Add to page layouts page.

  12. 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:

  1. Navigate to the opportunity customization setup page, by going to Your Name | Setup | Customize | Opportunities | Validation Rules.
  2. Click on New.

    Note

    Here, we are presented with the Opportunity Validation Rules edit page.

  3. Type Opportunity Reason Lost in the Field Label textbox (upon clicking out of the text box, the name changes to Opportunity_Reason_Lost).
  4. Leave the Active checkbox ticked.
  5. 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).

  6. Paste the following code:
    AND(
      NOT(IsWon),
      IsClosed, 
      ISBLANK(TEXT(Reason_Lost__c))
    )
  7. In the Error Message section, enter the text Please select an option for the Opportunity Reason Lost field.
  8. In the Error Location option, select Field.

    Note

    Here, we are presented with the Field selection pick list.

  9. Select the field Reason Lost (as shown):
    How to do it...
  10. 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:

How it works...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.186.46