Tuesday, August 25, 2015

RecordType Utility Class


  How to get RecordTypeId dynamically by sending Object and RecordType Name ?
 
 
    Id rt = RecordTypeUtil.recordTypeId('Account','AccountDraft');
    // here you will get  accountDraft Record type id
   
     controller:
   
     public class RecordTypeUtil {
    Public static id recordTypeId(string obj,string recName){
        id recTypeId;
        if(obj!= null && recName != null){
            recTypeId= Schema.getGlobalDescribe().get(obj).getDescribe().getRecordTypeInfosByName().get(recName).getRecordTypeId();
        } 
        return recTypeId; 
     }
    }

      Test class:
     
      @isTest
    private class RecordTypeUtil_Test {
        static testMethod void RTTestMethod() {
        string obj = 'Opportunity';
        string rtType = 'JanaKisan DE';
        ID rtId = RecordTypeUtil.recordTypeId(obj,rtType);
    }      
    }

No comments:

Post a Comment