I am struggling with how to select record type in Apex. In the following example, the code never puts 'A' in RT__c field (always 'other'). There are 2 record types 'A' and 'B' (names & developer names are the same). Can you tell me what I am doing wrong?

trigger recordTypeTest on Contact (before insert, before update) {
    for(Contact con : trigger.new){
        if(con.recordType.Name ==  'A' )  {con.RT__c = 'A';}
        else                              {con.RT__c = 'other';}

marked as duplicate by sfdcfox trigger Dec 28 '18 at 15:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    In addition to the duplicate marked, I would review the documentation on getRecordTypeInfosByDeveloperName() - you don't have to run a SOQL query here to get values to compare against. – David Reed Dec 28 '18 at 15:57
  • Hi Martin, and welcome to SFSE! I've linked a few answers that should answer your question, and some of those have further links. Please take time to review this information, and if it still doesn't help you, please let us know, and we'll be glad to assist you further. – sfdcfox Dec 28 '18 at 16:01
  • I'm sorry for being a dummy, but I'm very new to Apex and I still don't get it from the examples provided. Can somebody explain it on my example please? – Martin Dec 28 '18 at 16:03
  • 1
    @MorganMarchese, thanks a lot! – Martin Jan 1 at 12:32
  • 1
    @MorganMarchese, it all makes sense. Very useful advice - helped me understand it. Thanks a lot once more! :) – Martin Jan 1 at 12:36

Browse other questions tagged or ask your own question.