| | |

Salesforce Developer Quick Tip – How to Dynamically Get Record Type IDs in Apex


As a Salesforce developer, you will inevitably face a scenario where you need to assign a specific Record Type ID to a record you are creating or updating in Apex.

The “quick and dirty” solution is to copy the ID from the URL and hardcode it directly into your class.

Don’t do this. Hardcoding IDs makes your code brittle; it breaks deployments between sandboxes (where IDs often differ) and makes maintenance a nightmare.

In this post, we’re breaking down the proper, dynamic way to retrieve Record Type IDs in Apex, by using the Schema class.

Option 1: The Standard Approach (Best for Known Objects)

If you know exactly which object you are working with (e.g., you are writing an AccountTriggerHandler), this is the most efficient method. It utilizes SObjectType to fetch the ID based on the Record Type’s label (Name).

The Syntax:

Apex

Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName()
                  .get('My_Record_Type_Name')
                  .getRecordTypeId();

Breakdown:

  1. Schema.SObjectType.Account: Targets the specific object (replace Account with your object’s API name).
  2. .getRecordTypeInfosByName(): Returns a map of all record types on that object.
  3. .get('My_Record_Type_Name'): Selects the specific record type by its Label/Name.
  4. .getRecordTypeId(): Returns the actual 15/18-character ID.

Note: This matches on the Record Type Name (Label), not the DeveloperName.

Option 2: The Fully Dynamic Approach (For Abstracted Code)

Sometimes you might be writing a utility class that needs to handle any object dynamically, where you don’t know if it’s an Account, Contact, or Custom Object until runtime.

In this case, you can use the Global Describe.

Warning: This method is more resource-intensive (slower) because fetching the global describe loads metadata for the entire Org. Only use this if you truly need the flexibility of dynamic object types.

The Syntax:

Apex

String objectName = 'Account'; // This could be dynamic
String recordTypeName = 'Enterprise';

Id recordTypeId = Schema.getGlobalDescribe()
                  .get(objectName)
                  .getDescribe()
                  .getRecordTypeInfosByName()
                  .get(recordTypeName)
                  .getRecordTypeId();

Breakdown:

  1. Schema.getGlobalDescribe().get(objectName): Retrieves the SObject token dynamically based on a string.
  2. .getDescribe(): Accesses the metadata for that dynamic object.
  3. The rest of the chain follows the same pattern as Option 1.

Summary

Hardcoding IDs is a bad habit that leads to deployment errors. By using the Schema methods, you ensure your code works across all environments (Dev, QA, Production) regardless of whether the actual IDs match.

  • Use Option 1 for standard, specific object logic (Faster).
  • Use Option 2 only when the object type itself is variable (More flexible, but slower).

Need help with your Salesforce Org?

If you need help with your Salesforce org, schedule an hour of consulting time with me! I’m one of only ~500 Salesforce Certified Technical Architect’s (CTA) worldwide and I’ve spent over 30,000 hours building Salesforce implementations over the last decade!

Schedule and hour of consulting with me here!


Do you want to be the next Salesforce Certified Technical Architect (CTA)?

If you need training to help you on your journey to complete your Salesforce CTA Board then why not sign up for the cheapest Salesforce CTA course out there, with someone who has training over a million Salesforce professionals worldwide! You can check out and enroll in the course below!

Sign up for the CTA course here!

Or if a course isn’t your thing, you can always sign up for an hour long CTA study session with me here:
Schedule 1-on-1 CTA Coaching Here!


Get Coding With The Force Merch!!

We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.

Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!


Check Out More Coding With The Force Stuff!

If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!

Youtube
Patreon
Github
Facebook
Twitter
Instagram


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps

Good Non-SF Specific Development Books:

Clean Code
Clean Architecture


AI DISCLAIMER: Gemini assisted me in writing this blog post by analyzing and summarizing the contents on the YouTube video I created that is linked at the top of this post.

Similar Posts