1

I am struggling with how to select record type in Apex. I have been given some examples, but I still don't get it - sorry if it already has been answered somewhere - I am very new to this, so I guess I'll ask for explicit solution on my simplified code...

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

Different try (using some advice) - doesn't work...

trigger recordTypeTest on Contact (before insert, before update) {
    Map<Id, RecordTypeInfo> recordTypes = Contact.SObjectType.getDescribe().getRecordTypeInfosById();
    String RTName;

    for(Contact con : trigger.new){
        RTName= recordTypes.get(con.RecordTypeId).getRecordTypeName();
        if(RTName == 'A')     {con.RT__c = 'A';}
        else                  {con.RT__c = 'other';}
  }
}
  • Your first try unambiguously will not work. Can you describe what behavior or error you observed on your second try? – David Reed Dec 28 '18 at 16:56
  • Further, this appears to me to be a need that can easily be met with a formula field - no Apex required.. – David Reed Dec 28 '18 at 16:58
  • I agree with @DavidReed, this seems to me a Formula Field candidate. Please put more details so that we can guide you to correct solutions – Sudipta Deb Dec 28 '18 at 17:01
  • Yes, this can be formula, but I have simplified the real case. I'd like to see how I should write it in general (best on this example so I could really get it). 2nd try - I couldn't save it because I took it from some more complex example I guess.... Error: Compile Error: Method does not exist or incorrect signature: void getRecordTypeName() from the type Schema.RecordTypeInfo at line 7 column 51 – Martin Dec 28 '18 at 17:04
  • More details... It is just an example, but what I'm trying to comprehend is "How to run pieces of code based on RecordTypeName" the easiest way. That is why I've come up with this example which I understand. – Martin Dec 28 '18 at 17:14
3

Your second approach is probably the best way. The error your are seeing

Error: Method does not exist or incorrect signature: void getRecordTypeName() from the type Schema.RecordTypeInfo at line 7 column 51

Is because the method does not exist. The docs show that the correct method name is getName()

So try:

trigger recordTypeTest on Contact (before insert, before update) {
    Map<Id, RecordTypeInfo> recordTypes = Contact.SObjectType.getDescribe().getRecordTypeInfosById();
    String RTName;

    for(Contact con : trigger.new){
        RTName= recordTypes.get(con.RecordTypeId).getName();
        if(RTName == 'A')     {con.RT__c = 'A';}
        else                  {con.RT__c = 'other';}
  }
}
  • 2
    I would like to advice that is better to use getDeveloperName() instead of getName(). – m Peixoto Dec 28 '18 at 17:40
  • I agree @mPeixoto – gNerb Dec 28 '18 at 17:42

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.