Salesforce DAYOYEAR Function

In this Salesforce tutorial, we will learn the Salesforce DAYOFYEAR function. Additionally, we learn the syntax, use cases, and examples of the function.

Furthermore, we will discuss how to use the DAYOFYEAR function in Salesforce Lightning and Salesforce Classic with the help of business-related examples.

What is DAYOFYEAR Function in Salesforce

The DAYOFYEAR function of Salesforce is used to manage and work with the date-related data in Salesforce. The Salesforce DAYOFYEAR function is used to fetch the day of the year from the given date.

The syntax of the function is as given below:

DAYOFYEAR(date)

Here, the date parameter represents the date for which you want to determine the day of the year. And, the function will return the day of the calendar year from 1-366.

If we talk about the use cases of the DAYOFYEAR function, it can be used to perform various operations on the day of the year, and we can also use it to track the day of the year.

Furthermore, one consideration of the DAYOFYEAR is that it can accept only the date data type field as the parameter. If you pass the date/time data type field as the parameter, it will produce an error: Incorrect parameter type for function ‘DAYOFYEAR()’. Expected Date, received DateTime.

Let’s move ahead and understand the function with the examples:

INPUTOUTPUT
DAYOFYEAR(CreatedDate) Suppose here CreatedDate is 30 June 2024182
DAYOFYEAR(CreatedDate) Suppose here CreatedDate is 15 MArch 202575

Hence, we can say that the DAYOFYEAR function extracts which day of the year the date is, and the results are between 1 and 365 or 366, depending on the year.

With this, we have covered the overview of the Salesforce DAYOFYEAR function. Now, let’s move and learn how the function works in Salesforce Lightning.

How to use the DAYOFYEAR Function in Salesforce Lightning

Let’s learn how the DAYOFYEAR function works in Salesforce Lightning with the help of a scenario in which we want to check if the lead record was last modified in the first half of the year or the second half of the year.

1. Login to your Salesforce account as an admin and redirect yourself to the “Main Home Stup” page of Lightning Interface.

After that, click on the “Object Manager” tab at the top of the navigation bar. Find the object in the “Quick Find” box on whose field you want to use the DAYOFYEAR function in Salesforce.

According to the scenario, we need to choose the “Lead” object.

Salesforce DAYOYEAR Function

2. Now, choose the “Fields & Relationships” option from the left sidebar of the page, and after that, click on the “New” button on the right of the page.

Salesforce DAYOYEAR Function Example

3. In this step, we need to select the data type to specify the information that the field will contain. According to the requirement, you need to choose the “Formula” data type.

See also  Salesforce ISNEW Function

To proceed, click on the “Next” button.

Salesforce Lightning DAYOYEAR Function

4. Enter the “Field Label” for the field, and the “Field Name” will be automatically populated based on them. After that, choose the data type as per your formula return type by checking the radio button next to the data type.

According to the scenario, we can enter the field name as “Last Modified Status” and choose “Text” as the return data type. Then, click on “Next” to proceed.

Salesforce Lightning DAYOYEAR Function Example

5. Now, create the formula using the DAYOFYEAR year function that categorizes the lead records based on whether they were last modified in the first or second half of the year.

For this, follow the steps: Advance Formula -> All Functions Categories -> Date and Time -> DAYOFYEAR -> Insert Selected Function.

The formula is as given below:

IF(DAYOFYEAR( DATEVALUE( LastModifiedDate )) < 183, "First Half", "Second Half")

A detailed description of the formula is given below:

  • DAYOFYEATR(): It is used to extract the day of the year for the ‘LastModifiedDate’ field.
  • DATEVALUE( LastModifiedDate ): It is used to fetch only the date part from the date and time data field ‘LastModifiedDate’.
  • DAYOFYEAR(LastModifiedDate) < 183: It checks whether the day of the year for the last modified date is less than 183.
  • IF() function: It is used to evaluate the condition and give the result accordingly:
    • If the day of the year is less than 183, it returns “First Half”.
    • If the day of the year is more than 183, it returns “Second Half”.

After entering the formula, click on the “Check Syntax” button to check your formula wheater it is error-free or contains the errors. If we want, you can add the “Description” and “Help Text” also.

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

DAYOFYEAR Function in Salesforce

6. By using field-level security, choose which profiles you wish to allow edit access to this field. If you do not add the field to field-level security, it will be hidden from view in every profile.

Click on the “Next” button and proceed.

DYAOFYEAR Function in Salesforce Example

7. Choose the page layout on which you want to include this field. If you have not selected the page layout the field will not appear on that. Then, click on the “Save” button to complete the process of creating a formula field with the DAYOFYEAR function.

DAYOFYEAR Function in Salesforce Lightning

Now, we can check the formula how it is working:

  • Create a new lead by opening the “Leads” item and saving it.
  • Go to the section labeled “Details”. The output of the DAYOFYEAR function is displayed in the “Last Modified Status” field here.
