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

8:36 PM


Salesforce Documentation:

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.

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');