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.