10

I'm trying to find out the recordType Name/Id based on the record Id.

Id myid = 'a1aV0000003DzYE'; 
String sObjectType = ((Id)myid).getSobjectType().getDescribe().getName();

//finding recordtype Name/Id ?
2
  • 1
    you need an sobject, not an sobjectType to find the recordTypeId - and it will be in field RecordTypeId
    – cropredy
    Commented Jun 30, 2017 at 17:40
  • do you want to pass recordtype name or Id? Currently from your question you got object Name Commented Jun 30, 2017 at 17:42

1 Answer 1

25

Use following methods to get recordtype Name or Id based on the parameters passed to these methods.

public static Id getRecordTypeIdbyName(String objectName, String strRecordTypeName)
    {
        return  Schema.getGlobalDescribe().get(objectName).getDescribe().getRecordTypeInfosByName().get(strRecordTypeName).getRecordTypeId();
    }

    public static String getRecordTypeNameById(String objectName, Id strRecordTypeId)
    {
        return Schema.getGlobalDescribe().get(objectName).getDescribe().getRecordTypeInfosById().get(strRecordTypeId).getName();
    }   

Update

You need to query RecordTypeId through SOQL based on recordId.

SELECT Id, RecordtypeId, RecordType.name FROM Account where Id = '<your id>';
4
  • do you need to pass the recordtypename/id ? can we find out based on the record id which record type that particular record is using?
    – Nick
    Commented Jun 30, 2017 at 17:46
  • then you need to query for RecordTypeId based on recordId, no other way it can be found without query Commented Jun 30, 2017 at 17:50
  • There is a typo in getRecordTypeIdbyName, at the very end there is an extra bracket
    – Mattisdada
    Commented May 17, 2018 at 4:08
  • 1
    @Mattisdada thanks for your comment, removed extra bracket. Commented May 17, 2018 at 4:15

You must log in to answer this question.

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