6

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 ?
  • 1
    you need an sobject, not an sobjectType to find the recordTypeId - and it will be in field RecordTypeId – cropredy Jun 30 '17 at 17:40
  • do you want to pass recordtype name or Id? Currently from your question you got object Name – Santanu Boral Jun 30 '17 at 17:42
19

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>';
| improve this answer | |
  • 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 Kahn Jun 30 '17 at 17:46
  • then you need to query for RecordTypeId based on recordId, no other way it can be found without query – Santanu Boral Jun 30 '17 at 17:50
  • There is a typo in getRecordTypeIdbyName, at the very end there is an extra bracket – Mattisdada May 17 '18 at 4:08
  • 1
    @Mattisdada thanks for your comment, removed extra bracket. – Santanu Boral May 17 '18 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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