6

Shouldn't I be able to get the Record Type name with RecordType.Name ? In the following code I am able to get the ID by using RecordType.ID - but I get a null value in the string if I use RecordType.Name :

trigger AutoCreateSubOnAccount on Account (after insert, after update) {
         public string type{get;set;}
List <SubsidiariesAndBrands__c> subToInsert = new List <SubsidiariesAndBrands__c> ();

    for (Account o : Trigger.new) { 

        type = o.RecordTypeId;

Thank you very much.

2 Answers 2

7

What Doug B said is correct. RecordTypeId works for you because that's the actual field on Account. Its a lookup to RecordType, and as with all lookup fields in a trigger, to get the related value, you need to query for it.

However, you can probably get away with this, before your loop:

Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Account.getRecordTypeInfosById();

And then inside your for loop do:

type = rtMap.get(o.RecordTypeId).getName();
5
  • So Daniel - is that really the only way for me to get the name of the RecordType ? (not the ID).
    – Zoom_v
    Commented Jul 8, 2013 at 19:41
  • Well, no, you can still issue a SOQL query: select name from RecordType where SobjectType = 'Account' and Id in :set_containing_RecordTypeIDs but that will use up one SOQL query, and in Apex it's better to avoid issuing unnecessary queries because of governor limits.
    – Daniel N
    Commented Jul 9, 2013 at 23:22
  • 1
    I just thought of something else: you can create a formula field on Account, with the value of: RecordType.Name, that way you'll have access to it in the trigger, and you won't have to query for the Record Type names.
    – Daniel N
    Commented Jul 10, 2013 at 3:33
  • I was just thinking possbily doing this before reading this Daniel. But it just seemed like such a klunky way of doing it. But hey, it works ! I guess that's all that matters. Thanks for all of your input Daniel. I really appreciate it !
    – Zoom_v
    Commented Jul 10, 2013 at 14:20
  • How we can identify the recordTypeName associated with the Case ? Commented Aug 26, 2017 at 10:23
4

When you are in a trigger, you get all the values on the object that is being triggered. In this case you are in the Account trigger, and recordtypeid is a field so you get that. You do NOT get the fields of related object (in this case the related recordtype object) unless you specifically query for them.

The approach could be to collect all the recordtypeids referenced in the trigger and query them into a map at the top of the trigger.

5
  • I'm confused what you are saying. I am not attempting to get a field from a related object, just the Account object which the trigger is running on. Shouldn't I be able to get Recordtype.Name if I can get RecordTypeID ? Btw - I am attempting to do this in order to include the Record Type in an IF statement later in the trigger. The code works properly when I use the RecordTypeID and put in the ID, but I want to refer to the RecordTypeName, not RecordTypeID because this is all in a sandbox and I'm assuming the RecordTypeID will change once we deploy to production.
    – Zoom_v
    Commented Jul 3, 2013 at 19:19
  • 1
    The related object Doug refers to is Recordtype. The field Account.RecordtypeId defines what Recordtype the Account is related to. So you have access to the field RecordtypeId, but can't look at the Name field on the related Recordtype object. Commented Jul 3, 2013 at 21:16
  • Exactly. The dot in between recordtype and name is another way of telling that you are referring to a field on a related object rather than a field of Account directly
    – Doug B
    Commented Jul 3, 2013 at 22:43
  • So, there's no simple way for me to get the RecordType name ? I really don't want to have to use the Record Type ID because it won't work once I deploy to production...right ?
    – Zoom_v
    Commented Jul 8, 2013 at 19:42
  • 1
    Well, you can query the recordtype object at the start of your trigger and get the names for all the relevant record type IDs (those accounts that are in the trigger). That's probably the easiest solution. Record Type IDs will be the same where the Record Type exists in Production and has been copied across to the Sandbox but they won't be the same for new record types you've created in the sandbox but not yet deployed.
    – Doug B
    Commented Jul 8, 2013 at 20:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .