Salesforce Date Range Formula

While working for an organization that uses Salesforce, I was assigned a task to find all opportunities that were created in the last 30 days. So, to fulfill this requirement, I define an advanced formula that checks the date range.

Using a Date Range Formula, the user can automatically check if the opportunity creation date is within the last 30 days. In this Salesforce tutorial, we will learn the Salesforce Date Range Formula.

How to Use Date Range Formula in Salesforce

In Salesforce, we can track various activities, such as renewal dates, delivery dates, birthdays, and conversion dates, and for all these activities, we use date fields.

The Date field is the Salesforce data type used to store and record dates. So, to check if a date falls within a specific range, we need to define the date range formulas in Salesforce.

To check if a date falls within a specific range, we utilized the AND and OR functions with comparison operators.

Let’s understand this concept with the help of an example:

Suppose we are working as a sales manager, and we are responsible for monitoring leads.

We want to verify that the Lead Status is “Open – Not Contacted“. If the Start Date is between the first day of the previous month and the last day of the previous month, the Formula returns the text “Lead Opened Last Month“.

So, for this, we create an advanced formula given below:

IF(AND(ISPICKVAL(LeadStatus, "Open - Not Contacted"), StartDate >= DATE(YEAR(TODAY()), MONTH(TODAY())-1, 1), StartDate < DATE(YEAR(TODAY()), MONTH(TODAY()), 1)), "Lead Opened Last Month", "")

With this, we now know about the Salesforce Date Range Formula. Next, we will proceed to learn how to check if a date falls within a specific range using Salesforce Lightning.

Check if the Date Falls Within Range Using Salesforce

Here are the steps to define the Salesforce date range formula in Salesforce Lightning:

  1. Click the Setup icon in the top right corner of the page to access Salesforce Setup.
    • Click Object Manager from the menu’s left side.
Salesforce Date Range Formula
  1. Look for the item to which we want to check if the date falls within the range. I chose the Product object from the list of objects in this instance.
Salesforce Date Range Formula Example
  1. Choose Fields & Relationships. Next, select New.
Check if Date falls within range using Salesforce Lightning
  1. Choose the Formula as the field type. Click Next to proceed.
Check if Date falls within range using Salesforce Lightning Example
  1. After entering a Field Label for the formula, the Field Name will be filled in automatically. Here, I enter the Field Label as Delivery Charges.
    • Select Currency as the Formula Return Type. Here, I also set its decimal place to 0. After that, click Next.
Check if Date falls within range in Salesforce Lightning
  1. Enter the formula that checks if the date falls within the range. In the Advanced Formula editor. Here is the formula:
IF(
AND(
ISPICKVAL( Delivery_Tracking__c , "Delivered"),
Payment_Date__c <= Delivery_Date__c + 7,
Payment_Date__c >= Delivery_Date__c
),
0,
100
)
  • This formula determines the delivery charge for an order based on three criteria, which are as follows:
    • The delivery tracking status is now set to ‘Delivered’.
    • The payment date is on or after the delivery date.
    • The payment date is within 7 days after the delivery date.
  • Firstly, we use the IF() conditional function to determine whether these three conditions are true or false and return a value based on the result.
  • Next, we use the logical function AND(), which returns true if each of the defined requirements is satisfied.
  • Then, we use the ISPICKVAL() function, which determines whether the picklist field for Delivery_Tracking__c includes the value “Delivered”.
  • Next, we define the date range to check if the payment date is on or before 7 days after the delivery date: “Payment_Date__c <= Delivery_Date__c + 7”.
  • After this, we define the date range to check if the payment date is on or after the delivery date: “Payment_Date__c >= Delivery_Date__c”.
  • If all three conditions are met, the formula returns a delivery charge of 0. Otherwise, it returns a delivery charge of 100.
  1. To verify that the formula is valid, click Check Syntax. We can specify how you want to handle the empty field and include the Description and Help Text as alternatives. Then, select Next.
Check if Date falls within range in Salesforce Lightning Example
  1. Select the Field Level Security to make the field visible to the profiles.
How to check if date falls within range using Salesforce Lightning
  1. Select the Page Layout Name for which the formula field is to be added. Click Save to save the formula field.
How to check if Date falls within range using Salesforce Lightning Example

We can use the formula field once it has been created. Let’s look at an example:

  • Create a Product with the Delivery Date, Payment Date, and Delivery Tracking fields by opening the Products item and saving it.
  • After that, click on the Details section, and the Delivery Charges field appears, which checks if the date falls within the specified range and displays the result.
Salesforce Example Date Range Formula
Date Range Formula when Payment Date and Delivery Date are equal.
Salesforce Lightning Date Range Formula Example
Date Range Formula Example: when the Payment Date is less than the Delivery Date.
Salesforce Lightning Date Range Formula
Date Range Formula when the Payment Date is within 7 days of the Delivery Date.

As a result, we now know how to utilize Salesforce Lightning’s advanced formula to determine if the date falls within the specified range.

Conclusion

We have learned the advanced Salesforce formula to check if a date falls within a specified range using Salesforce.

Moreover, we have learned that Salesforce Lightning offers a straightforward method to determine if a date falls within a specific range.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.