Salesforce DAY Function

Do you want to learn the DAY function in Salesforce? In this Salesforce tutorial, you will see the syntax, usage cases, and examples of the DAY function in Salesforce.

This DAY function is used when you need to work on the day of the date in the Salesforce record. Let’s discuss how you can use the DAY function with real business examples in both Salesforce Lightning and Salesforce Classic.

DAY Function in Salesforce

The DAY function in Salesforce is one of the date and time category functions that is used to determine the day of the date field.

The main purpose of the DAY function is to extract the day component of the month from a given date. You can use this function in various scenarios, such as when you need to compute durations, filter records within a given day range, analyze data according to the day of the month, and many more cases.

The following is the syntax of the DAY function in Salesforce:

DAY(date)

Here, the date argument represents the date from which you wish to extract the day of the month.

If you talk about the return value of the DAY function, it will return the day of the month, ranging between numbers 1 and 31. So, it is important to note that it will always return a result in the “Number” formula return type.

Additionally, there is one consideration that the DAY function doesn’t work on date/time fields. To understand the DAY function working, let’s see an example.

DAY(Registration_Date__c)   #Here assume date is 04/24/2024 

According to this example, the function will return the result as 24 from the date “04/24/2024” (mm/dd/yyyy).

But if it is as below

DAY(Registration_Date__c)  #Here assume datetime is 3/11/2023, 10:23 AM

According to this example, the function will produce an error: “Error: Incorrect parameter type for function ‘DAY()’. Expected Date, received DateTime”.

With this, we have discussed the DAY function with its syntax, use case, examples, and consideration. Let’s move ahead and discuss how the DAY function is used in Salesforce Lightning.

How to use the DAY Function in Salesforce Lightning

Let’s have a scenario in which you want to check if a coupon expires within the current month and what is the day of expiration. To learn how to use the DAY function in the Salesforce Lightning formula field to solve this scenario, implement these steps:

1. Sign in to the Salesforce account as an administrator using the Lightning Interface. If you are not already on the main “Home Setup” page, click the “Gear” icon on the right side of the screen and select the “Setup” option from the dropdown menu.

See also  INCLUDES() Function in Salesforce

2. From the navigation bar, choose the “Object Manager” tab. To find the name of the object you wish to apply the DAY function, use the “Quick Find” box. As per the scenario, you need to search for the “Coupon” object.

Salesforce-DAY-Function.jpg

3. From the left sidebar, choose “Fields & Relationships”. Next, click on the “New” button located on the right to add a new field.

Salesforce DAY Function Example

4. To specify the information you wish to save, select the appropriate data type. As per the requirement, you need to select the radio button next to the “Formula” data type.

To proceed, click the “Next” button after that.

Salesforce Lightning DAY Function

5. Enter the “Field Label”; the “Field Name” will be automatically filled in based on the field label. As per the scenario, you can enter the field label “Coupon Expiry Day”.

Scroll down to the “Formula Return Type” section and select the data type from the list according to the formula’s specifications. According to the situation, you have to choose the “Text” data type.

To proceed to the next step, click the “Next” button.

Salesforce Lightning DAY Function Example

6. To create the formula with the function, click Advance Formula -> All Functions Categories -> Date and Time -> DAY -> Insert Selected Function.

According to the scenario, substitute the merger fields, expressions, or functions for the function’s parameters. For example, You want to check if a coupon expires within the current month and, if yes, show the day of expiration.

IF(
   MONTH(Expiration_Date__c) = MONTH(TODAY()) && YEAR(Expiration_Date__c) = YEAR(TODAY()),
   "Expires on" & TEXT(DAY(Expiration_Date__c)),
   "Expires in" & TEXT(MONTH(Expiration_Date__c) - MONTH(TODAY())) & " months"
)

Here is a detailed explanation of the formula:

  • MONTH(Expiration_Date__c) = MONTH(TODAY()) && YEAR(Expiration_Date__c) = YEAR(TODAY()): Here, we check if the month and year of the coupon’s expiration date match the current month and year.
  • IF() Function: We use the IF function to evaluate the expression and give the result on the basis of the condition:
    • If the expiration date is in the current month, The formula displays “Expires on” with the day of the month when the coupon expires.
    • If the expiration date is not in the current month, The formula displays “Expires in” followed by the number of months remaining until the coupon expires.

To check if the formula is correct or contains any errors, click the “Check Syntax” button. You will see the message “No Syntax error in merge fields or function”. If there is no error, if there is an error, an error will be displayed to you.

If you want, you can add additional information by filling out the “Description” and “Help Text” fields. Then, press the “Next” button to proceed.

DAY Function in Salesforce

7. To provide field-level security, choose the profiles to whom you wish to provide field edit access by checking the “Visible” checkbox and by selecting the checkbox next to the profile in the “Read Only” column; you can provide read-only access.

DAY Function in Salesforce Lightning

8. In this step, add the field to the page layout. However, if you don’t want to add the custom field on any of them, uncheck that page layout. After that, click on the “Save” button to complete the DAY() function in Salesforce Lightning.

See also  COS() Function in Salesforce | Calculate cosine value in Salesforce
DAY Function in Salesforce Lightning Example

Once the formula field is created, you will be able to use this formula field. Let’s see how it works:

  • Open the “Coupon” item, create a new coupon with the field “Expiration Date”, and save the coupon.
  • Move to the “Details Section”. Here, you will see the “Coupon Expiry Day” field with the output of the DAY function.

Let’s understand it with the example:

Suppose today’s date is “01/10/2024”, and the coupon is going to expire on “01/26/2024”, so it results as “Expires on 26”.

With this, we have discussed how to use the DAY function in Salesforce Lightning. Next, we will proceed and learn to use the DAY function in the formula field in Salesforce Classic.

How to use DAY Function in Salesforce Classic

Let’s explore how to use the DAY function and create a formula field in Salesforce Classic:

1. Login onto the Salesforce Classic account as an administrator. Then, choose the “Setup” option, which is located in the page’s upper right corner.

2. Next, look for the object on which you want to apply the DAY function using the “Quick Find” box on the left. Click the dropdown menu and choose “Fields” to begin the process of adding a new field.

For example, let’s suppose you choose the “Orders” object.

DAY Function in Salesforce Example

On the next page, move down to the “Custom Fields & Relationship” section and click on the “New” button.

Then, choose the data type as per the requirement. Here, you need to choose the “Formula” data type and to proceed ahead click on the “Next” button.

3. Enter the “Filed Label” and select “Field Name”; it will automatically fill in. For example, let’s say you enter the field label “Monthly Discount”.

Choose the appropriate data type for the return type by clicking the radio button next to the selected data type. According to the example, you need to choose the “Currency” data type.

Optionally, if you want, you can set up the “Decimal Places”.

DAY Function in Salesforce Classic

5. Select the appropriate function category from the “All Functions Categories”. Next, select the relevant functions and use the “Insert Selected Function” button to enter them.

Once the function has been entered into the advanced formula box, replace the parameters with the fields, operators, and functions to finish the formula.

For example, let’s suppose you want to calculate the monthly discount based on the day of the month. The formula is as follows:

CASE(
   DAY(Order_Date__c),
   1, Price__c * 0.02,
   15, Price__c * 0.01,
   31, Price__c * 0.005,
   0
)

The formula is described as given below:

  • DAY(Joining_Date__c): It is used to extract the day of the month from the order date.
  • CASE() Function: It is used to create conditional logic based on the day of the month. The conditions are as follows:
    • The bonus is computed as Price__c * 0.02 if the day is 1.
    • The bonus is calculated as Price__c * 0.01 if the day is 15.
    • The bonus is determined as Price__c * 0.005 if the day is 31.
    • In the event that none of the given requirements are met, the default outcome is 0.
See also  Salesforce TRIM Function

To check whether the formula contains any errors, click the “Check Syntax” option. If an issue arises, the error message will be displayed. If not, the message “No Syntax error in merge fields or function” occurs.

It is optional to provide “Description” and “Help Text” to give further information about the formula field. To proceed, click the “Next” button.

DAY Function in Salesforce Classic Example

6. You can select which profiles you wish to grant edit access to by granting field-level security. If you haven’t selected it, the field will be hidden from that specific profile.

Next, press the “Next” button to continue.

How to use DAY Function in Salesforce Classic

7. Choose the page layout where you want this field to appear; it won’t appear on any pages if you don’t choose a page layout.

Next, click the “Save” button to finish the formula field that uses Salesforce’s DAY function.

How to use DAY Function in Salesforce Classic Example

8. This formula field is now usable. Let’s go over the procedure:

  • Open the “Orders” object, create a new lead with the “Order Date” and “Price” and save it.
  • Move to the details section of the lead object and locate the “Monthy Discount” field that will give the result of the DAY function.

Let’s understand it with the example:

Suppose you create an order whose order date is “01/15/2024” and the price is “50000”. Execute the formula, you will get the monthy discount as “500”.

Example of Salesforce DAY function

With this, you have learned to use the DAY function in Salesforce Classic with an example.

Conclusion

In this article, we learned about the DAY function that is used to fetch the day from the date from 1 to 31. In addition, we discussed the use case, examples, syntax, and considerations.

Moreover, we have visualized the steps of using the DAY function in the formula field in Salesforce Lightning and Salesforce Classic.

You may like to read: