How to Query the RecordTypeId dynamically for use in Apex Trigger / Class

8:36 PM


Reference: http://salesforce.stackexchange.com/questions/11968/what-would-be-the-best-approach-to-get-the-recordtype-id

Salesforce Documentation: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_recordtype.htm

You might have to set or update the RecordType of records dynamically using Apex Triggers. In such cases, the RecordTypeId should be queried and fetched at runtime using the below two approaches.

SOQL:
RecordType RecType = [Select Id From RecordType  Where SobjectType = 'Contact' and DeveloperName = 'Strategic_Contact'];


The Developer name corresponds to the RecordType Name as shown below.



The other method is to use the Describe call.

Map rtMapByName = d.getRecordTypeInfosByName();
Schema.RecordTypeInfo rtByName =  rtMapByName.get('Strategic_Contact');

0 comments