May 222015

Salesforce – How to access Record Type name in a trigger?

When you are going through loop in a trigger you don’t have access to Record Type name like RecordType.Name, you can only access Id as RecordTypeId

trigger TriggerName on Account (after insert, after update) {
    for (Account o : Trigger.new) {
    type = o.RecordTypeId;
}

RecordTypeId works, because that’s the actual field on Account (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 also use before your loop

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

and then inside your loop:

type = rtMap.get(o.RecordTypeId).getName();
Tagged with , , , 0 comments

Leave a Reply

Your email address will not be published. Please enter your name, email and a comment.