How to get record type id in salesforce

A typical use case for salesforce developers is to dynamically get record type id from salesforce to either use in apex, lightning web components (lwc) based on the record type name.

Here we have compiled a list of code snippets developers can use in various use-cases.

Find record type id from setup menu


The record type id can be retrieved from the setup menu. The record type page will have its id in the url.

For example for account, navigate to set up -> Object Manager -> Account -> Record Types -> 'Company'

The characters after /RecordTypes/ in the url is the record type id

Get record type id from set up menu in salesforce

Find record type id in apex without using SOQL


Use this approach to get the record type for an object in apex. This does not use SOQL and you will be within SOQL query limits when you use this approach.


public static final companyRecTypeId = 
Schema.SObjectType.Account.getRecordTypeInfosByName().get('Company').getRecordTypeId();

Note: This does consume system resources so use this outside of loops for a better performance.

Find record type id using SOQL


Use this approach when you want to quickly query a record type id from developer console/ workbench/ vs code. As this requires a SOQL query, it is not recommended to use in apex.

public static final companyRecTypeId =
[Select id,name from RecordType where RecordType.Name='Company' AND SobjectType='Account'];

Here is how it can be used in the developer console query editor window

Get record type id in developer console using SOQL

Find record type id in LWC using uiObjectInfoApi


The lightning web components can retrieve record type information via uiObjectInfoApi, no apex code is necessary. The uiObjectInfoApi returns the whole object metadata information including fields, child relationships, dependent fields etc..

Here is the code to retrieve the record type info using uiObjectInfoApi


import { LightningElement,wire } from "lwc";
import { getObjectInfo } from "lightning/uiObjectInfoApi";
import ACCOUNT_OBJECT from "@salesforce/schema/Account";

export default class RecordFormWithRecordType extends LightningElement {
    @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT }) wireAccountData(objectInfo,error){
        if(objectInfo){
            
            let recordTypeInfo = objectInfo?.data?.recordTypeInfos;
            if(recordTypeInfo){
                let companyRecType = Object.keys(recordTypeInfo).find(rtype=>(recordTypeInfo[rtype].name === 'Company'));
                
            }
        }
    }
}
        

What is Decodeforce?

Decodeforce is a dedicated platform aimed at helping Salesforce developers improve their Apex coding skills by solving real-world programming challenges

Solve a number of practice scenarios in DML, SOQL, trigger and many more on Decodeforce.

String

Learn to use the apex string data type to validate and manipulate string variables in salesforce.

Solve
Date

Play with Apex date methods to gain hands-on experience in manipulating date data type in Salesforce.

Solve
List

Practice working with Apex lists to enhance your Salesforce development skills.

Solve
Map

Practice working with apex Maps to handle large and complex data for business requirements.

Solve
Practice apex collection control flow statements
Collections & Control flow

Discover how to utilize various conditional statements and collection types in Apex..

Solve
Practice apex SOQL and DML statements
SOQL & DML

Explore SOQL queries, database manipulation statements (DML), and how to retrieve and update records in Apex.

Solve
Practice apex triggers
Apex Triggers

Master apex triggers by leveraging trigger events and context variables to address real business scenarios.

Solve
Practice apex governor limits
Governor limits

Resolve apex code that hits the governor limits and discover techniques to prevent such occurrences in your projects.

Solve
Practice apex REST API
Apex REST API

Get hands-on experience with apex REST APIs to connect with external systems to send /receive data and updates records in Salesforce

Coming soon!
Lightning Web Components
Lightning Web Components

Develop and get hands on experience in creating Lightning Web Components to create modern user interface for salesforce apps.

Solve

To know more click the Get started button below