1

Is there a way I can test with an IF statement for a null object. Using an IF (obj==null) throws the error.

How can I account for these? With SOQL, I use a list and check the number. However, the return type for this map is an ID. I don't know how to check that. I'd also like to not use a list if possible.

 if (Schema.SObjectType.apm_applications__c.getRecordTypeInfosByName().get('NonExistentRecordType').getRecordTypeId()==null){
   system.debug('should be print ID ->' +
      string.valueof(Schema.SObjectType.apm_applications__c.getRecordTypeInfosByName().get('RecordTypeExists').getRecordTypeId()));  
 }

1 Answer 1

4

The problem is that you're not checking for null in time. There's no need to use try-catch (and you shouldn't, because they are incredibly expensive CPU-wise). The appropriate way would be to do this:

Map<String, RecordTypeInfo> apmRecordTypes = Schema.SObjectType.apm_applications__c.getRecordTypeInfosByName();
...
if(apmRecordTypes.containsKey('SomeRecordTypeName')) {
    Id recordTypeId = apmRecordTypes.get('SomeRecordTypeName').getRecordTypeId();
    ...
0

You must log in to answer this question.

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