Concepts or coding lessons of Salesforce that you can implement easily

The Ultimate Guide To Get Sobject Record Type Id By Record Type Name Without Soql

how to do it in salesforce
Usually, we fetch record type with the help of Salesforce SOQL which counts in Salesforce SOQL governors limit.

Example :

 [Select id from RecordType where sObjectType = 'Account' and developerName ='CustomerAccount'].id

Instead of SOQL which counts in governors limit, you can use dynamic apex code to access RecordTypeId.
Here is a simple way to do this without any SOQL query: 

Id customerAccountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('CustomerAccount').getRecordTypeId();

Here, 'CustomerAccount' is the account`s record type name. Also, you will have to specify your SObject type name like Account, Contact, Case etc, here in this example I have mentioned Account.

Basically to avoid SOQL governor Limit we can use dynamic apex code to get RecordTypeId. Schema 

Full apex code in detail:


A normal way to access record type id using SOQL query which counts in Salesforce SOQL governors limit.

Step 1: Create apex class


public class GetRecordTypeId{
  Public ID objId{ get; set; } 
  
  public GetRecordTypeId(){
   List<RecordType> lstRecordType = [Select id from RecordType where sObjectType = 'Account' and developerName ='CustomerAccount'];
   objId = lstRecordType[0].id;
  }
}

Best way to get record type id of any object using Schema which does not count in Salesforce SOQL governors limit. 
In below code snippet, instead of hard coding the object name and record type label, I am explaining dynamic approach which gives flexibility to admin or developer.  

Step 1: Create apex class


public class GetRecordTypeId
{
 public GetRecordTypeId()
 { }

// You need to pass two parameters SObject Type and RecordType Label not the Record Type Developer Name
public static ID GetRecordTypeIdDynamically(String ObjectName, String RecordTypeLabel)

  SObject ObjSObject;
  // Declaring Schema
  Schema.SObjectType objSchemaSObjectType = Schema.getGlobalDescribe().get(ObjectName);
  if (objSchemaSObjectType != null)
  {
    ObjSObject = objSchemaSObjectType.newSObject();
    // Declaring Object which Contains methods for describing sObjects.
    Schema.DescribeSObjectResult ObjDesSObjectRes = ObjSObject.getSObjectType().getDescribe(); 
    if (ObjDesSObjectRes != null)
    {
 Map mapRecordType = ObjDesSObjectRes.getRecordTypeInfosByName();
      if (mapRecordType != null) 
      {
  Schema.RecordTypeInfo objRecordTypeRes = mapRecordType.get(RecordTypeLabel);
  if (objRecordTypeRes != null) 
  {
   return objRecordTypeRes.getRecordTypeId();
  }
      }
    }
  }
 return null;
}
}




Enjoy! If you have any questions, comments, please feel free to let me know. 
As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.

Next post: The Best Way to Retrieve Names of All Objects Using Apex

loading...

No comments:

Post a Comment