Saturday, 1 November 2014

How would we get recordtype id and recordtype name of an particular Object?

Hi All,

Sometimes it may require to know the recordtypeid and recordtypename of an object in apex class.This is post is all about to make you comfortable with Recordtype methods.I am assuming you have concept of recordtype. Below is few code snippet to make you comfortable with recordtype method.

RecordTypeInfo is a class contains methods for accessing recordtype information for an sObject with associated recordtype.

getRecordTypeInfosByName(); - Returns maps that associate RecordTypeInfo with  record labels.

getRecordTypeInfosById();- Returns maps that associate RecordTypeInfo with  record Id.

getname():- Returns recordtype name.

getRecordTypeId() :- Returns recordtype id.

isAvailable() :-Returns true if this record type is available to the current user, false otherwise

isDefaultRecordTypeMapping() :-Returns true if this is the default record type mapping, false otherwise.

Here Contact is the standard object and Service Contact is the recordtype name.

Schema.DescribeSObjectResult contactDescribe = Schema.SObjectType.Contact;  
        Map<String,Schema.RecordTypeInfo> rtMapByName = contactDescribe.getRecordTypeInfosByName();  
        Schema.RecordTypeInfo rtName = rtMapByName.get('Service Contact');
        system.debug('hiiiii>>>>'+rtName.getRecordTypeId());--- It prints recordtype Id
system.debug('hiiiiihelllooo>>>>'+rtName.getName());--It prints recordtype name.

No comments: