Search This Blog

Sunday, June 26, 2016

How to get recordTypeId in Test Class

Assume 'Supplier' is the recordtype in Account Object as shown in image below.

If you want to get that supplier record type id in apex,We need to first prepare a map of all recordtypes of that account object and then get our required record typeid from it.
Sample Code:
Map <String,Schema.RecordTypeInfo> recordTypeMap = Account.sObjectType.getDescribe().getRecordTypeInfosByName();
Account accountObj = new Account();
accountObj.name = 'Test Record Type';
if(recordTypeMap.containsKey('Supplier')) {
accountObj.RecordTypeId= recordTypeMap.get('Supplier').getRecordTypeId();
}
insert accountOb;
system.debug('CreatedAccountId'+accountObj.id);
Output :

1 comment:

  1. Hi, I was wondering if we can just read it directly like in
    Supplier_RecTypeId = [SELECT Id FROM RecordType WHERE name = 'Supplier' and isActive = true LIMIT 1];

    ReplyDelete