See also  CASE Function in Salesforce

Let’s understand it with the example:

Suppose you last modified the lead on “01/11/2024”, so it will show the last modified status field as “First Half”.

DAYOFYEAR Function in Salesforce Lightning Example

We have now covered how to utilize Salesforce Lightning’s DAY function. We will next go ahead and learn how to utilize Salesforce Classic’s DAY function in the formula field.

How to use DAYOFYEAR Function in Salesforce Classic

With the help of a scenario situation where we wish to create the reminder depending on the difference between today’s and the account anniversary date, let’s explore how Salesforce Classic’s DAYOFYEAR function works according to it.

1. As an administrator, log onto the Salesforce Classic account. Next, select the “Setup” option found in the upper right corner of the page.

Salesforce Classic DAYOFYEAR Function

2. Next, use the “Quick Find” box on the left to locate the object to which you wish to apply the DAYOFYEAR function. To start the process of adding a new field, click the dropdown menu and select the “Fields” option.

According to the scenario, search for the “Account” object.

Salesforce Classic DAYOFYEAR Function Example

After that, scroll down to the “Custom Field & Relationship” section and click on the “New” button.

3. Now, choose the “Formula” as the data type to specify the information that we are storing. Then, click on the “Next” button to proceed.

DAYOFYEAR Function in Salesforce Classic Example

4. Enter the display name of the field in the “Field Label” and then click on the “Field Name”. It gets populated automatically.

Here, we can enter the field label as “Anniversary Reminder” as per the scenario.

Now, from the list of data types, choose the formula return data type that suits your formula requirement. In accordance with our scenario, we need to choose the “Text” data type.

How to use DAYOFYEAR Function in Salesforce Classic

5. To create the advanced formula, select the “Advanced Formula” option. Click the “All Functions Categories” dropdown menu from the “Functions” section and choose the necessary function category.

For instance, choose the “Date and Time” category since you require the “DAYOFYEAR” function. Next, look through the category for the necessary function and select it.

Once you select the function, you will get the “Insert Selected Function” button under the function section; click on it. Next, replace the parameters of the function with the merger fields, expressions, or functions as per the requirement of the scenario.

According to the scenario, we want to display whether the anniversary is today, tomorrow, or upcoming. The formula for the scenario is given below:

CASE(
    DAYOFYEAR(Anniversary_Date__c) - DAYOFYEAR(TODAY()),
    0, "Anniversary Today",
    1, "Anniversary Tomorrow",
    "Upcoming Anniversary"
)

Here is the detailed description of the formula:

  • DAYOFYEAR(Anniversary_Date__c): It is used to extract the day of the year for the anniversary date of the account.
  • DAYOFYEAR(TODAY()): It is used to extract the day of the year for the current date.
  • DAYOFYEAR(Anniversary_Date__c) – DAYOFYEAR(TODAY()): Here, the difference between the day of the year of the anniversary date and the current date is calculated.
  • CASE() Function: The CASE function is used to evaluate the result based on the conditions met.
    • If the result is equal to 0, the output will be “Anniversary Today”.
    • If the result is equal to 1, the output will be “Anniversary Tomorrow”.
    • If none of the conditions are met, the output will be “Upcoming Anniversary”.
See also  Salesforce FROMUNIXTIME Function

Click on the “Check Syntax” button to check whether the formula is correct or has some errors. Optionally, you can enter the “Description“, and “Help Text“.

After that, click on the “Next” button to proceed.

How to use DAYOFYEAR Function in Salesforce Classic Example

6. Choose the profiles to whom you want to provide field edit access in order to grant field-level security. To allow access, tick the box next to each profile name in the “Visible” or “Read Only” column.

Click on the “Next” button to move to the next step.

How to use DAYOFYEAR Function in Salesforce

7. In this step, add the field to the page layout.  But if you don’t want to add the custom field to any one of the selected page layouts, unselect that page layout.

Click on the “Save” button to complete the process of using the DAYOFYEAR() function in the Salesforce Classic formula field.

How to use DAYOFYEAR Function in Salesforce Example

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

Open the “Accounts” item, open up an account, and move to the “Details” section. Here, you will find the “Anniversary Reminder” field with an output of the DAYOFYEAR function.

Let’s understand it with the example:

Suppose we have opened an account having an anniversary date “01/12/2024”. After the execution of the formula, we will get the output for the anniversary reminder as “Anniversary Tomorrow”.

Example of DAYOFYEAR Function in Salesforce

With this, we have learned how to use the DAYOFYEAR function in Salesforce Classic.

Conclusion

With this, we have learned about the DAYOFYEAR function in Salesforce. Additionally, we have covered the syntax, use case, examples, and considerations.

Moreover, we have learned step by step how to use the DAYOFYEAR function in Salesforce Lightning and Salesforce Classic in the formula to return the day of the calendar year from 1 to 366.

You may like to read: