When I work with new Salesforce admins, one pattern always shows up: users dumping messy text into fields and then asking for clean reports and automations.
Phone numbers with country codes, IDs mixed with prefixes, tracking codes inside long URLs — you name it. As an admin or consultant, you need a reliable way to pull just the useful part of those strings into a Formula Field or Validation Rule.
That’s exactly where the MID function in Salesforce becomes one of your best friends. Once you understand how it works, you can extract account codes from names, last four digits from IDs, or dynamic segments from custom text without touching any code.
In this guide, I’ll walk you through how the MID() function in Salesforce works, step-by-step examples, and practical patterns I actually use in real orgs.
What Is the MID() Function in Salesforce?
The MID function in Salesforce is a text (string) function that returns a substring from a larger text value. It takes three inputs: the original text, the starting position, and how many characters you want to return.
In simple terms:
“Start reading this text from character X, and give me the next Y characters.”
This is especially useful in Formula Fields, Validation Rules, Workflow Rules, Flows (formula resources), and Approval Processes whenever you need to carve out a specific part of a text field.
If you are new to formula fields, it’s worth first understanding basic text formulas like the LEFT function in Salesforce and the RIGHT function in Salesforce, because MID follows the same pattern but gives you more flexibility in the middle of a string.
MID Function Syntax and Parameters
Let’s start with the official syntax so you know exactly what goes where.
MID(text, start_num, num_chars)
- text: The source text you want to extract from. This can be a Text field, a formula that returns text, or a literal string like “ABC-123-XYZ”.
- start_num: The position (1-based index) of the first character you want to extract. 1 means “start from the first character”.
- num_chars: How many characters you want to extract from that starting position.
Basic Examples
Assume Account_Name__c = “ACME-IND-001”
- MID(Account_Name__c, 1, 4) returns “ACME”
- MID(Account_Name__c, 6, 3) returns “IND”
- MID(Account_Name__c, 10, 3) returns “001”
This is the same pattern you see in other formula functions across Salesforce, like LEN (length of a string) and TRIM (remove leading and trailing spaces).
When Should You Use MID Instead of LEFT or RIGHT?
You could technically use LEFT or RIGHT when the substring is always at the beginning or end of the string. But in real orgs, many business IDs, codes, and structured texts are in the middle.
Use MID when:
- The part you need is in the middle of the text.
- The prefix and suffix lengths are fixed.
- You want to combine it with functions such as FIND, LEN, or INCLUDES to enable more dynamic extraction.
For example:
- Extracting the 3-letter region code from “ACME-US-WEST-009”.
- Pulling the date portion from a text like “INV-2025-11-30-007”.
- Getting a channel code from “WEB::2024::12345”.
If you only need characters at the start or end, LEFT or RIGHT formulas will be simpler to read and maintain.
Real-World Example: Region Code from Account Name
Let’s use a simple but realistic Sales Cloud example. Imagine a sales team with 20 reps where each Account Name follows this naming convention:
<CompanyName>-<RegionCode>-<4DigitNumber>
Examples:
- GlobalTech-US-1200
- BlueWave-EMEA-1840
- UrbanCloud-APAC-2030
Your sales manager wants a Region Code field to drive territory reports, but users are only filling out the Account Name.
Instead of training reps again, you decide to create a Formula (Text) field called Region_Code__c that automatically pulls the middle part.
Step-by-Step Configuration
- Go to Setup.
- Open Object Manager.
- Select Account.
- Go to Fields & Relationships.
- Click New.
- Choose Formula as the data type.
- Click Next, then:
- Field Label: Region Code
- Field Name: Region_Code
- Formula Return Type: Text
- Click Next to open the formula editor.

What this does:
- Name is the Account Name (standard field).
- FIND(“-“, Name) gives you the position of the first hyphen.
- FIND(“-“, Name) + 1 moves you to the first character after the hyphen.
- 4 tells MID to take the next four characters (e.g., “US-1”, “EMEA”, depending on your pattern).
If your region codes are always 2 or 4 characters, you can adjust num_chars accordingly.
Pro Tip: I’ve found that combining MID with FIND is much more robust than hardcoding positions, especially when company names vary in length but the structure around separators (like
-or ::) stays consistent.
You can later use Create a custom report type in Salesforce to build territory reports based on Region_Code__c.
Example: Extract Last 4 Digits of Customer ID
Now think about a support team handling 500 cases per month. You have a custom field Customer_ID__c on Case, storing values like:
- “CUST-2024-9876”
- “CUST-2025-1234”
Agents often ask: “Can we show just the last 4 digits in the Case header?” You don’t want a Flow or Apex for this. A Formula Field does the job.
Step-by-Step Formula
- Go to Object Manager → Case.
- Create a new Formula (Text) field called Customer_ID_Last_4__c.
- Use this formula:
MID(Customer_ID__c, LEN(Customer_ID__c) - 3, 4)

Explanation:
- LEN(Customer_ID__c) returns the total number of characters.
- LEN(Customer_ID__c) – 3 gives you the starting point 3 characters from the end (so you get the last 4 total).
- 4 Characters are extracted from that point.
If you’re not familiar with how string length works, check the LEN function in the Salesforce article first.

You can then reuse this pattern in Validation Rules or formulas inside Flows (like in a record-triggered Flow in Salesforce).
Example: Extract Domain from Email Address
Consider an HR team managing employee records with an Email__c field on a custom object Employee__c. They want a quick way to see which email domain each employee uses (e.g., gmail.com, company.com) without manually scanning emails.
You can build a formula field called Email_Domain__c using FIND and MID.
Step-by-Step Formula
- Go to Object Manager → your custom object (e.g., Employee__c).
- Create a Formula (Text) field.
- Use this formula:
MID(
Email__c,
FIND("@", Email__c) + 1,
LEN(Email__c) - FIND("@", Email__c)
)

What’s happening:
- FIND(“@”, Email__c) returns the position of
@. - FIND(“@”, Email__c) + 1 moves one character forward to start at the first letter of the domain.
- LEN(Email__c) – FIND(“@”, Email__c) calculates how many characters to grab until the end of the string.
Now you can use this domain field in reports, filters, or even Validation Rules to ensure only company domains are allowed. Pair this with Validation Rules in Salesforce to enforce specific formats.
Example: Use MID in a Validation Rule
The MID function in Salesforce is not only for display formulas; you can also use it inside Validation Rules to block bad data before it gets saved.
Say your sales operations team wants all Opportunity Names to start with a 3-letter region code followed by a dash (like US-, EMEA-, APAC-). You can validate that the 4th character is a dash, and that the first three are uppercase letters.
Here’s a simple Validation Rule that at least checks for the dash in the 4th position:
- Go to Object Manager → Opportunity.
- Open Validation Rules and click New.
- Rule Name: Check_Region_Code_Format.
- Error Condition Formula:
MID(Name, 4, 1) <> "-"
- Error Message:
Opportunity Name must start with a 3-letter region code followed by a dash (e.g., “US-” or “EMEA-“).
To get more advanced, you can combine this with REGEX and other logical functions, such as AND and OR.
Using MID in Salesforce Flow Formulas
You can also use MID in Flow formulas when you’re building Screen Flows or Record-Triggered Flows. For example, you might have a Screen Flow that asks a user to paste a “composite code” like CUST-IND-2025-01 and then splits it into separate fields.
If you’re new to Flow, start with Introduction to Flows in Salesforce and Create a Salesforce Screen Flow.
Example Formula in Flow
Let’s say your Screen has an input text component called Composite_Code. You create three formula resources:
- Cust_Prefix:MID({!Composite_Code}, 1, 4)
- Region_Code:MID({!Composite_Code}, 6, 3)
- Year_Code:MID({!Composite_Code}, 10, 4)
Then you use an Assignment or Create Records element to set these values on the record you’re creating. You can explore more Flow techniques such as Use formulas in Salesforce Flow and Get current record Id in Salesforce Flows to build richer solutions.
Things to Keep in Mind
- Watch your indexes. Remember that Salesforce string positions start at 1, not 0. A one-off error in start_num is one of the most common MID mistakes.
- Validate your assumptions. MID is safest when your source text follows a consistent pattern. If users can change the pattern, consider adding Validation Rules or using ISBLANK and LEN checks.
- Handle blank values. Wrap your MID calls in conditions like IF(ISBLANK(Text_Field__c), “”, MID(…)) so you don’t end up with weird results or error messages in formula fields.
- Use FIND for dynamic positions. Hardcoding start_num only works if the prefix has a fixed length. For anything involving delimiters like
-or @, combine FIND with MID. - Test with edge cases. Always test your formula with short strings, long strings, missing delimiters, and unexpected formats before rolling it out to real users.
- Keep formulas readable. When MID formulas get long, break them into multiple formula fields or Flow formula resources, similar to how you’d refactor a long Apex method for clarity.
Frequently Asked Questions
How does the MID function work in Salesforce formulas?
The MID function in Salesforce takes a text value, a starting position, and a length, then returns only that portion of the text. You can use it in Formula Fields, Validation Rules, Workflow Rules, and Flows whenever you need a substring. It’s especially handy when the substring is in the middle of the original string, not at the start or end.
What is the difference between MID, LEFT, and RIGHT in Salesforce?
LEFT returns characters from the beginning of a string, RIGHT returns characters from the end, and MID returns characters from any position in the middle. For fixed prefixes or suffixes, LEFT and RIGHT are usually simpler to read. For patterns where the useful part sits between other information (like codes between dashes), MID is the best choice.
Can I use the MID function in a Validation Rule?
Yes, you can use MID in Validation Rules to check specific positions within a text field. For example, you can ensure the 4th character of an Opportunity Name is a dash or verify that a certain segment matches a required code. Pair it with functions like ISBLANK, LEN, or REGEX for stronger data validation.
How do I avoid errors when using MID with empty fields?
Wrap your MID expression with a blank check. A common pattern is:
IF(ISBLANK(Text_Field__c), “”, MID(Text_Field__c, 5, 3)). This way, if the field is empty, the formula returns an empty string rather than attempting to process a substring of a blank value. This makes your formulas more user-friendly and safer.
Can I combine MID with other text functions in Salesforce?
Absolutely. The most common combinations are MID + FIND (to locate dynamic start positions), MID + LEN (to work backward from the end), and MID + TRIM (to clean up spaces). For example, extracting domains from emails or codes from the middle of a string typically uses a mix of these functions.
Is MID available in Salesforce Flow and Process Builder?
Yes, the same MID function works in Flow formula resources and historically worked in Process Builder formulas as well. While Salesforce is moving towards Flow instead of Process Builder, the formula syntax inside Flow remains very similar to formula fields, so anything you do with MID in a field formula can usually be replicated in a Flow formula.
Conclusion
We walked through how the MID function in Salesforce works, common patterns, and practical examples for formula fields, validation rules, and flows. The best approach is to start with simple patterns, test with real data, and then layer in functions like FIND and LEN as your scenarios get more complex. I hope you found this article helpful.
You May Also Like
- LEFT function in Salesforce
- RIGHT function in Salesforce
- LEN function in Salesforce
- TRIM function in Salesforce
- Text function in Salesforce
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.