In this Salesforce tutorial, we will learn about one of the widely used Salesforce MONTH function. We will discuss the use case of the MONTH function in the Salesforce Lightning and Classic editions.
What is MONTH() function in Salesforce
In Salesforce, MONTH is a datetime function that returns the month, a number between 1 (January) and 12 (December) in the number format of a given date. The MONTH() function is helpful when we have to extract the month component from a date field.
This function is used in formula fields, validation rules, workflow rules, and other Salesforce automation features.
Syntax for the MONTH() function in Salesforce
The syntax for the MONTH function is as follows:
In the above syntax, the date is supposed to be removed by a field or expression from which you want to extract the month.
The considerations that we should know while using the MONTH function in Salesforce.
- In the expression of MONTH() function use a date or date/time value as its input.
- The MONTH() function is useful for extracting only the month component from a date.
- If you need both the month and year, you should consider using the CALENDAR_MONTH or functions.
Use MONTH() function in Salesforce Lightning
In this example, we will use the MONTH() function to define a custom formula field. This field will track the Cases associated with various projects and each task has a due date and the formula field will indicate whether the task is due in the current month.
Now log in to your Salesforce developer account and follow the steps below.
1. Navigate to the Setup page of Lightning and for that click on the settings icon that is on the upper right side then select the option Setup.
2. On the Setup page click on the Object Manager tab.
3. In the Object Manager select an object in which you have to create a custom formula field. In this example, I have selected the object Cases.
4. In the setup of the object click on Fields & Relationships then in the next window click on the New button.
5. In this step, select the custom type as Formula and click on the Next button.
6. In this window, we will define the output of the formula field by entering the following fields.
I have named the output Field Label “Case Month” and the File 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.
7. Enter the below formula in the formula editor.
MONTH(TODAY()) - 1, 'Due date is Passed',
MONTH(TODAY()), 'Due in Current Month',
MONTH(TODAY()) + 1, 'Due Date in Upcoming Months',
The above formula is defined as follows:
- The CASE function is used for conditional logic. It evaluates conditions and returns a result based on the first true condition.
- MONTH(Due_Date__c) extracts the month from the “Due_Date__c” field.
- If the month of the “Due_Date__c” field is equal to the current month -1, it returns the text ‘Due date is Passed‘.
- If the month of the “Due_Date__c” field is equal to the current month, it returns the text ‘Due in Current Month‘.
- If the month of the “Due_Date__c” field is equal to the current month +1, it returns the text ‘Due Date in Upcoming Months’.
- If none of the above conditions is true, the output will be “Not Due“.
6. After entering the above formula in the formula editor click on the button Check Syntax to check the error in the formula and on successful validation, click on the Next button.
7. In this step, we have to establish the field-level security for the custom formula field and for that select the profiles that have access to this field. To select all profiles, click on the Visible checkbox and click on the Next button.
8. Select the page layouts to make this field visible to the users on the page layouts. After selecting the page layouts click on the Save button.
As we click on the Save button the formula field will be saved in the object that you have selected in the steps above.
In this example, we have created the field in the Task object, so now we will open a task record to view the output in the created formula field.
In the above image, we can see the Due date is of the current month and according to this condition, the output in the field Case Month is Due in the Current month.
In this way, we have successfully executed the MONTH function in Salesforce Lightning with the help of the above steps by creating a custom formula field.
Use MONTH() function in Salesforce Classic
In this example we will create a custom formula field that will extract the month from the date when the Lead was created with this feature we can track the months when the Lead record was created.
Now to create a custom formula field, switch to the Salesforce Classic version and follow the below steps.
1. On the Home page of Salesforce Classic, click on the Setup tab.
2. On the Setup page, go to the left sidebar click on the Customize dropdown, and select Leads -> Fields.
3. In the Fields setup scroll down to the section Custom Fields and Relationships and click on the New button.
4. In this step, select the field type as Formula, and click on the Next button.
5. In this window, we will define the output of the formula field.
Entered a relevant Field label for the output field. For example, I have entered the Field Label as Lead Created in Month.
In the Formula return type, select the option Text and click on the Next button.
6. In this step, enter the below formula in the formula editor.
CASE(MONTH(DATEVALUE(CreatedDate)), 1, "January", 2, "February", 3, "March",4,"April",5,"May",6,"June",7,"July",8,"August",9,"Septempber",10,"October",11,"November",12,"December", "Other")
We have defined the above formula in the following way.
- DATEVALUE(CreatedDate) converts the CreatedDate field, which is a date/time field in Salesforce, into a date value.
- The Month function extracts the month component from the date value CreatedDate.
- The Case conditional checks that check the month obtained return a corresponding text value. For example, March for number 3.
- If the month doesn’t match any of the specified values (1 to 12), it returns “Other“.
7. After entering the formula validate it by clicking the button Check Syntax and on successful validation click on the Next button.
8. In this step, select the profiles that can have access to this field, and in case you want to give access to all profiles then activate the validate checkbox and click on the Next button.
9. In the last step, activate the page layouts to make this field visible on the page layouts then click on the Save button.
The custom formula field will be saved in the object that you have selected in the above steps. In this example, we have created this formula field in the Lead and now we will open a Lead record to view the output of the formula field.
As we can see in the above image the record was created on 26/05/2023 and the field Lead Created in month extracted the month number 5 and returns May in output according to the condition applied in the formula.
Now we have successfully created a Custom formula field in Salesforce Classic with the help of the MONTH function.
In this Salesforce tutorial, we have learned about the MONTH function and we also discussed a use case of the MONTH function where we created a custom formula field in the Lightning and Classic editions using the MONTH function.
Along with this, we have also learned about the Syntax and the Considerations that we should follow while using the MONTH function.
You may like to read:
- Salesforce NOW Function
- Salesforce TODAY Function
- Salesforce YEAR Function
- Salesforce SECOND Function
- Salesforce ISOWEEK Function
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.