Get Record Type Id in Salesforce Apex

In Salesforce, a Record Type is used to offer different business processes, page layouts, and picklist values for the same object.

Record types can be used to differentiate the data in the same object. In this article, we will be discussing how to retrieve the Record Type Id in Salesforce Apex.

Understanding Record Type Id in Salesforce

In Salesforce, each record type has a unique Id that can be used to differentiate between different record types on the same object.

The Record Type Id is a 15-character alphanumeric Id that can be used to retrieve the record type information for a given object. This Id is useful for querying, validating and updating the record based on the record type.

Record Type Id is also useful when you want to restrict users to view, edit or create only specific record types.

Retrieving the Record Type Id can be done using various methods, including SOQL, DescribeSObjectResult, RecordTypeInfo class, and ApexPages.StandardController.

In this article, we will be discussing all these methods in detail and provide examples of how to use them.

Get Record Type Id By recordTypeName using SOQL

When working with Salesforce records, it’s sometimes necessary to obtain the Record Type Id based on the Record Type name. One way to do this is by using a SOQL query to retrieve the Id from the RecordType object.

The RecordType the object represents a record type in Salesforce, and it has a number of fields such as Id, Name, IsActive, etc. In order to retrieve the Id of a Record Type based on its name, you can use the following SOQL query:

//SELECT Id FROM RecordType WHERE SObjectType = 'YourObjectName' AND Name = 'YourRecordTypeName'


RecordType RecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'RecordTypeName'];
System.debug(RecordTypeId.Id);

Get Record Type Id By Record Type Name or API Name

Id RecordTypeId = Schema.getGlobalDescribe().get('objectAPIName').getDescribe().getRecordTypeInfosByName().get('RecordTypeName').getRecordTypeId();
System.debug(RecordTypeId);

Get Record Type Id With Name by objectAPIName

One way to retrieve the Record Type Id in Salesforce Apex is by using the Record Type Name and the Object API Name.

String objectAPIName = 'Account' ;
Schema.DescribeSObjectResult sobjectResult = Schema.getGlobalDescribe().get(objectAPIName).getDescribe();
List<Schema.RecordTypeInfo> recordTypeInfo = sobjectResult.getRecordTypeInfos();
Map<String,Id> mapOfRecordTypeIdWithName = new Map<String,Id>();
for(Schema.RecordTypeInfo info : recordTypeInfo){
      mapOfRecordTypeIdWithName.put(info.getName(),info.getRecordTypeId());
}
System.debug('Record Type Id With Name' + mapOfRecordTypeIdWithName);

It is worth noting that, if you are working with standard objects, you could use the RecordTypeInfo.getDeveloperName() instead of RecordTypeInfo.getName() which is more reliable.

Please keep in mind that if you are working with custom objects, you should use the RecordType.DeveloperName instead of RecordType.Name when querying the recordType.

Get RecordTypeId In LWC

This method is useful when you want to get all the record types in LWC component. It uses lightning/uiObjectInfoApi and @salesforce/schema/ with apex wire adapter.

You can use the below sample code to retrieve the record type id.

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

export default class SavePdf extends LightningElement {
@track recordTypeId;

@wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
accObjectInfo({data, error}) {
   if(data) {
      const rtInfos = data.recordTypeInfos;
      let rtValues = Object.values(rtInfos);
      for(let i = 0; i < rtValues.length; i++) {
         this.recordTypeId  = rtValues[i].recordTypeId;
      }
      console.log(this.recordTypeId);
   }else if(error) {
     window.console.log('Error'+JSON.stringify(error));
   }
 }
}

Get RecordType Id By recordId In LWC

This method allows you to retrieve the record type id of a Salesforce record using the record id.

It makes use of the lightning/uiRecordApi module to retrieve the record data and the @salesforce/schema/Account.RecordTypeId field to access the record type id.

import {LightningElement, api, track, wire} from 'lwc';
import { getRecord } from "lightning/uiRecordApi";
import ACCOUNT_RECORDTYPE_FIELD from '@salesforce/schema/Account.RecordTypeId';
export default class SavePdf extends LightningElement {
@track account;
@track recordTypeId;
@wire(getRecord, { recordId: '$recordId', fields: [ACCOUNT_RECORDTYPE_FIELD] })
 getAccount({ error, data }){
   if(data){
      var result = JSON.parse(JSON.stringify(data));
      console.log('acc data: ', result);
      this.account = result;
      this.recordTypeId =
                     result.fields.RecordTypeId.value;
      console.log('recordTypeId' + this.recordTypeId);        
    }else if(error) {
        var result = JSON.parse(JSON.stringify(error));
        console.log('error: ', result);
    }
  };
}

It also uses track properties for account and recordTypeId, so any updates to these properties will cause the component to re-render. With this method, you can easily retrieve the record type id of any record using its record id.

Related Question

https://developer.salesforce.com/forums/?id=906F0000000937iIAA

https://salesforce.stackexchange.com/questions/195378/getting-the-record-type-id-via-apex

Learn more about Record Types In Salesforce


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Index