Salesforce TRIM Function

In this Salesforce Tutorial, we will learn about Salesforce TRIM Function. We will also learn the syntax and real business-related scenarios where we can use the Salesforce TRIM function.

While working in a company that uses Salesforce as a customer relationship management tool, I saw that most of the time all the Salesforce developers face an error where the client enters inaccurate data that has lots of unnecessary spaces.

Due to this, the salesforce developer faces problems in searching, decision-making, customer search, inconsistent data entry, missed opportunities, etc.

So, I decided to overcome this problem, I started searching and found that in Salesforce we have a function named “TRIM” that removes the extra space and makes the data consistent and reliable.

If you want to learn how I used the Salesforce TRIM function with many more real-world business examples, and also want to learn how to use the TRIM function in Salesforce Lightning and Salesforce Classic, deep dive into the complete article.

What is TRIM Function in Salesforce

The TRIM() Function in Salesforce is one of the TEXT functions of Salesforce, that is used to perform the text operations on the Salesforce fields.

The Salesforce TRIM() Function, is used to remove all spaces from a text string except for single spaces between words. It removes the spaces and tabs from the beginning and end of a text string.

It helps in data cleaning and ensuring that there are no unwanted spaces between or start and end of the data entered. In simple terms, the function guarantees that unnecessary spaces are removed from fields.

The syntax of the Salesforce TRIM function is as given below:

TRIM(text)

Here, the text parameter specifies the expression or a field that you want to trim.

In addition, you can say that the TRIM function removes the spaces, making inconsistent data with spaces valuable and clean when extracting the data from external systems, spreadsheets, etc.

Let’s see an example of the TRIM  function to understand the function better:

Assume a scenario, one person is working in a USA-based digital marketing company and the company gives him the task of importing all the contact details of the clients from an external spreadsheet. After importing it, he observed that when he searched for the client’s name he did not get the client’s contact details but the client is in the record.

After spending time, he figured out that there is an issue of extra spacing in the client name, some of the client names have leading spaces and some of them have trailing spaces.

To solve this, he found that there is a Salesforce TRIM() Function, so he decided to try it. Below, is the automatic formula logic for the scenario:

TRIM(Name)

With this, we have learned about the Salesforce TRIM function with syntax. In addition, we have seen a small example related to the function for a deep understanding of the topic.

Let’s move ahead and learn to use the TRIM() Function in Salesforce Lightning and Salesforce Classic.

Read UPPER Function in Salesforce | How to convert String to Uppercase in Salesforce

How to use TRIM Function in Salesforce Lightning

The following are the steps to use the Salesforce TRIM Function to remove the spaces and tabs from the string in Salesforce Lightning. Let’s take a scenario and understand it better:

Suppose you are working in a company that uses Salesforce as a customer relationship management tool. As an event manager, you have found that there are unwanted spaces in the description of the campaign and you decided to remove these spaces with the help of the TRIM function.

Let’s see the steps on how to use the TRIM function in Salesforce Lightning to get the solution of the defined scenario.

Step-1: Log in to the Salesforce Lightning Experience interface. If you are not on the main home setup page within the Lightning Experience, find and select the “Gear” icon located at the top right side. Then, from the dropdown menu, choose the “Setup” option.

Salesforce TRIM Function

Step2: Once you are directed to the setup for the current app in Lightning, you will get an option for the “Object Manager” in the navigation bar, click on it.

Salesforce TRIM Function Example

After you click on it, you will get the list of custom and standard objects. Search for the specific object in the “Quick Find Search” on which you want to use the TRIM function. As per the current scenario, you need the “Campaign” object, so search for the campaign and click on it.

TRIM Function in Salesforce

Step3: Once you click it, you will be redirected to the contact object manager page. Here you will get the “Fields & Relationships” option at the left side of the page, click on it. Next, click on the “New” button located at the top right, as your objective is to create a new field where you can use the TRIM function.

TRIM Function in Salesforce Example

Step4: In this step, you will be presented with the list of available field types of Salesforce. From the list, select the field type that you need according to the above-defined scenario. According to your objective, you need the “Formula” field type so choose it by clicking on the radio button in front of the formula. After that, click on the “Next” button to move to the next step.

TRIM Function in Salesforce Lightning

Step5: Once you click on the next button, you will be redirected to the “Choose Output Type”. Enter the “Field Label” and “Field Name”. According to the scenario, I entered the field label “Description Without Spaces”, When you click on the field name box it will be automatically populated.

Scroll downward and choose the appropriate data type for the field that will be used to store the resultant output. As per the scenario, choose the text data type, by clicking on the radio button front to the “TEXT” data type to make sure that the formula returns the value in the text format. Then, click on the “Next” button to move to the next step.

TRIM Function in Salesforce Lightning Example

Step6: As you click on the next button, you will be moved to the “Add Formula” step. Move the cursor toward the “Functions Section” on the right side of the page. Here you will see the “All Functions Categories” dropdown click on it.

From the function category dropdown, choose the function according to your requirements. As you know the TRIM function falls under the text type functions, select “TEXT” from the available categories.

Selecting the text function shows the list of the functions related to the text category will be shown. Scroll through the list and locate the “TRIM” function, then select it. Then, click on the “Insert Selected Function” button located below the function category box.

So, the function will be inserted into the advance formula tab. Replace the parameters with the appropriate merge field, functions, operators, etc. according to your scenario-specific needs. Below is the formula for the, given requirement of removing the extra space from the description of the campaign.

TRIM(Campaign_Description__c)
  • This formula is used to remove the extra spaces along with starting and ending spaces from the text stored in the “Campaign_Description__c” field.

Step7: To validate the formula entered to check whether it contains any errors, click on the “Check Syntax” button. Moreover, you have the option to add “Description” and “Help Text” and determine how to handle empty fields. Then, click on the “Next” button to move to the next step.

Salesforce Lightning TRIM Function

Step8: In this step, you will be directed to the “Field Level Security”. You can choose which user profiles should be granted edit access to the field through the field-level security settings. If you don’t add the custom formula field to the field-level security the field will not appear for profiles that have not been selected.

In my case, I have selected the checkbox next to the “Visible” option, as I want to make this custom formula field visible for all profiles. After that, click on the “Next” button and move to the next step.

Salesforce Lightning TRIM Function Example

Step9: Once you click on the next button, you will be directed to the “Page Layout” step. Here, you need to select the page layout where you want to add the custom formula field. Note one thing, the field will be added as the last field on the page layout.

If you do not select a layout, the field will not appear on any page. By, default all the page layouts are selected, so I keep it as it is and click on the “Save”  button to save the newly created formula field.

How to use TRIM Function in Salesforce Lightning

Once the formula field is created, you can use the formula field. Let’s see an example:

  • Open the “Campaign” item, proceed to create a new campaign with the “Description” field, and save the campaign.
  • Upon successfully saving the campaign, move to the campaign details page. Here you will observe the “Description Without Spaces” field, showing the outcome evaluated from the TRIM function.

Let’s understand with an example:

Imagine you have created a campaign, with the campaign description ” Join us for our exciting Summer Sale event !” and save it. So, for the campaign description ” Join us for our exciting Summer Sale event !” the output of the formula is “Join us for our exciting Summer Sale event!”. This formula effectively removes the extra spaces from the description of the campaign.

How to use TRIM Function in Salesforce Lightning Example

With this, we have learned the utilization of the TRIM function in Salesforce Lightning, we shall now move and explore the implementation of the TRIM function in Salesforce Classic, with the help of an example.

Read LOWER Function in Salesforce | How to convert string to lowercase in Salesforce

How to use TRIM Function in Salesforce Classic

The following are the steps to use the Salesforce TRIM Function to remove the spaces and tabs from the string in Salesforce Classic. Let’s take a scenario and understand it better:

Suppose you are working in a company as a legal team advisor. Each of the contracts has a unique contract code which is required to be accurate. Sometimes due to manual data entry or copy-paste of the code, some contract codes get unintentional spaces at the beginning, end, and within the code.

Your task is to find the unwanted spaces in the contract code of the contract and remove these spaces with the help of the TRIM function. For this, you decided to use the Salesforce TRIM function and remove the spaces.

Let’s see the steps of how to use the TRIM function in Salesforce Classic to get the solution of the defined scenario.

Step1: Sign in to the Salesforce account in the Salesforce Classic Experience. If your account opens in the Lightning Experience, navigate to the “Profile”, then from the dropdown menu, click on the “Switch to Salesforce Classic” option.

Salesforce TRIM Function Classic

Step2: After this, click on the “Setup” option located at the top between the username and the help option.

Salesforce TRIM Function Classic Example

Step3: In this step, scroll down to the “Build” section, click on the “Customize” dropdown, and choose the Salesforce object where you want to use the TRIM function. According to the scenario, I searched for the “Contracts” object and clicked on it.

On clicking on the contracts dropdown, you will get a list of several contracts options from here select the “Fields” option as your main goal is to create the field where you can use the Salesforce TRIM function.

TRIM Function in Salesforce Classic

Step4: Once you click on the fields option, you will get the page of the contracts fields. Move down to the “Contracts Custom Fields & Relationships” section, and click on the “New” button to create the new custom field within Salesforce Classic.

TRIM Function in Salesforce Classic Example

Step5: Upon clicking the new button, you will get the list of field types that are available in Salesforce. According to your specific needs, select the data type from the list. Your main requirement as per the scenario is to create the formula with the TRIM function and click on the “Formula” field type by clicking on the radio button in front of the formula option. After that, click on the “Next” button to move to the next step.

Salesforce Classic TRIM Function

Step6: In this step, you are reached to the “Choose Output Type” page. Enter the details in the “Field Label” and “Field Name” fields. In this case, I have entered “Contract Code Without Space” as the field label, and on clicking on the field name box, it automatically populates.

Move downward to specify the formula return type. In this case, I have chosen “Text” as the data type by clicking on the radio button in front of the text data type. After that, click on the “Next” button to proceed to the next step.

Use of TRIM Function in Salesforce Classic

Step7: After you click on the next, you are on the enter the formula step. Move towards the right side of the page to the “Functions” section. Click on the “All Functions Categories” dropdown and select “Text” As you know the TRIM function is one of the text functions.

Then click on the text, and you will get the list of all the functions within the text category. Search the TRIM function in the list and select it. After that, click on the “Insert Selected Function” button under the functions section.

Once you click on it, the function gets inserted into the advance formula box you can replace the function parameter with the appropriate text, fields, etc. as per the requirement. Below is the formula for the, given requirement of removing the space from the contract code.

TRIM(Contract_Code__c)
  • This formula is used to remove the extra spaces along with starting and ending spaces from the text stored in the “Contract_Code__c” field.

Step8: Click on the “Check Syntax” button situated below the advance formula subtab to check whether the formula has any error. If there are no errors, you will get the message “No Syntax errors in merge fields or functions”. If an error is present, an error message will be displayed on the screen.

Additionally, You can enter the “Description” and “Help Text” fields. Optionally, If you wish, you can specify how to manage an empty field. Then to proceed to the next step, click on the “Next” button.

Salesforce Classic TRIM Function Example

Step9: In this step, you are on the “Field Level Security” page. In this step, you need to choose the profiles to whom you want to grant field-level security access to editing fields. If you don’t select any profile, the field will be hidden from these specific profiles. In my case, I have checked the checkbox next to “Visible” as I want to grant edit access to all the profiles. Next, click on the “Next” button and move to the next step.

How to use TRIM Function in Salesforce Classic

Step10: You are now on the “Add Page Layout” step. Choose the page layout that will include the field. By default, all the page layouts are selected If you don’t want to include the field in the specific page layout, uncheck them and click on the “Save” button.

How to use TRIM Function in Salesforce Classic Example

Once the formula field is created, you can use the formula field. Let’s see an example:

  • Open the “Contract” item, create a new contract with the “Contract Code” field and save the contract.
  • After saving the contract, move to the contract details page. You will see the “Contract Code Without Space” field, showing the result of the TRIM function.

Let’s understand with an example:

Suppose, you have created a contract with the contract code ” C6789FT4P” and saved it. For the contract code, the output of the formula is “C6789FT4P”. This formula effectively removes the extra spacing from the contract code.

Example of Salesforce TRIM Function

With this have learned how to use the TRIM function in Salesforce Classic with the help of examples.

Conclusion

With this, we have learned about the Salesforce TRIM function. We have also learned the syntax and its practical applications. Additionally, we have learned how to efficiently utilize the TRIM function in Salesforce Lightning and Salesforce Classic experience.

You may like to read the following articles: