Activity Forums Salesforce® Discussions How to get the Recordtype Id using Dynamic Apex in salesforce?

  • Piyush

    Member
    August 22, 2019 at 3:33 am

    Hi Prachi,

    Normally to get the RecordtypeId for any sObject we use SOQL and it will count against your limit. So below method will bypass the need of SOQL Query.

    Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe() ;
    Schema.SObjectType s = m.get(‘API_Name_Of_SObject’) ;
    Schema.DescribeSObjectResult cfrSchema = s.getDescribe() ;
    Map<String,Schema.RecordTypeInfo> RecordTypeInfo = cfrSchema.getRecordTypeInfosByName();
    Id rtId = RecordTypeInfo.get(‘Record Type Name’).getRecordTypeId();

  • Bob Nunemaker

    Member
    October 2, 2019 at 9:50 pm

    The globalDescribe solution is decent and works.  However, it is VERY slow and takes a lot of time at Enterprise size orgs.  Can really kill performance.  Instead, if you know what you want, you can use:

    string recTypeID = Schema.SobjectType.Campaign.getRecordTypeInfosByName().get('Program').getRecordTypeId();

    and this also means you could pass the Record Type name to a method needing this.  However, the object can't be dynamic, so you might need a method per Object desired to use this.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos