3

Is there a way I can find out what recordType the user have selected?

I have put together the apex code :

private String getRecordTypeSelectedId(String recordTypeName)
{
        String rtId;
        Map<ID, RecordType> rtMap = New Map<ID,RecordType>([Select ID, Name From RecordType Where sObjectType = 'Work_Order__c']);
        rtId = (rtMap.get(recordTypeName)).Id;
        return rtId;        
}

//calling the above method:

   String selectedRTId = getRecordTypeSelectedId('By Calendar');

I'm calling this above apex controller class from trigger (before insert, before update)

the getting this error:

execution of BeforeInsert caused by: System.StringException: Invalid id: By Calendar: External entry point

3 Answers 3

5

Since you want just the ID of the record type by name, use a describe call:

public static Id getRecordTypeId(SObjectType entity, String recordTypeName) {
    try {
        return entity
               .getDescribe()
               .getRecordTypeInfosByName()
               .get(recordTypeName)
               .getRecordTypeId();
    } catch(Exception e) {
        return null;
    }
}

This reduces your query usage, which is a governor limit, which you can then use to query actual records.

8
  • great, I'm trying to call the above method like this: String selected = getRecordTypeId(Work_Order__c, 'By Calendar'); getting this error Variable does not exist Work_Order__c
    – Nick
    May 27, 2015 at 22:07
  • 1
    The entity argument needs to be of type SObjectType . One way of providing that is to make the call like this String selected = getRecordTypeId(Work_Order__c.SObjectType, 'By Calendar');.
    – Keith C
    May 27, 2015 at 22:50
  • Thanks for your help, do you know any good blog or site where i can learn more about apex code and governor limit etc...?
    – Nick
    May 27, 2015 at 22:57
  • @sfdcfox why not make it a static method?
    – Phil B
    May 27, 2015 at 23:04
  • 1
    @AbuHamzah You already know the name of it, so I don't see why you'd want the name again. However, if the inverse were true (you had the ID and you wanted the name), you could use getRecordTypeInfosById(), which lets you get the name from an ID.
    – sfdcfox
    May 28, 2015 at 0:33
1

You're passing in the record type name and the key of your map is an ID. You should update it to make your map a map of RT Name => recordtype instead.

2
  • 1
    This answer is right enough, but I think a proper code snippet showing how it'd be done using the map would be cool.
    – sfdcfox
    May 27, 2015 at 21:51
  • @sfdcfox agreed. However it's my understanding from past posts the OP is learning SF and thought spelling it out might not be as helpful. Future visitors probably just want the answer though.
    – Phil B
    May 27, 2015 at 21:53
1

Your map has Ids as keys but you are trying to retrieve a value from it by giving the name as the key.

One option is to query out only the recordtype with that name instead of querying all recordtypes of the SObject:

private String getRecordTypeSelectedId(String recordTypeName) {
    RecordType rt = (RecordType)[Select ID, Name From RecordType Where sObjectType = 'Work_Order__c' AND Name = :recordTypeName];
    return rt.Id;        
}

Or, if you want to query out all the record types :

private String getRecordTypeSelectedId(String recordTypeName){
    String rtId;
    List<RecordType> rtList = New List<RecordType>([Select ID, Name From RecordType Where sObjectType = 'Work_Order__c']);
    for(RecordType rt : rtList) {
        if(rt.Name == recordTypeName) {
             rtId = rt.Id;
        }
    }
    return rtId;        
}
2
  • -1: Returning a String instead of an Id, using a Map when a List would suffice, casting a RecordType to a RecordType, querying all record types instead of just the one you need, not inlining the query to take advantage of for-loop query optimization, declaring variables that are never used, and not caching the results of querying all record types (which would be the only reason why you wouldn't filter the results by name to begin with).
    – sfdcfox
    May 27, 2015 at 21:50
  • @sfdcfox Thorough, instructive code review.
    – Adrian Larson
    May 27, 2015 at 22:08

You must log in to answer this question.

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