CHR Function in Salesforce

Are you facing a challenge within Salesforce in adding special characters like icons, geometric shapes, or symbols?

Like you need to add the pi mathematical symbol but you do not get the way to add it.

If yes, there is an awesome function for it in Salesforce called CHR. This function is useful to give you the string character, special symbol, icons, shapes, etc. with the first character’s code point in the given number.

In this Salesforce tutorial, you will learn the CHR function in Salesforce. You will also learn the basic syntax of the function in Salesforce and see various examples illustrating several use cases of the function.

Furthermore, you will explore the steps of utilizing the CHR function in Salesforce Lightning and Salesforce Classic with the real-based scenario.

Salesforce CHR Function

The CHR Function in Salesforce is one of the TEXT functions in Salesforce, which are used for text operations and effective data manipulation on Salesforce fields.

In general, each character in the string is represented by a unique number known as a Unicode Code Point. For example, the Unicode code point for the letter ‘A’ is 65.

Similarly, in Salesforce you have one function called CHR Function that represents the string of the given numerical value according to the character encoding unique code point.

In simple words, you can say that it represents the character, symbol, icon, etc (code point) according to the first character of the numerical value that you passed to the function as a parameter.

Moreover, if you try to use something other than the number in the parameter you will get an error.  The error message will be like “Incorrect parameter type for function ‘CHR()’. Expected Number, received other DataType”.

Additionally, the CHR function return result in the Text data type and become incompatible with the other data type such as Number, Date, Time, etc.

You are now aware of the CHR function’s basic concepts, let’s move on to the next section to look at the Salesforce CHR function’s syntax.

CHR Function Syntax in Salesforce

Before using any function in Salesforce, it is important to know the syntax of the function.

CHR(number)

Here:

  • number: The number is the parameter that specifies the code point for every first number whose string character, symbol, or icon you want to generate.

Return: The CHR function returns a string with the first character’s code point as the given number.

Now, you have covered the syntax of the CHR function. Let’s move on to the section and look at some basic examples of the function to help you grasp how it works in real-world scenarios.

Basic Examples of CHR Function in Salesforce

The table given below illustrates the examples of the CHR function in Salesforce. In the “Input Field” column, users input various numbers, while the “Return” column showcases the corresponding special character or string generated by the CHR function.

Input Field or ExpressionReturnDescription
960πMathematical Symbol pi
128522😊Emoticons
81QMathematical Division Symbol
247÷Aries Zodiac Signs
11044Black Large Circle Geometric Shapes
9824Playing Card Spade Suit
9835Beamed Eighth Notes Musical Symbol
9800Aries Zodiac Signs

Now that you have gone through examples showcasing how the CHR function works. Let’s proceed to the next section to explore the steps on how to use the CHR function in Salesforce Lightning with the scenario.

How to use CHR Function in Salesforce Lightning

While working with Salesforce there are many cases where you want the string character that is not possible to add from the keyword like pi, infinity symbol, etc.

Image a scenario, where you want to add the smiling face string character in the greeting line.

This section will guide you through the steps of how to add a smiling face to the well-wishing line using the CHR function in Salesforce Lightning. Here are the steps to follow:

1- Login to the Salesforce account in the Lightning Experience interface. If you are not logged into the Lightning experience setup, click on the “Gear” icon, and from the dropdown menu, select the “Setup” (Setup for the current app) option.

CHR Function in Salesforce

2- Once redirecting to the main setup page, find the “Object Manager” option in the navigation bar and click on it.

CHR Function in Salesforce Example

3- You will get both custom and standard objects on the page. Now, utilize the “Quick Find Search” box to search for the specific object on which you want to apply the CHR function.

Then from the search results click on the specific object that you want.

According to the scenario, suppose you wish to use the CHR function in the “Contacts” object.

Salesforce CHR Function

4- You will be now on the Contact Manager page. Find the “Fields & Relationships” option on the left side and click on it.

Next, click on the “New” button located at the top-right side of the page.

Salesforce CHR Function Example

5- In this step, you will get all the available data types in Salesforce. Select the data type according to the requirement.

In this case, according to the scenario, I opt for the “Formula” data type by clicking on the radio button next to it.

See also  TRUNC() Function in Salesforce

After that, click on the “Next” button and proceed to the next step.

CHR Function in Salesforce Lightning

6- Enter the “Field Label” and “Field Name”. Note: once you click on the field name box it will automatically fill up.

For example, I type the field label as “Greetings” according to the scenario.

Moving downward, choose the formula return type from the list of available data types by clicking on the radio button next to it.

In accordance with the scenario, I select the “TEXT” as the formula return data type.

Then, click on the “Next” button and proceed to the next step.

