Friday, 17 August 2018

How to get Record Type developer name in Apex ?

Hi All,

Before summer 18, we used to write SOQL query to get developer name of a particular record type, now SOQL query is not required. 

This is my earlier post where recordtypeinfo method has been explained. What can we do using those methods.

1. How can we get Record Type Id of an object ?

    a.  Lets assume we know the record type name. say its "Inquiry".

Id objectRecordTypeId = Schema.SObjectType.CASE.getRecordTypeInfosByName().get('Inquiry').getRecordTypeId();
System.debug('objectRecordTypeId ---'+objectRecordTypeId ); 

    b.  Lets assume we know the record type developer name, say its "DevInquiry".

Id objectRecordTypeId = Schema.SObjectType.CASE.getRecordTypeInfosByDeveloperName().get('DevInquiry').getRecordTypeId();
System.debug('objectRecordTypeId ---'+objectRecordTypeId );

2.  How can we get record type developer name ? 

     a. Lets assume we know the record type name. say its "Inquiry".

String developerName = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Inquiry').getDeveloperName();

Same code can be broken into following.

String recordTypename = 'Inquiry';

Schema.DescribeSObjectResult caseDesribe = Schema.SObjectType.Case;
Map<String,Schema.RecordTypeInfo> rtMapByName = caseDesribe.getRecordTypeInfosByName();
Schema.RecordTypeInfo rtByName =  rtMapByName.get(recordTypename);
String RecordTypeDeveloperName = rtByName.getDeveloperName();
System.debug('RecordTypeDeveloperName --'+RecordTypeDeveloperName ); 

  b.  Lets Assume we know recordType id.

String developerName = Schema.SObjectType.Case.getRecordTypeInfosById().get('012w0000000kc2XAAQ').getDeveloperName();

Please let me know if you have any confusion, I would be happy to help.

Maintain Your Platform Developer I Certification for Summer ’18


Susmitha Bommepalli said...

Very nice information. I would like to appreciate you. Keep it up!
Best Data Science Online Training Institute In Hyderabad | Online Data Science Training
Data Science Online Training Institute In Hyderabad
Data science online training in hyderabad
Best data science training in hyderabad

Unknown said...

Best selenium online training institute `

supreet said...

Awesome blog. It was very informative. I would like to appreciate you. Keep updated like this best sap simple finance online training institute in hyderabad

unid said...

Thank you very much. I am about to write this post when doing code but I bump into this blog. I will my post and link to this later.

davidsmith said...

Salesforce Marketing Cloud Email Specialist dumps 2019 Actually its very nice site! this is my first visit on this site my friend gave the link of this site! and site is really great

Salesforce Developer said...

Thanks a lot for explaining practically. Really Appreciable!
Integrate Salesforce with third party software seamlessly and smoothly with the help of Atocloud.

unknown said...

Hiiii....Thank you so much for sharing Great information....Good post....Keep move on....
Best Salesforce Training Institutes in Hyderabad