+ Start a Discussion
Dippan PatelDippan Patel 

Get RecordtypeId if RecordType Exists in SObject

Hi, 

I want to get recordtypeId and recordtypeName in Sobject if that exists.. For example, 
List<RecordType> recordTypeList;
try{
    recordTypeList = [select Id,Name from RecordType where sObjectType = 'Account'];
    System.debug('recordTypeList' + recordTypeList);
	if(!recordTypeList.isEmpty()){
        System.debug('inside if recordTypeList');
    	Account existingAccount = [select Id,RecordTypeId,RecordType.Name from Account where Id = '0010r00000xxxx'];
        System.debug('existingAccount' );
    }                        
}
catch(Exception e){
           System.debug(LoggingLevel.ERROR, 'Exception:  ' + e.getMessage());
}

The above code gives compilation error, 

Line: 7, Column: 32
select Id,RecordTypeId,RecordType.Name from ^ ERROR at Row:1:Column:11 No such column 'RecordTypeId' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.


What should be the right approach for the above scenario? 

Thank you! 
ShirishaShirisha (Salesforce Developers) 
Hi Dippan,

Greetings!

You can use:
 
Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId();
 
Here, 'Development' is the record type's name. You shuld use 'someone'. Also, you will have to specify your SObject type, here I have mentioned Account.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
David Zhu 🔥David Zhu 🔥
This means there is no record type on object Account. When no record type on the object, recordtypeid is a invalid field.
You can try this in query editor, it must return 0.

select Id,Name from RecordType where sObjectType = 'Account'