Tag Archives: Record Type

Select Record Types For Current User Profile On Specific Object

Sample Code:

//Get all record types on Account object
List<Schema.RecordTypeInfo> recordTypeInfoList = Account.SObjectType.getDescribe().getRecordTypeInfos();
List<SelectOption> recordTypes = new List<SelectOption>();
for(RecordTypeInfo info: recordTypeInfoList) {
    //Check record type is available for current user profile
    if(info.isAvailable()) {
        if(info.getName() != 'Master' && info.getName().trim() != ''){
            recordTypes.add(new SelectOption(info.getRecordTypeId(), info.getName()));
        }
    }
}
System.debug('recordTypes-' + recordTypes);

Add Record Type in Test Class

Sample Code:

@isTest()
public class TestUtils {
    
    private static testmethod  void CreateAccount(){
        //Get Account Record Type Id
        Id accountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('DeveloperNameOfRecordType').getRecordTypeId();
        
        //Create Account
        Account acc = new Account();
        acc.Name ='Test Account';
        acc.Industry = 'Retail';
        acc.AccountNumber = '123456';
        acc.RecordTypeId = accountRecordTypeId;
        Insert acc;
    }
}