Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am trying to extend a apex metadata coding example for retrieving the record types for all objects in my org. I know this is basic but I am working my way through coding examples for my 501 certification and stuck on this one. Using this code:

Schema.DescribeSObjectResult sobj = Lead.SObjectType.getDescribe();
List<Schema.RecordTypeInfo> recordTypes = sobj.getRecordTypeInfos();

for (Schema.RecordTypeInfo recordType : recordTypes) {
    system.debug(recordType.getName());
}

I would like to replace 'Lead' in line 1 with a List of strings called objNameList, which contains all object names.

thanks in advance !

share|improve this question

3 Answers 3

up vote 8 down vote accepted

Depending on the number of objects you have, it may be more efficient to perform a query instead. If you're truly interested in all objects and their record types, you can do this:

RecordType[] recordtypes = [SELECT Id, Name, DeveloperName, IsActive, SObjectType FROM RecordType];

This uses a SOQL query, and is so limited as such (100 queries per transaction, 50k rows in a normal context). Other fields are available as well. It doesn't include some information that RecordTypeInfo does, however, but would let you find record types for more than 100 objects (the limit for describe calls in a transaction).

You can use filters on this object (such as by a list of SObjectTypes, or just those that are active or inactive), and there are additional fields not mentioned in this answer, but are mentioned in the RecordType documentation.

share|improve this answer

Its always better to use recordType developer name and hence i prefer doing a query and storing it in a Map and retrieving it by developer name for Recordtypes as developer name once record type is saved ,it remains constant even if recprdtype label changes .

But to answer your question here is the sample code for the same

public static ID getRecordTypeId(String ObjectType, String RecordTypeLabel){   
  SObject OBJ;  
  // Describing Schema 
  Schema.SObjectType sobj = Schema.getGlobalDescribe().get(ObjectType);  
  if (sobj != null){  
        OBJ = sobj.newSObject();  
    // Describing Object   
        Schema.DescribeSObjectResult DesRes = OBJ.getSObjectType().getDescribe();   
        if (DesRes != null){  
                Map<string,schema.recordtypeinfo> RecordTypeMap = DesRes.getRecordTypeInfosByName();                    
            if (RecordTypeMap != null){  
                Schema.RecordTypeInfo RecordTypeRes = RecordTypeMap.get(RecordTypeLabel);  
                if (RecordTypeRes != null){  
                    return RecordTypeRes.getRecordTypeId();  
                }  
            }  
        }  
    }  
        return null;  
}  

Example of how to use

acc.RecordTypeId=Utility.getRecordTypeId('Account','recrdtypelabel');
share|improve this answer

This isn't 'production' worthy as it's not very efficient but it'll get you what you're after, at least for an example.

The related documentation:

Watch out for limits on the object describes when doing this type of operation.


// get the object tokens for all of the objects in the org
Map<String, Schema.SObjectType> globalDescribeMap = Schema.getGlobalDescribe(); 

// create a list of object names to pull from the describe map
List<String> objectNamesList = 'Account,Contact,Lead'.split(',');

// iterate over the object names and describe these objects
for (String objectName : objectNamesList) {

    // spit out the name of this object
    system.debug('Object Name: ' + objectName);

    // Get the sObject describe result for the Account object
    Schema.DescribeSObjectResult sobj = globalDescribeMap.get(objectName).getDescribe();

    List<Schema.RecordTypeInfo> recordTypes = sobj.getRecordTypeInfos();

    // output the recordtype name
    for (Schema.RecordTypeInfo recordType : recordTypes) {
        system.debug(recordType.getName());
    }
}
share|improve this answer
    
appreciate your reply but anonymous execution of this code in Force.com IDE generated the following error: 'Compile error at line 14 column 41 Expression of type Schema.SObjectType has no member named sObjectType' –  bruce frager Apr 21 at 18:09
    
Corrected the example, there was an extra 'sObjectType' in it left over from some example code. Change line 14 to this: Schema.DescribeSObjectResult sobj = globalDescribeMap.get(objectName).getDescribe(); –  Mark Pond Apr 21 at 21:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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