MAX() Function in Salesforce | Syntax, Examples, & Use Cases

I recently joined a USA-based IT company that uses the powerful customer relationship management system “Salesforce” to deal with the business world. One day, I was assigned the task of calculating the maximum lead score across all leads.

As a solution, I discovered that Salesforce gives us the function named “Max() Function”, which is used to calculate the maximum value of the field.

In this tutorial, we will learn the MAX() Function in Salesforce and how to calculate the maximum value of a field. In addition, we will learn the syntax and some practical business use cases of the MAX() Function.

So, let’s move and learn how to use this MAX() function to calculate the maximum value of the field in Salesforce.

What is the MAX() Function in Salesforce?

The MAX() Function is one of numerous mathematical aggregate functions in Salesforce that can be used to perform mathematical computations.

The MAX() function returns the maximum value (the highest number) from a list of numbers in Salesforce.

The syntax of the MAX() Function is as given below:

MAX(number/field/formula)

The parameter value may be a number, a field, or an advanced formula used as the argument to the MAX() function.

Furthermore, the MAX() function only supports numeric data types, such as Numbers, Decimals, and Currency. And if we try to use text, dates, or other non-numeric data types, it will produce an error.

Now, let’s take an example to help us understand the concept and syntax of the MAX() function.

Suppose we want to find out the maximum number of users associated with an account. For this, I use the MAX() function in Salesforce, and here is the formula for this:

MAX(NumberOfUsers__c)

With this, we learned the syntax, explanation, and example of the MAX() function. Let’s go on and see how to use Salesforce Lightning to determine the highest number from a list of numbers.

MAX() Function in Salesforce: Calculate the Maximum Value of the Field

Here are the steps to find out the maximum value of the field in Salesforce Lightning:

  1. Log in to Salesforce org, then click the “Gear Icon” in the top right corner of the page. Click Setup from the dropdown menu to continue.
MAX Function in Salesforce
  1. From the left side of the menu, click on “Object Manager”.
MAX Function in Salesforce Example
  1.  Search for the object in the search bar where we wish to utilise the MAX() function. In this case, I search for the “Contact” object to track the client’s maximum age based on their date of birth.
    • Now, click the “Contact” object; it will redirect us to the contact page.
MAX Function in Salesforce Lightning
  1. On the left side of the page, click the Field & Relationship option. Click on the “New” button to create a new field where we can use the MAX() function.
MAX Function in Salesforce Lightning Example
  1. It will redirect to the “Choose the field type” page. Now, check the radio button in front of the “Formula” data type.
    • To proceed to the next step, click on the “Next” button.
Calculate maximum value of field in Salesforce
  1. Enter the “Field Label” for the formula, and the unique “Field Name” will be automatically populated. In this case, I entered the field label named “Maximum Age”.
    • Choose the Data Type for the field for which we want to calculate the maximum age of the client.
    • Here, I select the data type as “Number” to get the formula return type as a number, so I check the radio button in front of it, and I also set the “Decimal Place” to 0.
    • Move to the next step by clicking on the “Next” button.
Calculate maximum value of field in Salesforce Example
  1. Move to the advanced formula editor, and enter the formula that uses the MAX() function. In this case, I want to calculate the maximum age of the client using the MAX() function. Here is the formula:
MAX(YEAR(TODAY()) - YEAR(Birthdate))
  • Here, we subtract the birth date year YEAR(Birthdate) from the current year YEAR(TODAY()).
  • Then we pass the result to the MAX() function to calculate the client’s maximum age.
  • Click on the “Check Syntax” button to validate the formula and ensure that there are no errors.
  • Optionally, we can “Handle the empty field,” and we can also define the “Description” and “Help text” if we want.
  • In this case, I set the Description to “Find Maximum Age of the Contact” and the Help Text to “Use MAX Function”.
  • Click on the “Next” button to move to the formula field-level security step.
MAX() Function in Salesforce | Syntax, Examples, & Use Cases
  1. Select the relevant Field Level Security. I have selected “Visible” in this case because I want it to be visible to everyone. Then, click on the “Next” button.
Calculate maximum value of field in Salesforce Lightning Example
  1. Now, select the appropriate Page Layout where we want to be visible.
    • Click on the “Save” button to save the formula field.
How to Calculate maximum value of field in Salesforce Lightning

We can use the formula field once it’s created. Let’s look at an example:

  • Open the Contact Tab and create a new contact with the “Birth Date” field.
  • After that, click on the Details section, and here the “Maximum Age” field appears, which shows you the result of the MAX() function.
How to Calculate maximum value of field in Salesforce Lightning Example

As a result, we now know how to calculate the maximum value of a field in Salesforce.

Conclusion

With this, we have concluded that the MAX() Function in Salesforce is a great and efficient way to find out the maximum value from the list. In addition, we focused on the syntax and various real-world business-related examples of the MAX() Function.

Furthermore, we covered the process of calculating maximum value using the MAX() Function in Salesforce Lightning and in Salesforce Classic step by step.

You may like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.