how to get the RecordType/Id name based on the ID

  • 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 ?
    

    you need an sobject, not an sobjectType to find the recordTypeId - and it will be in field RecordTypeId

    do you want to pass recordtype name or Id? Currently from your question you got object Name

  • 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>';
    

    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?

    then you need to query for RecordTypeId based on recordId, no other way it can be found without query

    There is a typo in getRecordTypeIdbyName, at the very end there is an extra bracket

    @Mattisdada thanks for your comment, removed extra bracket.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used