In this Salesforce tutorial, we will learn how to use the ASCII function in Salesforce to get the first character’s ASCII code from the given string as a number. 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 uses Salesforce as a CRM application. One day I got the task to assign the rating to the leads based on the lead source.
So, as a solution, I found that Salesforce provides a function named ASCII which helped me to give the rating to the leads based on the lead source.
If you want to learn how to use the ASCII() function in Salesforce Lightning and Salesforce Classic components, let’s deep dive into this tutorial and learn with examples.
Salesforce ASCII function
As we all know that in the computer world, ASCII (American Standard Code for Information Interchange) is a commonly used character encoding system that gives each character in the English alphabet, as well as numerals and different symbols, a unique numeric value.
And in Salesforce, the ASCII() function is one of the text functions available that is used to carry out text operations. In basic terms, the ASCII() function is used when we need to get the numeric representation of the first character’s code point from the given string.
In addition, the ASCII function represents the lower case letter a-z by values 97 to 122, and similarly, the upper case letter A-Z is represented by values 65-90. Moreover, numeric digits 0-9 are represented by values 48 to 57, respectively. And each symbol also has its unique code point or, you can say, numeric value.
Syntax and Usage of the ASCII Function in Salesforce
The syntax of the ASCII() function in Salesforce is as follows:
Here, the value parameters specify the value or field for that we want to return the numeric representation or code point of the first single character of the string.
For a better understanding of how the ASCII function works, let’s look at an example.
IF( ASCII(TEXT(Lead_Score__c)) >= 62 && ASCII((TEXT(Lead_Score__c)) <= 78, "Hot", "Warm" )
Suppose we have a lead score value of “W8933”. Here, the ASCII value of the first character, “W” is 87. And 87 does not fall within the range 62-78. Therefore, the lead rating is set to “Warm”.
With this, we learned the syntax of the ASCII() function and an example. We’ll now look at how to use the ASCII() function in Salesforce Lightning.
How to use ASCII function in Salesforce Lightning
The following are the steps to use the ASCII() function in Salesforce Lightning.
Step 1: Open 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.
Step 2: The setup page is opened after selecting the setup option. The “Object Manager” is found in the Navigation Bar next to the Home tab. Click on it.
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 we want to use the ASCII() function. In this instance, I look for the “Coupon” object because I need to determine the discount based on the first character of the coupon code.
Step 4: Click the “Coupon” object to move to the product object page.
Step 5: Under this object page, the “Field & Relationship” option is found in the details section on the left side of the page. 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.
Step 7: The field type page is opened after clicking the new button. The page contains a wide variety of field types. In this case, I choose the “Formula” field type field, as I want to use the ASCII function in the advanced formula.
Step 8: Click the “Next” button to move on to the next step.
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 in when you put the cursor on the field name. In this instance, I typed “Discount Price” 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 in front of the “Currency” data type to receive the formula return type as a currency. I also set the Decimal Place to 2.
Step 11: Move to the next, click on the “Next” button.
Step 12: Enter the ASCII() function formula in the advanced formula editor. In this case, I wish to utilize the ASCII function to determine the discount based on the first character of the coupon code. The formula reads as follows:
IF( ASCII(CouponCode) >= 65 && ASCII(CouponCode ) <= 74, Price__c * 0.3, Price__c )
- We use the ASCII function to convert the first character of the Coupon Code field into the unique ASCII value of the character.
- Then, we use the IF function that checks wheater the first character of the Coupon Code falls within the range of 65-74 i.e. A-J.
- If it is, we multiply the price with the value 0.3, which means a 30% discount is applied to the price.
- If not, we get the price without any discounted value.
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 we like.
Step 15: Click the “Next” button to go to the security setup for the formula field.
Step 16: In this step, establish “Field-Level Security”. Choose the profiles we wish to grant field-level security edit access to this field. If field-level security is not added, the field will be hidden from all profiles.
As I want it to be visible to all profiles, I have checked the “Visible” checkbox. After that, click the “Next” button located at the top.
Step 17: Add the custom field to the “Page Layout” after setting up the field-level security. Pick the page layout that has this field in it. The field will not appear on any pages if we do not select a layout.
Step 18: Click the “Save” button to save the formula field.
We can use the formula field once we have created it. Let’s look at an example:
- Create a new coupon with the fields Coupon Code and Price by opening the Coupon Tab.
- Click on the Details section after that, and the Discount Price field will display the ASCII() function output.
As a result, we now understand how to use the ASCII function in Salesforce Lightning. Now, we will look at how to use the ASCII function in Salesforce Classic.
How to use ASCII function in Salesforce Classic
The following are the steps to use the ASCII() 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. Click “Profile Picture” once you are in Salesforce Lightning. The drop-down menu for the profile photo is available. A “Switch To Salesforce Classic” option is below the profile picture selection. Click on it.
Step 2: Next, click on the “Setup” option from the menu.
Step 3: 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 ASCII() function under the “Customize” dropdown. In this instance, I choose the “Orders” object since I need to generate the order’s unique code based on the order’s first character and id.
Step 4: Select “Orders” from the drop-down menu. The drop-down list is available; under it, there is a field option. Select “Fields” from the menu.
Step 5: The product custom field page is opened after selecting the fields option. A section titled “Orders custom field and relationship” is at the bottom of the page. Simply click the “New” button to add a new field.
Step 6: 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 7: Click the “Next” button to move on to the following step.
Step 8: 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 “Order Unique Code“ into the field label.
Step 9: 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 10: Click the “Next” button to move on to another step.
Step 11: Go to the advanced formula editor and enter the ASCII() function formula there. In this instance, I want to use the ASCII function to generate the unique code of the order based on the first character and the id of the order. The formula is as follows:
TEXT (ASCII( Name )) & "-" & Id
- We pass the order name field “Name” to the ASCII function to get the ASCII value of the first character of the order name.
- Next, we pass the resultant ASCII value to the TEXT function to convert the ASCII value into the text format, as we want to concatenate it with other text also. If you do not do so, you will get an error.
- Then, we use the & operator to combine the text representation of the ASCII value with the hyphen “-“ and the id of the order.
Step 12: Click “Check Syntax” to validate the formula and ensure no errors.
Step 13: Define the “Description” and “Help text” as well as “Handle the empty field” if you would like.
Step 14: To access the security page for the formula field, click the “Next” button.
Step 15: Establish “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. To continue, click the “Next” button at the top.
Step 16: After configuring the field-level security, add the custom field to the “Page Layout”. Choose the page layout that includes this field. The field would not appear on any pages if we choose not to select a layout.
Step 17: Click the “Save” button to save the formula field.
Once we have established the formula field, we may use it. Let’s examine an illustration:
- Open the Orders Tab and create a new order with the field Order Name.
- The Order Unique Code field, which displays the ASCII() function output, then appears when you click on the Details Section.
We now understand how to use Salesforce Classic’s ASCII() function.
In conclusion, we have learned the Salesforce ASCII() function. Moreover, we have learned the ASCII() function in Salesforce syntax and several real-world business scenarios.
In addition, we also covered the steps for using the ASCII() Function in Salesforce Lightning and Salesforce Classic.
You may like to read:
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.