3

I am developing a managed package. i am accessing leads recordtypeid in my code

if (rtypeMap.containsKey(Leads.recordtypeid) && 
    rtypeMap.get(Leads.recordtypeid) != null && 
    String.isNotblank(rtypeMap.get(Leads.recordtypeid).CMFL__Default_Converted_Status__c)){ 
    //Do something
}

where Leads is the Lead record Now this results in a failure while installing in org without recordtype for Leads.

Leads Missing Organization Feature: Lead.RecordType

Is there a way i can make sure that it works seamless while installing in orgs without recordtypes set for leads?

  • @eyescream, question does not looks duplicate to me :) – Raul Oct 4 '17 at 11:49
  • @RahulSharma I'd say zachelrath's answer is great. Very detailed, covers lessons learned like availability of that particular rec type for given user... Sure, this question's scope is smaller but if I can point people to strong, well-tested solution... Easy to check afterwards if there are 0 rec types available. Presence of rtypeMap in snippet suggests Prady already knows about the rec type describe calls :) For me a perfect answer for just "does it exist" would include an isAccessible() check, not only a field describe ;) – eyescream Oct 4 '17 at 13:01
  • Had seen that question before answering and it seemed different, makes sense now! – Raul Oct 4 '17 at 13:02
6

Two options:

  1. Use describe call to validate if RecordTypeId for a object is available. (Recommended method).

    Usually I would use following utility call:

    public static Boolean isAvailableField(String sObjectAPIName, String fieldAPIName) {
        // call to get the appropriate object's describe
        Map<String, Schema.SObjectType> mapGlobalDescribe = Schema.getGlobalDescribe();
        Schema.SObjectType objSObjectType = mapGlobalDescribe.get(sObjectAPIName);
    
        // field describe
        Schema.DescribeSObjectResult objectDescribe = objSObjectType.getDescribe();
        Map<String, Schema.SObjectField> mapFieldDescribe = 
            objSObjectType.getDescribe().fields.getMap();
    
        // check if field exists in describe call
        return mapFieldDescribe.containsKey(fieldAPIName);
    }
    

    And use it to check in my code:

    // check if field is available
    if(isAvailableField('Lead', 'RecordTypeId')) {
        // using SObject.get method to avoid issues when field is not available
        String recordTypeId = Leads.get('RecordTypeId');
        if (rtypeMap.containsKey(recordTypeId) && 
            rtypeMap.get(recordTypeId) != null && 
            String.isNotblank(rtypeMap.get(recordTypeId).CMFL__Default_Converted_Status__c)){ 
            //Do something
        }
    }
    
  2. Quick and dirty method: Check with doing SObject.get on RecordTypeId field, like below:

    Boolean doesLeadHasRecordType = false;
    // in try catch check if record type is available
    try {
        Object recordtypeId = new Account().get('recordtypeId');
        doesLeadHasRecordType = true;
    } catch(Exception objEx) {
        System.debug('objEx: ' + objEx);
    }
    // using this flag, we know if the object has a record type
    System.debug('doesLeadHasRecordType: ' + doesLeadHasRecordType);
    

Note: For package development, always use SObject get and put methods along with describe calls to make sure features are available before checking.

| improve this answer | |
  • Thanks, i will check if this resolves the issue. i am sure this should do it – Prady Oct 4 '17 at 10:10
  • 1
    Added better example with the first option. Hope it helps! – Raul Oct 4 '17 at 10:22

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