CHR Function in Salesforce Lightning Example

7- You are now on the add formula step. Navigate to the right side of the page to the “Functions Section”.

From the “All Functions Categories” dropdown, select the function category according to the requirement. In this case, I choose the “TEXT” category as per the above-defined scenario.

Next, find and select the function from the selected category that you require as per the need. According to the scenario, I select the “CHR” function as per my requirement.

Click on the “Insert Selected Function” button located below the function box. Once the function is added to the advanced formula tab, replace the parameters with the appropriate merge fields or numbers.

For example, you want to add a smiling face emoji with a greeting message to the contact. Below is the formula:

Message__c & CHR(128522)

Here is a detailed explanation of the formula:

  • Message__c: The “Message__c” refers to the custom field that stores the text.
  • CHR: The CHR() is the function that represents the string with the first character’s code point as per the given number.
  • 128522: The 128522 is the number that you passed to the function that represents a Unicode character associated with a smiling face with smiling eyes.

8- In this step verify whether the formula is error-free or not. To check the formula, click on the “Check Syntax” button.

  • If the formula is error-free, you will receive the message “No Syntax Error”.
  • If the formula has an error, an error message will be displayed.

Furthermore, you have the option to add a “Description” and “Help Text” and decide how to handle empty fields. After that, click on the “Next” button.

Salesforce Lightning CHR Function

9- Set up field-level security by choosing the user profiles to whom you want to grant edit access to the field. If you have not selected the profile it means that the formula field is not visible to the profile and users under this profile not able to edit it.

In this example, I want to grant edit access to this custom field to all the profiles, to do that quickly, I check the checkbox next to “Visible”.

Then, click on the “Next” button to proceed to the next step.

Salesforce Lightning CHR Function Example

10- Select the specific page layout where you want to add the custom formula field. On the page, the field will be added at the end.

If you have not chosen the page layout, the field will not appear on the specific page. By default, all page layouts are selected. If you want to unselect any of them you can do it easily.

Finally, finish the process of creating a field and using the CHR function by clicking on the “Save” button.

How to use CHR Function in Salesforce Lightning

11- After successfully creating the formula field, you can now use it.

  • Open the “Contacts” item.
  • Create a new contact by entering the value in the “Message” field.
  • Then, save the contact.
  • Navigate to the details page.

Here, you will find the “Greetings” field which displays the result of the CHR function in Salesforce. The formula successfully returns the string based on the passed code point number.

Let’s understand with an example:

Suppose you have created the contact with the message “Warm wishes for a day that brings you happiness, success, and moments of inspiration” and save it.

After execution of the formula, you will get the output for the greeting field “Warm wishes for a day that brings you happiness, success, and moments of inspiration 😊”.

You can say that the formula effectively adds the smiling emoji to the message or you can say the formula returns a string with the first character’s code point as the given number.

How to use CHR Function in Salesforce Lightning Example

With this, you have learned how to use the CHR function in Salesforce Lightning. Now, you will learn how to use the CHR function in Salesforce Classic.

How to use CHR Function in Salesforce Classic

You can also use the CHR function in the Salesforcece Classic, in multiple cases.

Let’s suppose you want to automatically assign the zodiac sign to the lead as per their date of birth with the symbol of the zodiac.

Let’s explore the steps of how to use the CHR function in the Salesforce Classic that adds the zodiac sign symbol.

1- Login to the Salesforce account using the Classic experience interface. If you are on the Lightning interface by default shift to the classic first by clicking on the “Profile” and choose the “Switch to Salesforce Classic” option from the dropdown menu.

CHR Function in Salesforce Classic

2- Select the “Setup” option located at the top right side, between the username and the help option.

CHR Function in Salesforce Classic Example

3- Navigate down to the “Build” section on the left side.

Click on the “Customize” dropdown, and from the list of objects, select the object in which you want to use the CHR function and click on it.

See also  ACOS() Function in Salesforce | Calculate inverse cosine value in Salesforce

According to the scenario, I search for the “Leads” object.

Once on the object, a variety of options for the lead object will be presented on the dropdown menu, from here click on the “Fields” option.

Salesforce Classic CHR Function

4- Scroll down to the section labeled “Leads Custom Fields & Relationships” and click on the “New” button.

Salesforce Classic CHR Function

5- Specify the type of information that the custom field will contain by choosing the data type from the list of the available Salesforce data types.

According to the scenario, you need to select the “Formula” data type by clicking on the radio button given in front of it.

After that, click on “Next” to proceed to the next step.

How to use CHR Function in Salesforce Classic

6- Enter the “Field Label” and when you click on the “Field Name” the field will be automatically populated.

In this example, you can enter the field label as “Zodiac Sign”.

Move down to the formula return type section and choose the return type for the formula field. In accordance with the scenario, you need to select the “Text” data type by clicking on the radio button.

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

How to use CHR Function in Salesforce Classic Example

7- In this step you are on the enter formula page. Proceed with moving your mouse to the “Function” section located on the right side of the page.

You will see the dropdown menu labeled “All Function Categories” Click on it and choose the function category in which your required function is located.

In accordance with the scenario, you need to choose “Text” since the CHR function is one of the text functions, so it will be located there.

Once selecting the category (text) a list of functions within this category will be displayed. Find the function that you want and click on it to select it.

According to the scenario, you need to click and find the “CHR()” function of Salesforce.

After that, click on the “Insert Selected Function” button in the functions section. Now the selected function is placed into the advance formula box.

Modify the function by replacing the function parameters with relevant fields or numbers based on the specific requirement.

Similarly, you can add multiple functions in the advance formula tab where you needed and create the formula according to the requirement.

According to the above-defined use case, the formula is as given below:

CASE(
  MONTH( Birth_Date__c ),
  1, IF(DAY( Birth_Date__c ) <= 19, "Capricorn" & CHR(9809), "Aquarius" & CHR(9810)),
  2, IF(DAY( Birth_Date__c ) <= 18, "Aquarius" & CHR(9810), "Pisces" & CHR(9811)),
  3, IF(DAY( Birth_Date__c ) <= 20, "Pisces" & CHR(9811), "Aries" & CHR(9800)),
  4, IF(DAY( Birth_Date__c ) <= 19, "Aries" & CHR(9800), "Taurus" & CHR(9801)),
  5, IF(DAY( Birth_Date__c ) <= 20, "Taurus" & CHR(9801), "Gemini" & CHR(9802)),
  6, IF(DAY( Birth_Date__c ) <= 21, "Gemini" & CHR(9802), "Cancer" & CHR(9803)),
  7, IF(DAY( Birth_Date__c ) <= 22, "Cancer" & CHR(9803), "Leo" & CHR(9804)),
  8, IF(DAY( Birth_Date__c ) <= 22, "Leo" & CHR(9804), "Virgo" & CHR(9805)),
  9, IF(DAY( Birth_Date__c ) <= 22, "Virgo" & CHR(9805), "Libra" & CHR(9806)),
  10, IF(DAY( Birth_Date__c ) <= 22, "Libra" & CHR(9806), "Scorpio" & CHR(9807)),
  11, IF(DAY( Birth_Date__c ) <= 21, "Scorpio" & CHR(9807), "Sagittarius" & CHR(9808)),
  "Capricorn" & CHR(9809)  
)

Here is a detailed description of the above-written formula:

  • CASE: The CASE statement is one of the conditional statements that checks the month of the birth date.
  • MONTH: The MONTH() function fetches the month from the Birth_Date__c field.
  • CHR: The CHR() function is used to add the zodaic symbol to each of the zodaic sign. You need to use this fucntion with each zodiac sign string. The list of the each zodaic symbol with its code point number is as given below:
Zodic Sign NameCode Point Number
Aries9800
Taurus9801
Gemini9802
Cancer9803
Leo9804
Virgo9805
Libra9806
Scorpio9807
Sagittarius9808
Capricorn9809
Aquarius9810
Pisces9811
  • &: To combine the zodiac sign and the zodiac symbol use the & connector in between.

For Month 1 i.e. January:

  • If the day of the month is less than or equal to 19, it assigns “Capricorn” and to add the zodiac symbol use the CHR function with the code point i.e. CHR(9809).
  • Otherwise assign “Aquarius” and to add the zodiac symbol use the CHR function with the code point i.e. CHR(9810).

For Month 2 i.e. February:

  • If the day of the month is less than or equal to 18, it assigns “Aquarius” with the zodiac symbol using the CHR function i.e. CHR(9810).
  • Otherwise assign “Pisces” and to add the zodiac symbol use the CHR function with the code point i.e. CHR(9811).

For Month 3 i.e. March:

  • If the day of the month is 20 or less, assign “Pisces” and to add zodiac symbol use the function with the code point such as CHR(9811).
  • Otherwise assign “Aries” and to add the zodiac symbol use the CHR function with the code point i.e. CHR(9800).

For Month 4 i.e. April:

  • If the day of the month is less than or equal to 19, it assigns “Aries” with the zodiac symbol using the CHR function i.e. CHR(9800).
  • Otherwise assign “Taurus” and to add the zodiac symbol use the CHR function with the code point i.e. CHR(9801).

For Month 5 i.e. May:

  • If the day of the month is less than or equal to 20, it assigns “Taurus” with the zodiac symbol using the CHR function i.e. CHR(9801).
  • Otherwise assign “Gemini” and to add the zodiac symbol use the CHR function with the code point i.e. CHR(9802).
See also  Salesforce MINUTE Function

For Month 6 i.e. June:

  • If the day of the month is less than or equal to 21, it assigns “Gemini” with the zodiac symbol using the CHR function i.e. CHR(9802).
  • Otherwise assign “Cancer” and to add the zodiac symbol use the CHR function with the code point i.e. CHR(9803).

For Month 7 i.e. July:

  • If the day of the month is less than or equal to 22, it assigns “Cancer” to zodiac sign and to add the zodiac symbol use the function with the code point such as CHR(9803).
  • Otherwise assign the “Leo” as the zodiac sign and to add the zodiac symbol use the function with the code point such as CHR(9804).

For Month 8 i.e. August:

  • If the day of the month is less than or equal to 22, it assigns “Leo” to zodiac sign and to add the zodiac symbol use the function with the code point such as CHR(9804).
  • Otherwise assign the “Virgo” as the zodiac sign and to add the zodiac symbol use the function with the code point such as CHR(9805).

For Month 9 i.e. September:

  • If the day of the month is 22 or less, it includes the zodiac sign as “Virgo” and add the zodiac symbol represented by CHR(9805).
  • Alternatively, if the day of the month exceeds 22, it includes the sign as “Libra” and add the associated zodiac symbol using CHR(9806).

For Month 10 i.e. October:

  • If the day of the month less than or equal to 22, it includes the sign as “Libra” and add the associated zodiac symbol using CHR(9806).
  • Otherwise, it includes the sign as “Scorpio” and add the associated zodiac symbol using CHR(9807).

For Month 11 i.e. November:

  • If the day of the month less than or equal to 21, it includes the sign as “Scorpio” and add the associated zodiac symbol using CHR(9807).
  • Otherwise, it includes the sign as “Sagittarius” and add the associated zodiac symbol using CHR(9808).

Rest Cases:

  • For the rest of cases i.e if the month is 12 and date is greater than and equal to the 22 and if the month is 1 and date is less than and equal to 19, it assign the zodiac sign as “Capricorn” and add associated zodic symbol using the CHR(9809).

8- To ensure that the formula created is free from errors, click on the “Check Syntax” button located under the advanced formula editor.

  • If there are no errors, a message is displayed as “No Syntax errors in merge fields or functions”.
  • If there are errors you will get an error message showing the problem in the formula.

Optionally, you can add “Description” and “Help Text” according to the formula and you can also specify how to handle an empty field.

After that, click on the “Next” button to proceed to the next step.

How to use CHR Function in Salesforce

9- You need to select the profiles to which you want to grant field-level security editing access. It is important to note if no profiles are chosen, the specific field will remain hidden from all profiles, and users of this profile can’t edit the field if required.

Some of the profiles are by default checked, if you want to grant access to some other profiles click on the checkbox given next to them.

Or if you want to grant the edit access of the field to all the profiles, simply check the checkbox next to “Visible”.

To proceed to the next step, click on the “Next” button.

How to use CHR Function in Salesforce Example

10- Select the appropriate page layouts where you want to include the newly created custom formula field. By default, all the page layouts are selected, but if you don’t want to include this field on any specific page uncheck the checkbox given next to it.

Click on the “Save” button to save the changes and complete the process of field creation.

Example of CHR Function in Salesforce

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

  • Open the “Leads” item create a new lead, and fill in the field “Birth Date”.
  • Save the newly created lead.
  • Once your lead is successfully saved, navigate to the lead details page.
  • On the lead details page, you will find the “Zodiac Sign” field, which shows the result of the CHR function.

Let’s understand with an example:

Imagine you have created a lead with the birth date “07/12/1996” and saved it. The formula will now execute and show you the output field zodiac sign as “Cancer.

Hence, you can say that the formula effectively fetches the zodiac sign from the date of birth and also adds the zodiac sign symbol using the code point.

Example of CHR Function in Salesforce Classic

With this, you have learned how to use the CHR function in Salesforce Classic.

Conclusion

In conclusion, you say that it is possible to add string characters such as icons, geometric shapes, or symbols in Salesforce.

You can use the Salesforce CHR function in the formula field to do this because the function converts the number we passed to the string using the code point that assigns unique characters to each number.

Through this complete article, you have learned the syntax and use cases of the CHR function.

Furthermore, you have gone through the complete step-by-step implementation of the function in both Salesforce Lightning and Salesforce Classic.

You may like to read the following articles: