2

I am looking for a SOQL query to get the list of record type Id for a given sObject Name

10

By querying the recordType object and filtering by the sObjectType

select Id,Name from RecordType where sObjectType='Account'
  • It takes a minute for you to post a question and 2 for finding the answer. In that case I suggest you should do more research. Still good to see that you are able to answer your own question :) – Raul May 5 '17 at 7:58
  • 1
    @RahulSharma I'm just sharing some knowledge here, I am following this stack recommendation => stackoverflow.com/help/self-answer ;) – benahm May 5 '17 at 8:18
  • Ah thanks for the clarification, it was not clear from your question and answer. – Raul May 5 '17 at 8:26
10

While this can be accomplished with SOQL, SOQL queries are a precious resource and we have but a scant few in each transaction.

There is a way to get this information without using a single query by using SObjectDescribe.

// The general expression to use is 
//   Schema.SObjectType.<your SObject's API name here>.getRecordTypeInfosByName()
// I'll be using Account in this example
Map<String, Schema.RecordTypeInfo> recordTypes = Schema.SObjectType.Account.getRecordTypeInfosByName();

Id standardRecTypeId = recordTypes.get('Parent').getRecordTypeId();
Id customRecTypeId = recordTypes.get('My Record Type Display Name').getRecordTypeId();

Schema.RecordTypeInfo has a few other methods, which can be found by looking through the documentation on this class, but getRecordTypeId() is basically the only method from this class that I ever call.

  • 3
    But then you can't get unique identifiers like DeveloperName, right? – Adrian Larson May 5 '17 at 13:06
  • @AdrianLarson indeed. This method doesn't give access to BusinessProcessId, DeveloperName, IsPersonType, Description, or Namespace_Prefix. You'd need to query if you happened to need any of those fields, but in that case it's probably better to use something like the org cache to avoid querying over and over. – Derek F May 5 '17 at 13:20
2

When working in a new org, I often write a caching mechanism that gets them all in one go. It's not a ton of query rows or processing time and saves queries in the long run:

public with sharing class RecordTypeCache
{
    public static Map<String, RecordType> get(SObjectType key)
    {
        return cache.containsKey(key) ? cache.get(key) : new Map<String, RecordType>();
    }
    public static RecordType get(SObjectType key, String developerName)
    {
        return get(key).get(developerName);
    }

    static Map<SObjectType, Map<String, RecordType>> cache
    {
        get
        {
            if (cache == null)
            {
                cache = new Map<SObjectType, Map<String, RecordType>>();
                Map<String, SObjectType> objects = Schema.getGlobalDescribe();
                for (RecordType recordType : [SELECT DeveloperName, SObjectType FROM RecordType])
                {
                    SObjectType schemaType = objects.get(recordType.SObjectType);
                    if (!cache.containsKey(schemaType))
                        cache.put(schemaType, new Map<String, RecordType>());
                    cache.get(schemaType).put(recordType.DeveloperName, recordType);
                }
            }
            return cache;
        }
        private set;
    }
}

So if you wanted to get the List<RecordType> for a specific SObjectType, you would just do:

List<RecordType> myObjectRecordTypes = RecordTypeCache.get(MyObject__c.sObjectType).values();
0

@benahm As per your comment "I'm just sharing some knowledge here, I am following this stack recommendation => [stackoverflow.com/help/self-answer] " to @RahulSharma.

Yes, that's a good thing but the kind of question which you've posted is very basic question. If you find something new which has not been discovered earlier then you're free to post it.

I just went to Ask Question section and I tried to ask a question similar to your question and look what I found:

enter image description here

And though you've answered in SOQL, but I guess it's better to use in Apex so that you can build some logic around it.

List<RecordType> lstAccountRecordTypes = [SELECT Id, Name FROM RecordType WHERE sObjectType='Account'];
System.debug('Record Types:'+lstAccountRecordTypes.size());
  • 3
    I agree with your comment: * the kind of question which you've posted is very basic*. The level of research displayed in the question itself is quite poor, even if it was posted as a Q&A. – Adrian Larson May 5 '17 at 12:50
  • In my case I wasn't looking for how to get the record type Id in Apex, I was just looking for the fastest way to get it, cause I needed to use it in a process builder for testing purpose... – benahm May 5 '17 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.