5

I have been using the following code successfully for a few things, mostly test classes, and it has been working fine:

    String gsRecordTypeID = [SELECT id FROM RecordType WHERE name = 'G S' and sobjecttype ='Case'].Id;
    String wsRecordTypeID = [SELECT id FROM RecordType WHERE name = 'W S' and sobjecttype ='Case'].Id;
    String wsWorkOrderRecordTypeID = [SELECT id FROM RecordType WHERE name = 'WS' and sobjecttype ='WorkOrder'].Id;  

For whatever reason our test classes have stopped working (maybe a week ago). The third line in the above always reports an error:

Too Many SOQL queries (101)

I have then changed the code to the following:

     String gsRecordTypeID = Schema.SObjectType.Case.getRecordTypeInfosByName().get('G S').getRecordTypeId();
     String wsRecordTypeID = Schema.SObjectType.Case.getRecordTypeInfosByName().get('W S').getRecordTypeId();
     String wsWorkOrderRecordTypeID = Schema.SObjectType.WorkOrder.getRecordTypeInfosByName().get('W S').getRecordTypeId();

This worked fine for the third line but the 2 case record types didn't work, so I then tried this:

    String gsRecordTypeID = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('G S').getRecordTypeId();
    String wsRecordTypeID = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('W S').getRecordTypeId();
    String wsWorkOrderRecordTypeID = Schema.SObjectType.WorkOrder.getRecordTypeInfosByName().get('W S').getRecordTypeId();

Both of the last two attempts result with the error. I have also tried using the api names of the record type and this hasn't worked

System.NullPointerException: Attempt to de-reference a null object

I can get the code to test successfully by using the SOQL Selects for the Case record types and the schema. for the Work Order but I'd like to know why they don't all work with the same code? Thanks.

4 Answers 4

8
String gsRecordTypeID = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Record Type Label').getRecordTypeId();

-

String gsRecordTypeID = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('Record Type Label').getRecordTypeId();

Looks like you are passing the Record Type Name instead of Record Type Label. I executed your code and that is the only case it failed. Replacing the name with the label should work.

Also, using Schema.getGlobalDescribe() more times will result in hitting Apex CPU time limit. I'd go with the first option.

8

Please note that getting record type by it's label is tricky when you have multi-language org, because labels can depend on user's language.

The best would be to retrieve record types by using feature new in Summer18:

Schema.DescribeSObjectResult.getRecordTypeInfosByDeveloperName();
5

Expanding Wookie88's answer, here is a working example:

Id recordTypePerson = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName()
      .get('Opp_Person')
      .getRecordTypeId();
0

Dynamically get recordTypeId with recordType and objectName

Id caseRecordTypeId = Schema.getGlobalDescribe().get(ObjectName).getDescribe().getRecordTypeInfosByName().get(RecordTypeName).getRecordTypeId();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .