BEGINS() Function in Salesforce

In this Salesforce tutorial, we will learn how to use the BEGINS function in Salesforce to determine if text begins with specific characters or not. We will also learn its syntax and a few real-world business-related useful examples.

Recently, I got hired by a USA-based multinational company that makes use of Salesforce as a CRM application. One day I got the task to assign an urgency rating to the cases based on the case source’s first character.

So, as a solution, I found that Salesforce provides a function named BEGINS which helped me to give the rating to the cases based on the case source’s first character.

Let’s go deep into the topic and learn through examples to understand how to use the BEGINS() functions in Salesforce Lightning and Salesforce Classic components.

What is BEGINS Function in Salesforce

The BEGINS() function is one of the Salesforce text functions that is used to carry out text operations. In general, the BEGINS() function is used when we need to check if text begins with specified characters and returns TRUE if it does and otherwise returns FALSE.

Make sure of the following points, while working on the BEGINS function:

  • The BEGIN function is case-sensitive so it will treat the compare text differently for both lower case and upper case.
  • Fields that are left blank when utilizing this function in a workflow rule or validation rule are treated as valid. For instance, if a validation rule checks to see if an order starts with “56” all records with the blank order id number are accepted as valid.

Syntax and Usage of BEGINS Function in Salesforce

Below is the syntax of the BEGINS function in Salesforce:

BEGINS(text, compare_text)

Here:

  • text: It specifies the value or field that you want to check. It can be anything a single plain string text or a reference to the text field.
  • compare_text: It specifies the value of the prefix that we want to look at the beginning of the text value. It can be plain text enclosed in double or single quotes, a numerical value, or a field that has compared value.

Let’s look at the scenarios, for a better understanding of the BEGINS function.

As I have discussed, I got the task of assigning an urgency rating to the cases based on the case source’s first character. So, here is the function base formula with the use of BEGIN function.

IF(
    OR(
        BEGINS(Case_Source, "Phone"),
        BEGINS(Case_Source, "Email"),
        BEGINS(Case_Source, "Lead")
 ),
    10,
    5
)

With this, we have learned the syntax of the BEGINS() function with a real-life based scenario. Now, we will proceed and learn to use the BEGINS() function in Salesforce Lightning Component.

Read BR() Function in Salesforce | How to Break Line in Salesforce

How to use BEGINS function in Salesforce Lightning

The following are the steps to use the BEGINS() function in Salesforce Lightning.

Step 1: Log in to the Salesforce Lightning mode. Click on the “Gear Icon” in the top right corner of the page. Then, click on the “Setup” option from the dropdown menu to continue.

BEGINS Function in Salesforce

Step 2: After that the setup page is opened. Here, we have the “Object Manager” option found in the Navigation Bar next to the Home tab. Click on it.

BEGINS Function in Salesforce Example

Step 3: The object manager page is opened after you click it. There are numerous objects seen here. Use the Quick Find Search field to look for the item on which we want to use the BEGINS() function.

In this example, I look for the “Coupon” object because I need to declare the first price based on the coupon code.

Step 4: Now click the “Coupon” object to move to the coupon object page.

BEGINS Function in Salesforce Lightning

Step 5: On the left side of the coupon object page, the “Field & Relationship” option is found in the details section. Select “Field & Relationship” from the menu.

Step 6: The field and relationships page is opened after selecting it. Click the “New” button at the top of the page.

BEGINS Function in Salesforce Lightning Example

Step 7: The field type page is opened after clicking the new button. The page contains multiple field types. In this case, I choose the “Formula” field type field, as I want to use the BEGINS function in the advanced formula.

Step 8: Click the “Next” button to move on to the next step.

Salesforce BEGINS Function

Step 9: The choose output type screen appears after clicking the next button. Enter the information now by filling out the “Field Label” and the “Field Name”, which is automatically filled up. In this instance, I entered the “Prize” into the field label.

Step 10: Choose the “Output Data Type” for the field in which we want to store the result. In this case, I check the radio button in front of the “Text” data type to receive the formula return type as a text.

Step 11: Move to the next, click on the “Next” button.

Salesforce BEGINS Function

Step 12: Go to the advanced formula editor, move to the right side of the page in the Functions Section, and choose the Text Categories Function from the dropdown.

Step 13: Now, select the BEGINS from here and enter the BEGINS() function formula there. In this instance, I want to use the BEGINS function to declare the first price based on the coupon code. The formula is as follows:

IF( 
    BEGINS(CouponCode, 'FR5968DS'), 
    'Won IPhone13', 
    'Better Luck Next Time' 
)

Here:

  • We use the BEGINS() function that checks if CouponCode begins with a specific character “FR5968DS”.
  • Then, we use the IF() function that prints the prize message based on the result of the BEGINS() function.
    • If the CouponCode begins with specified characters, it prints the message Won IPhone13.
    • If the CouponCode does not begin with the specified characters, it prints the message Better Luck Next Time.

Step 13: To check the formula and make sure there are no errors, click the “Check Syntax” option.

Step 14: Optionally, we may define the “Description” and “Help text” as well as “Handle the empty field” if want like to.

Step 15: Click on the “Next” button to go to the security setup for the formula field where we have used the BEGINS function.

Salesforce BEGINS Function

Step 16: In this step, establish “Field-Level Security”. Choose the user profiles to which we wish to grant field-level security edit access to this field. If field-level security is not added, this custom formula field will not be shown on all profiles.

As I want it to be visible to all profiles, I have checked the “Visible” checkbox in this instance.

Step 17: After that, click the “Next” button located at the top.

Salesforce Lightning BEGINS Function Example

Step 18: Add the custom formula field that we have created to the “Page Layout” after setting up the field-level security. Now, select the page layout that has this field in it. If we do not select a layout, the field will not appear on any pages.

Step 19: To save the formula field, click the “Save” button.

How to use BEGINS Function in Salesforce Lightning

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

  • Open the Coupon Tab and create a new coupon with the field Coupon Name.
  • The Prize field, which displays the output of the BEGINS() function, will be shown in the Details Section.

As a result, we now understand how to use the BEGINS function in Salesforce Lightning. Now, we will look at how to use the BEGINS function in Salesforce Classic.

Read CASESAFEID() Function in Salesforce

How to use BEGINS function in Salesforce Classic

The following are the steps to use the BEGINS() function in Salesforce Classic.

Step 1: Log in to your Salesforce account first, after you are logged in, the Salesforce Lightning Edition should already be open.

Step 2: Click “Profile Avatar” once you open it in Salesforce Lightning. The drop-down menu for the profile photo is available. And here, we have an option of “Switch To Salesforce Classic” below it and now we have to click on it.

BEGINS Function in Salesforce Classic

Step 3: Next, click on the “Setup” option from the menu.

BEGINS Function in Salesforce Classic Example

Step 4: The setup page is opened after selecting the setup option. Go down to the “Build” section now. Select the object for whose field we want to use the BEGINS() function under the “Customize” dropdown. In this instance, I choose the “Lead” object since I need to check whether the city name entered in the address is one of the USA major USA-based cities or not.

Step 5: Select “Leads” from the drop-down menu. The drop-down list is available, and under it, there is a field option. Select “Fields” from the menu.

Salesforce Classic BEGINS Function

Step 6: The product custom field page is opened after selecting the fields option. There is a section titled “Lead custom field and relationship” at the bottom of the page. Simply click the “New” button to add a new field.

Salesforce Classic BEGINS Function Example

Step 7: The field type page is opened after clicking the new button. The page contains a wide variety of data types. I am choosing the “Formula” data type field in this case.

Step 8: Click the “Next” button to move on to the following step.

How to use BEGINS function in Salesforce Classic

Step 9: The “Choose Output Type” window appears after selecting the next button. Fill out the “Field Label” and “Field Name” (which are pre-filled when you click on the field name) to enter the information. In this case, I entered “Major USA City Status” into the field label.

Step 10: Choose the “Output Data Type” for the field where we want to store the result. In this case, I check the radio button before the “Text” data type to receive the formula return type as a text.

Step 11: Click the “Next” button to move on to another step.

How to use BEGINS function in Salesforce Classic Example

Step 12: Go to the advanced formula editor, move to the right side of the page in the Functions Section, and choose the Text Categories Function from the dropdown.

Step 13: Now, select the BEGINS from here and enter the BEGINS() function formula there. In this instance, I want to use the BEGINS function to check whether the city name entered in the address is one of the USA major USA-based cities or not. The formula is as follows:

IF(
    OR(
        BEGINS(City, "New York"),
        BEGINS(City, "Los Angeles"),
        BEGINS(City, "Chicago"),
        BEGINS(City, "Houston"),
        BEGINS(City, "Phoenix")
    ),
    "This is a major USA city",
    "This is not a major USA city"
)

Here:

  • We use the BEGINS function to determine if the text value in the City field begins with one of the main US cities that have been listed.
  • Next, we use the OR function to check that at least one of the passed conditions is true.
  • After that, we use the IF function that checks if the city name starts with any of the major USA-based city names:
    • If it is, we print the message “This is a major USA City”.
    • Otherwise, we print the message “This is not a major USA City”.

Step 14: Click “Check Syntax” to validate the formula and ensure no errors.

Step 15: Define the “Description” and “Help text” as well as “Handle the empty field” if you want to add it, it is an optional step.

Step 16: To access the security page for the formula field, click the “Next” button.

Use BEGINS function in Salesforce Classic

Step 17: To set up the “Field-Level Security”. Select the profiles to whom you want to grant field-level security editing access. The field will be hidden from all profiles if field-level security is not set.

In this case, I selected the “Visible” checkbox because I want it to be visible to all profiles.

Step 18: To continue, click the “Next” button at the top.

Use BEGINS function in Salesforce Classic

Step 19: After setting up the field-level security, add the custom field to the “Page Layout”. Choose the page layout that must have your new include field. The field would not be shown up on any pages if we choose not to select a layout.

Step 20: Click the “Save” button to save the field.

Example of BEGINS function in Salesforce Classic

Once we have established the formula field, we may use it. Let’s examine an illustration:

  • Open the Leads Tab and create a new lead with the field City.
  • The Major USA City Status field, which displays the BEGINS() function output, then appears when you click on the Details Section.
BEGINS function in Salesforce Classic Example

We now understand how to use Salesforce Classic BEGINS() function.

Conclusion

In conclusion, we have learned the Salesforce BEGINS() function. Moreover, we have learned the syntax of the BEGINS() function in Salesforce and several real-world business scenarios.

In addition, we also covered the steps for using the BEGINS() Function in Salesforce Lightning and Salesforce Classic component.

You may like to read: