Salesforce ADDMONTHS Function

In this Salesforce tutorial, you will learn the Salesforce ADDMONTHS function. You will also learn the basic syntax of the function and multiple examples demonstrate use cases of the function in Salesforce.

Do you face any challenges where you want to add specific months to date in Salesforce quickly and efficiently?

If yes, Salesforce provides us with a function known as ADDMONTHS() that works with date and time data within the platform. The function is used to add the number of months to the date.

Moreover,  you will explore the steps of using the ADDMONTHS function in Salesforce Lightning and Salesforce Classic with the real-based scenario.

What is Salesforce ADDMONTHS Function?

The Salesforce ADDMONTHS() function is one of the Salesforce DATETIME functions that is used to perform calculations with date and time data within the Salesforce platform.

The ADDMONTHS function is used to add or subtract a specified number of months to a given date or datetime value. It is best suited in scenarios where you want to predict future dates, handle subscription renewals, or figure out when things will be delivered.

In addition, if the specified date is the last day of the month, the resulting date is the last day of the month. Otherwise, the result has the same date component as the specified date.

The following are the considerations that you must take care of while selecting the formula input type and return type:

  • If you choose the formula input type as a date value make sure that the return type must be the date data type. If you choose the return type as the date/time you will get an error.
  • If you choose the formula input type as a date/time value you can choose the return data type both date and date/time.

Now that you have learned the basics of the ADDMONTHS function, let’s move to the next section and take a look at the syntax of the ADDMONTHS function in Salesforce.

Syntax of the ADDMONTHS Function

The syntax of the Salesforce ADDMONTHS() function is straightforward, easy to understand, and use. The syntax is as given below:

ADDMONTHS(date,num)

Here:

  • date: The date is the parameter that specifies the date value or datetime value to which you want to add months.
  • num: The num parameter specifies the number of months you want to add or subtract from the date or date/time value.

Return: It returns the date with a certain number of months before or after a given date. If the given datevalue -> last day of month -> result is last day of the month. Otherwise, the result will have the same day as the given date.

Now, you have covered the syntax of the ADDMONTHS function. Let’s move to the next section and see some simple examples of the function and understand how the function works in practical situations.

Examples of Using ADDMONTHS

The table given below shows simple examples of how the ADDMONTHS functions work in Salesforce.

The “Input Field” column shows the date or date/time expression entered by the user and the “Return” column displays the output returned by the ADDMONTHS function in Salesforce.

Input FieldOutput of ADDMONTHS() Function
5/17/2023If added 2 months result = 7/17/2023
5/17/2023If subtracted 2 months result = 3/17/2023
10/27/2023, 12:46 AMIf added 5 months result = 3/27/2023, 12:46 AM
10/27/2023, 12:46 AMIf subtracted 5 months result = 5/27/2023, 12:46 AM
10/31/2023If added 1 month result = 10/30/2023. Because 10/31/2023 is the last day of the month it will return the last day of the month of the resultant month. In this case, the result month’s last date is 10/30/2023 therefore it result is 30/10/2023.

With this, you have learned how the ADDMONTHS function works with examples. Let’s proceed to the next section and learn the step-by-step procedure of using the ADDMONTHS function in Salesforce Lightning with a scenario-based example.

See also  PI() Function in Salesforce

How to use ADDMONTHS function in Salesforce Lightning

You can use the ADDMONTHS() function in Salesforce Lightning in various cases. One such case can trial period for the product. You can use the function to calculate the trial period to give someone a free period of the product.

Below are the step-by-step implementation of how to use the ADDMONTHS function to achieve the above-defined scenario in Salesforce Lightning.

1. Signin to the Salesforce account in the Lightning Interface. By default, you will be on the home page. To redirect yourself to “Home Setup”, click on “Gear Icon” -> “Setup (Setup for current app)”.

Salesforce ADDMONTHS Function

2. On the top navigation bar, click on the option “Object Manager”.

Salesforce ADDMONTHS Function Example

3. After you reach the object manager setup, search for the object in the “Quick Find” on which you want to utilize the ADDMONTHHS function and click on it. According to the scenario, you need to choose the “Product” object.

ADDMONTHS Function in Salesforce Lightning

4. Then, click on the “Fields & Relationships” option -> “New” button to create a new field.

ADDMONTHS Function in Salesforce Lightning Example

5. Select the field type that corresponds to the kind of information you wish to save. Our main agenda is to use the function, so you need to choose the “Formula” data type by clicking on the radio button. Then, proceed by clicking “Next”.

Salesforce Lightning ADDMONTHS Function

6. Input the “Field Label” as “Trial End Date” and the “Field Name” will be automatically populated based on the field label.

Now, proceed by scrolling down and selecting the formula return type from the available data types. As per the scenario, click on the radio button next to the “Date” data type. Then, click on the “Next” button.

Salesforce Lightning ADDMONTHS Function Example

7. Click on the “All Functions Categories” dropdown and choose the necessary function category. According to the scenario, you should select the “Date and Time” category.

Next, search for the “ADDMONTHS” function and click on it. Then, click on the “Insert Selected Function” button located in the functions section.

8. The function will be successfully added to the formula subtab. Replace the function parameters with the date field as per the scenario.

For instance, if you want to calculate the product trial period based on the product purchase date. And your organization decided to give a trial period of 3 months only.

For this, use the ADDMONTHS function and the formula provided below:

ADDMONTHS(Purchase_Date__c, 3)

Here is the detailed description of the formula:

Fields, Operators, and Functions used in the formula:

  • Purchase_Date__c: It is the custom date field on the basis of which you want to determine the trial end date.
  • 3: It is the number of months that you want to add to the purchase date value.
  • ADDMONTHS(): It is the function that will add the specific number of months to date.

Working of the formula:

  • ADDMONTHS(Purchase_Date__c, 3): It is a Salesforce formula where you use the ADDMONTHS function and it is used to calculate a date that is three months after the date “Purchase_Date__c” field.
  • In general, the function added the second argument value i.e. 3 to the first argument value i.e. “Purchase_Date__c” field.

9. Click on the “Check Syntax” button to check the correctness of the formula. If there are errors, you will be notified. Otherwise, you will receive a notification saying “No Syntax error in merge fields or function”.

If you wish, you can enter a “Description” and “Help Text” to give deep details about the formula. After that, proceed by clicking the “Next” button.

How to use ADDMONTHS function in Salesforce Lightning

10. Choose the profiles to which you want to provide field-level security editing access. Some of the profiles are already selected by default.

If you wish to grant access to reset the pending profiles, click on the checkbox next to the respective profile names.

See also  LN Function in Salesforce | Calculate natural logarithmic value in Salesforce

If you want to select all profiles at once, click on the checkbox next to the “Visible” option. Then, click on the “Next” button.

How to use ADDMONTHS function in Salesforce Lightning Example

11. In this step, you will see a list of the page layouts where this field is visible. By default, all the page layouts are selected.

However, if you don’t want to include the custom field on any specific layout, deselect those layouts.

Finally, to finish the process of using the ADDMONTHS() function in Salesforce, click on the “Save” button.

How to use ADDMONTHS function in Salesforce

12. After you have created the formula field, you can use it. Here’s how it works:

  • Open the “Products” item and create a new product.
  • Fill in the “Purchase Date” field, and then save the product.
  • Navigate to the “Details Section”. There, you will find the “Trail End Date” field displaying the output of the ADDMONTHS function based on the “Purchase Date”.

Let’s understand it with the example:

  • Suppose you have entered the purchase date “06/15/2023” of the product. After the execution of the formula, you will get the output for the trial end date “09/15/2023”.
  • You can say that the formula effectively adds 3 months to the date value using the ADDMONTHS function in Salesforce.
How to use ADDMONTHS function in Salesforce Example

With this, you have learned how to use the ADDMONTHS function to add or subtract specific months in the date value in Salesforce Lightning. In the next section, you will learn how to use the ADDMONTHS function in Salesforce Classic.

How to use ADDMONTHS function in Salesforce Classic

There are various situations where you can utilize the ADDMONTHS function within Salesforce. Consider a scenario where you want to manage billing and payments efficiently by determining the next due date for invoices.

Let’s explore the steps of how to create a formula to add or subtract specific numbers of the month from a date value in Salesforce Classic.

1. Login into the Salesforce account in the Salesforce Classic Interface.

2. On the top right side, click on the “Setup” option. If it is not located here, click on the dropdown arrow next to your name here you will get it.

Salesforce Classic ADDMONTHS Function

3. Locate the “Build” section on the left sidebar. Search and expand the “Customize” dropdown, you will get the list of the objects in the Salesforce.

4. Select the “Object” on which you want to apply the ADDMONTHS function by clicking the dropdown menu and expanding the options. From the expanded list, click on the “Fields” option.

In accordance with the scenario, choose the “Orders” object.

Salesforce Classic ADDMONTHS Function Example

5. Scroll down to the “Order Custom Fields & Relationships” section and click on the “New” button to create a new field in Salesforce Classic.

ADDMONTHS Function in Salesforce Classic

6. From the list of the data type specify the type of information that the custom field will contain by clicking on the radio button next to it. According to the scenario, select “Formula” as the data type.

Afterward, click on the “Next” button to move on to the next step.

ADDMONTHS Function in Salesforce Classic Example

7. Type the “Field Label” for the new custom formula field, and then click on “Field Name”, which will be automatically populated based on the field label.

By the scenario, you can enter the field label as “Payment Due Date”.

Scroll down to the formula return type and select the appropriate data type for the formula return type by clicking on the radio button next to the desired data type.

As per the scenario, choose “Date” as the data type. After that, click on “Next” and move to the next step.

How to use ADDMONTHS function in Salesforce Classic

8. Click and expand the “All Function Categories” dropdown. Search and select the category of the function where your function is located. As per the scenario, click on the “Date and Time” category.

Now, choose the function from the category by clicking on it. According to the scenario, you should click on the “ADDMONTHS” function. Click on the “Insert Selected Function” button located below the function section to insert the formula into the sub-tab.

See also  Salesforce UNIXTIMESTAMP Function

9. Replace the parameters with the appropriate fields and operators to create the desired formula. For example, you need to determine the next payment due date of the invoice.

You decided to calculate the payment due date based on the order purchase date. Below is the formula for that:

ADDMONTHS(Purchase_Date__c , Bill_Generated_Month__c)

Here is the detailed description of the formula:

Fields, Operator, and Function used in the formula:

  • Purchase_Date__c: It is a custom date field that represents the purchase date of the order.
  • Bill_Generated__c: It is a custom numeric field that represents the number of months to be added to the purchase date.
  • ADDMONTHS: It is a Salesforce function that is used to add specific numbers of months to the date or date/time value.

Working on the formula:

  • ADDMONTHS(Purchase_Date__c , Bill_Generated_Month__c): It is a formula that is used to calculate the date after a specified number of months added to the stored date or date/time value in Salesforce.

10. To confirm the correctness of the formula click on the “Check Syntax” button. If there is an error, an error message will be displayed. Otherwise, you will see a message stating “No Syntax error in merge fields or function.”.

Optionally, you can enter a “Description” and “Help Text” to provide additional information about the formula. You can also handle blank fields as needed.

Once the step is completed, click on the “Next” button to continue.

How to use ADDMONTHS function in Salesforce Classic Example

11. In this step, set up field-level security. Choose the profiles to which you want to provide editing access for this field through field-level security.

The field will be hidden from profiles that you don’t add to the field-level security. Some profiles may already be selected by default. If you need to make changes and grant or revoke edit access, select or deselect profiles accordingly.

If you want to quickly select all profiles, simply click on the “Visible” checkbox. To proceed to the next step, click on the “Next” button.

Use ADDMONTHS function in Salesforce Classic

12. In the “Add to Page Layout” step, select the page layout where you want to include this field. The field will be added as the last field in the first two-column section of these page layouts.

If you do not select a layout, the field will not appear on any pages. By default, all page layouts are selected. You can uncheck the checkbox to deselect page layout if you don’t need to add the custom field to a specific page layout.

Finally, to complete the process of creating a formula field using the ADDMONTHS() function in Salesforce, click on the “Save” button.

Use ADDMONTHS function in Salesforce Classic Example

13. Now, you will be able to use this formula field. Let’s see the steps:

  • Click on the “Orders” item in the Salesforce Classic, create a new order with the field “Purchase Date” and save it.
  • Open the order, move to the details section, and find the “Payment Due Date” field. Here you will get the output of the ADDMONTHS function based on the formula you defined.

Let’s understand it with the example:

  • Suppose you have entered an order with a purchase date of “08/15/2023” and the bill generated “1”. After the execution of the formula, you will get the output for the payment due date “09/15/2023”.
  • Hence you can effectively say that the formula adds the specified number of months to the given date value in Salesforce Classic using the ADDMONTHS function.
Example of ADDMONTHS Function in Salesforce

You have now learned how to use the ADDMONTHS function to create a formula that adds or subtracts a specified number of months in Salesforce Classic.

Conclusion

It is possible to add and subtract months from the date and date/time value in Salesforce by utilizing the ADDMONTHS function.

This article has taught you how to use the ADDMONTHS function, its syntax, and various use cases. It also provides a step-by-step guide on how to ADDMONTHS function in both Salesforce Lightning and Salesforce Classic.

You may like to read the following articles: