Salesforce YEAR Function

In this Salesforce tutorial, we will learn about the YEAR function in Salesforce, a widely used Date/Time function. This YEAR function can be used in formula fields and Salesforce Lightning and Salesforce Classic validation rules.

What is YEAR() function in Salesforce

In Salesforce, the YEAR function is used to extract the year component from a date or datetime field. It is commonly used in Salesforce formulas, validation rules, workflow rules, and other similar cases where we need to perform calculations, comparisons or create a formula based on the year.

The output of the YEAR function returns the four-digit year in the number format of a given date. This four-digit is a year of date, and it returns the number between 1900 and 9999. For example, if the date field is “21/12/2024” the output will be 2024.

Syntax for YEAR() function

The syntax of the YEAR function is written in the following way:

YEAR(date)

In the above syntax, the date value is supposed to be replaced by the date or datetime field from which you want to extract the year.

Considerations:

The considerations that we should be aware of while using the YEAR function in Salesforce.

  • In the expression value of the YEAR() function use a date parameter.
  • If the date field is null, then it’s important to handle null values appropriately to avoid formula errors.
  • The YEAR function will return the year based on the time zone of the user executing the formula.

How to use YEAR function in Salesforce Lightning

In this example, we will use the YEAR function to create a custom formula field that will track the opportunities that are won and closed in the current year.

We will define a condition in the formula that if the opportunity is won and closed in the current year the output will be “Won and Closed in the current year” else the output will be “Opportunity Lost or not closed in Current Year“.

To create a custom formula field in Salesforce Lightning, log in to your developer account and follow the below steps.

1. To navigate on the Setup of Salesforce Lightning, click on the Settings icon that is on the upper left side of the window and then select the option Setup.

2. On the Setup page of Lightning, click on the Object Manager tab.

3. In the setup of Object Manager, click on the Opportunity object.

4. In the setup of the Opportunity object, click on the fields and relationships on the left sidebar then click on the New button to create a field.

5. Select the Data type as Formula for the new field then click on the Next button.

6. In this step, enter the relevant name for the output of this field and for that, I have named the Field Label as Opportunity Status. The Field Name will be auto-filled according to the entered Field Label.

In the Formula Return Type select the option Text and click on the Next button.

Salesforce Lightning Formula field with YEAR function

7. In this window, we will enter the formula below in the formula editor to define the formula field.

IF(
    AND(
        ISPICKVAL(StageName, 'Closed Won'),
        TEXT(YEAR(CloseDate)) = TEXT(YEAR(TODAY()))
    ),
    "Current year Won Opportunity",
    "Opportunity is Lost or doesn't belong to the current year"
)

We have defined the above formula in the following way.

  • The IF conditional checks a given condition and returns one value if the condition is true, and another value if the condition is false.
  • The AND function checks that both conditions are true.
  • ISPICKVAL(StageName, ‘Closed Won’) checks if the Stage field of the opportunity is set to ‘Closed Won’.
  • TEXT(YEAR(CloseDate))= TEXT(YEAR(TODAY())) checks if the year of the CloseDate field is the same as the current year. It uses the YEAR function to extract the year from both the CloseDate and the current date and compares them.

8. After entering the formula in the formula editor, click on the button Check Syntax to validate the formula and on successful validation, click on the Next button.

Salesforce Lightning Custom Formula field with YEAR function

9. In this window, we have to select the profiles that can have access to or view this field. Here, we select specific profiles, or we can also select all profiles by activating the Visible checkbox.

After selecting the profiles, click on the Next button.

Year function formula field in Salesforce Lightning

10. In the last step, activate the page layouts to make this field visible on the page layouts, then click on the Save button.

Custom formula field from Year function in Salesforce

The created formula field will be saved in the Opportunity object, and now we will open an opportunity to view the output of the custom formula field we created.

Salesforce Lightning Year function output field

As we can see, the output field in the above image is showing the Opportunity Status as the Current year Won Opportunity. Since this opportunity was Won and closed in the current year and according to the condition defined in the formula, we got this output.

In this way, we have successfully created a custom formula field with the help of YEAR function in Salesforce Lightning.

Use YEAR function in Salesforce Classic

In this example, we will use the YEAR function to create a custom formula field in the CASE object that will track the CASES if the case belongs to the current year or previous years.

The output field will display “Current Year” for cases created in the current year and “Previous Year” for cases created in the previous year.

Now follow the below steps, to create a custom formula field in Salesforce Classic.

1. Navigate to the setup page, and for that, click on the Setup tab that is on the upper left side of the window.

2. In the Setup window, go to the left sidebar and click on Customize dropdown, then select Cases -> Fields.

Salesforce Classic Custom Formula field with YEAR function

3. In the Fields setup window, scroll down to the Custom Fields and Relationships section and click the New button.

New field to execute the YEAR function in Salesforce

4. In this step, select the Field data type as Formula and click on the Next button.

Create a custom formula field in Salesforce using Year function

5. In this window, we will define the output of the custom formula field by entering the below field.

Enter the output field name in the Field Label, like I have named it as Case Year and the Field Name will be auto-filled according to the entered Field Label.

Select the Formula return type as Text and click on the Next button.

Salesforce Classic YEAR function use case

6. In this step, we will enter the below formula in the formula editor.

CASE(VALUE(MID(TEXT(CreatedDate), 1, 4)), 
    YEAR(TODAY()), 'Current Year', 
    YEAR(TODAY()) - 1, 'Previous Year', 
    'Other Year'
)

The conditions defined in the formula are as follows:

  • In the expression, the CASE function evaluates multiple conditions in order and returns a result when a condition is True.
  • The Value function converts the extracted text back into a numeric value.
  • MID(TEXT(CreatedDate), 1, 4) extracts the first four characters from the text, that will return the year of CreatedDate.
  • YEAR(TODAY()) will return “Current Year” in the output if the extracted year matches the current year.
  • YEAR(TODAY()) – 1 will return the output “Previous Year” if the extracted year matches the previous year.

7. Now click on the button Check Syntax to validate the formula, and on successful validation, i.e., no error in the formula, then click on the Next button.

Year function syntax in salesforce

8. In this step, select the profiles that can have access to view this field, we can either select specific profiles or select all profiles by activating the Visible checkbox and, after this click on the Next button.

Setup Custom formula field of Year function in Salesforce

9. In the last step, activate the page layouts on which you want to make this field visible, then click on the Save button.

Salesforce Classic formula field with Year function

As we click on the Save button, the field will be saved in the Case object. Now open a case record to check the output field that we have created. We will see the output field on the record page.

Salesforce Classic YEAR function Output field

As we can see in the above image, the formula field returns the output Current year since the Case was created in the current year, and according to the condition applied in the formula, we got this output.

In this way, we have successfully executed the YEAR function in Salesforce Classic by creating a custom formula field with the YEAR function.

Conclusion

In this Salesforce tutorial, we have learned about the YEAR() function in Salesforce and discussed a use case where we created a custom formula field with the help of the YEAR function in Salesforce Lightning and Classic editions, respectively.

Along with this, we also learned about the YEAR() function uses, its syntax, and the conditions that we should follow while executing the YEAR() function in Salesforce.

You may like to read: