Text Formula Operators and Functions in Salesforce

HELLO Salesforce Thinkers, In our previous blog we learned about Math Formula Operators and Functions in Salesforce In this blog we are going to learn about “Text Formula Operators and Functions in Salesforce”.

Formulas that use the Text return type can concatenate Text values, convert numbers and dates to text, or display text conditionally.

Text Operators:

& (Concatenate) :

Connects two or more strings.

string1&string2

Replace each string with merge fields, expressions, or other values.

Example : “FirstName & ” ” & LastName” we could display a contact’s full name by combining the first and last name.

Text Functions :

We have the following Text Functions in Salesforce.

BEGINS:

Determines if text begins with specific characters and returns TRUE if it does. Returns FALSE if it does not.

Syntax: BEGINS(text, compare_text)

Replace text, compare_text with the characters or fields you want to compare.

Notes:

  • This function is case sensitive.
  • When using this function in a validation rule or workflow rule, fields that are blank are considered valid.

BR:

Inserts a line break in a string of text.

Syntax: BR()

Notes:

  • Remember to surround the BR() with concatenation operators ‘&’.
  • Do not remove the parentheses after the function name.
  • Keep the parentheses empty.
  • Avoid using this function in mail merge templates.
  • This function is not available in custom buttons and links, s-controls, or reports.

CASESAFEID:

Converts a 15-character ID to a case-insensitive 18-character ID.

Syntax: CASESAFEID(id)

Replace id with the object’s ID.

Notes:

  • Convert to 18-character IDs for better compatibility with Excel.
  • The CASESAFEID function is available everywhere that you can define a formula except reports and s-controls.

CONTAINS:

Compares two arguments of text and returns TRUE if the first argument contains the second argument. If not, returns FALSE.

Syntax: CONTAINS(text, compare_text)

Replace text with the text that contains the value of compare_text.

Notes:

  • This function is case sensitive.
  • When using this function in a validation rule or workflow rule, fields that are blank are considered valid.
  • The CONTAINS function does not support multi-select picklists. We can use INCLUDES to see if a multi-select picklist has a specific value.

FIND:

Returns the position of a string within a string of text represented as a number.

Syntax:FIND(search_text, text[, start_num])

Replace search_text with the string you want to find, replace text with the field or expression you want to search, and replace start_num with the number of the character from which to start searching from left to right.

Notes:

  • If the field referenced in the text parameter is blank, the formula field displays 0.
  • The search_text parameter is case sensitive and cannot contain any wildcard characters.
  • If search does not return any results, a 0 displays in the field.
  • The start_num parameter is optional.
  • When entering start_num parameter, remember that some fields like the Website field are unique because a “http://” is automatically appended to the beginning of the text we enter.
  • The first character in a string is designated as one rather than zero.

GETSESSIONID:

Returns the user’s session ID.

Syntax: GETSESSIONID()

HTMLENCODE:

Encodes text and merge field values for use in HTML by replacing characters that are reserved in HTML, such as the greater-than sign (>), with HTML entity equivalents, such as >.

Syntax: {!HTMLENCODE(text)}

Replace text with the merge field or text string that contains the reserved characters.

Notes:

This function is only available in custom buttons and links, and in Visualforce.

HYPERLINK:

Creates a link to a URL specified that is linkable from the text specified.

Syntax: HYPERLINK(url, friendly_name [,target])

Replace url with the Web address, replace friendly_name with the link text, and, optionally, replace target with the window or frame in which to display the content.

Notes:

  • Hyperlink formula fields are of type text.
  • Avoid using text functions such as LEN, LEFT, or RIGHT on HYPERLINK function results.
  • Remove the brackets, [ and ], from your formula before validating it.
  • The target parameter is optional. If we do not specify a target, the link opens in a new browser window.

Some common target parameters are:

_blank – Displays link in a new unnamed window.

_self- Displays link in the same frame or window as the element that refers to it.

_parent- Displays link in the immediate frameset parent of the current frame. This value is the same as _self if the current frame has no parent.

_top- Displays link in the full original window, canceling any other frames. This value is the same as _self if the current frame has no parent.

The HYPERLINK function is available to create formula except default values, field updates, s-controls, validation rules, approval processes, custom buttons and links, and workflow rules.

IMAGE:

Inserts an image with alternate text and height/width specifications.

Syntax:IMAGE(image_url, alternate_text, height, width)

Replace image_url with the full path to the image.

Replace alternate_text with the string of text you want to appear when the image can’t be rendered for some reason. This text can be used by screen reader software.

Replace height with the vertical size of the image in pixels. Replace width with the horizontal size of the image in pixels.

Notes:

  • The height and width parameters are optional.
  • The IMAGE function is available only in formula fields and email templates.

INCLUDES:

Determines if any value selected in a multi-select picklist field equals a text literal you specify.

Syntax: INCLUDES(multiselect_picklist_field, text_literal)

Replace multiselect_picklist_field with the merge field name for the multi-select picklist and replace text_literal with the multi-select picklist value you want to match in quotes.

Notes:

  • The text_literal expression must be of type text and enclosed in quotes. It cannot be a merge field or the result of a function.
  • Salesforce returns an error if any of the following occurs:

When we not provide a text_literal expression.

When we provide an empty text_literal expression, such as “” or ” “.

We can use ISBLANK to determine if a multi-select picklist field is empty.

ISPICKVAL:

Determines if the value of a picklist field is equal to a text literal we specify.

Syntax: ISPICKVAL(picklist_field, text_literal)

Replace picklist_field with the merge field name for the picklist, replace text_literal with the picklist value in quotes.

text_literal cannot be a merge field or the result of a function.

Replace picklist_field with a custom or standard field of type picklist.

JSENCODE:

Encodes text and merge field values for use in JavaScript by inserting escape characters, such as a backslash (\), before unsafe JavaScript characters, such as the apostrophe (‘).

Syntax: {!JSENCODE(text)}

Replace text with the merge field or text string that contains the unsafe JavaScript characters.

Notes:
This function is only available in custom buttons and links, and in Visualforce.

JSINHTMLENCODE:

Encodes text and merge field values for use in JavaScript inside HTML tags by replacing characters that are reserved in HTML with HTML entity equivalents and inserting escape characters before unsafe JavaScript characters.

JSINHTMLENCODE(someValue) is a convenience function that is equivalent to JSENCODE(HTMLENCODE((someValue)).

That is, JSINHTMLENCODE first encodes someValue with HTMLENCODE, and then encodes the result with JSENCODE.

Syntax: {!JSINHTMLENCODE(text)}

Replace text with the merge field or text string that contains the unsafe JavaScript characters.

Notes:

This function is only available in custom buttons and links, and in Visualforce.

LEFT:

Returns the specified number of characters from the beginning of a text string.

Syntax: LEFT(text, num_chars)

Replace text with the field or expression you want returned, replace num_chars with the number of characters from the left you want returned.

Notes:

  • Reference auto-number fields as text fields in formulas.
  • If the num_chars value is less than zero, Salesforce replaces the value with zero.

LEN:

Returns the number of characters in a specified text string.

Syntax: LEN(text)

Replace text with the field or expression whose length you want returned.

LOWER:

Converts all letters in the specified text string to lowercase. Any characters that are not letters are unaffected by this function. Locale rules are applied if a locale is provided.

Syntax: LOWER(text, [locale])

Replace text with the field or text you wish to convert to lowercase, and locale with the optional two-character ISO language code or five-character locale code, if available.

LPAD:

Inserts characters you specify to the left-side of a text string.

Syntax: LPAD(text, padded_length[, pad_string])

Replace the variables:

text is the field or expression you want to insert characters to the left of.

padded_length is the number of total characters in the text that will be returned.

pad_string is the character or characters that should be inserted. pad_string is optional and defaults to a blank space.

If the value in text is longer than pad_string, text is truncated to the size of padded_length.

Notes:

Leading blank spaces and zeros are omitted.

MID:

Returns the specified number of characters from the middle of a text string given the starting position.

Syntax: MID(text, start_num, num_chars)

Replace text with the field or expression to use when returning characters.

Replace start_num with the number of characters from the left to use as a starting position.

Replace num_chars with the total number of characters to return.

RIGHT:

Returns the specified number of characters from the end of a text string.

Syntax: RIGHT(text, num_chars)

Replace text with the field or expression you want returned.

Replace num_chars with the number of characters from the right you want returned.

Notes:

  • Reference auto-number fields as text fields in formulas.
  • If the num_chars value is less than zero, Salesforce replaces the value with zero.

RPAD:

Inserts characters that we specify to the right-side of a text string.

Syntax: RPAD(text, padded_length[, ‘pad_string’])

Replace the variables:

text is the field or expression after which you want to insert characters.

pad_length is the number of total characters in the text string that will be returned.

pad_string is the character or characters to insert. pad_string is optional and defaults to a blank space.

If the value in text is longer than pad_string, text is truncated to the size of padded_length.

SUBSTITUTE:

Substitutes new text for old text in a text string.

Syntax: SUBSTITUTE(text, old_text, new_text)

Replace text with the field or value for which you want to substitute values, old_text with the text you want replaced, and new_text with the text you want to replace the old_text.

Notes:

  • Each term provided in quotes is case-sensitive.
  • If the old_text appears more than once, each occurrence is replaced with the new_text value provided, even when that results in duplicates.

TEXT:

Converts a percent, number, date, date/time, or currency type field into text anywhere formulas are used.

Also, converts picklist values to text in approval rules, approval step rules, workflow rules, escalation rules, assignment rules, auto-response rules, validation rules, formula fields, field updates, and custom buttons and links.

Syntax: TEXT(value)

Replace value with the field or expression you want to convert to text format.

Notes:

Avoid using any special characters besides a decimal point (period) or minus sign (dash) in this function.

TRIM:

Removes the spaces and tabs from the beginning and end of a text string.

Syntax: TRIM(text)

Replace text with the field or expression you want to trim.

UPPER:

Converts all letters in the specified text string to uppercase. Any characters that are not letters are unaffected by this function. Locale rules are applied if a locale is provided.

Syntax: UPPER(text, [locale])

Replace text with the field or expression you wish to convert to uppercase, and locale with the optional two-character ISO language code or five-character locale code, if available.

URLENCODE:

Encodes text and merge field values for use in URLs by replacing characters that are illegal in URLs, such as blank spaces, with the code that represent those characters as defined in RFC 3986, Uniform Resource Identifier (URI): Generic Syntax.

For example, blank spaces are replaced with %20, and exclamation points are replaced with %21.

Syntax: {!URLENCODE(text)}

Replace text with the merge field or text string that you want to encode.

Notes:

  • This function is only available in custom buttons and links, and in Visualforce.

VALUE:

Converts a text string to a number.

Syntax: VALUE(text)

Replace text with the field or expression you want converted into a number.

Notes:

The text in a VALUE function does not include any special characters other than a decimal point (period) or minus sign (dash).

For example, the formula VALUE(Text_field__c) produces these results:

If Text field is 100, the result is 100.
If Text field is blank, the result is #Error!
If Text field is INR100, the result is #Error!

Thanks for reading…

One thought on “Text Formula Operators and Functions in Salesforce

  1. Marci's avatar Marci

    Hi Ayushi! This is a very helpful article. I’m stuck at a scenarios hoping you should be able to help me.
    I implemented a formula checkbox to check if Field A contains a text ‘Spring’:
    Solution: If(CONTAINS(Field_A__c,”Spring”),true,false)
    but then a condition got added to check if Field A contains first word as “Spring”
    Solution: Formula text field -> LEFT(Field_A__c, FIND(” “, Field_A__c) -1)
    Now, again the condition got added to check if ‘Field A’ OR ‘Field B’ contains text “Spring” in the first word (Field A or Field B can have just one or two words separated by space). Can you help me resolve this through a formula field? Thanks!

    Like

Leave a comment