Another Salesforce blog!!!

Salesforce, Apex

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


link question

another link

id myid = 'a1aV0000001czYE'; 
String sObjectType = ((Id)myid).getSobjectType().getDescribe().getName();
 
private string getRecordTypeId(String objName, String whereClause) {
    
    String sqry = ' SELECT Id, RecordtypeId, RecordType.name ';
    sqry += ' FROM '+objName+' WHERE Id =: whereClause ';
    List<SObject> objs = Database.query(sqry); 
    //system.debug('soql: '+ objs);
    string recordTypeId = '';
    for(SObject obj : objs) {
		system.debug('soql: '+ obj);
        recordTypeId = (string)obj.get('RecordTypeId');
    }
    system.debug('rtypeid: ' + recordTypeId);
    return recordTypeId;    
}
 
private string getRecordTypeName(string objName, string whereClause) {
    String sqry = ' Select Id,name,developername FROM RecordType ';
    sqry += ' WHERE sobjecttype =: objName AND id =: whereClause';
    List<SObject> objs = Database.query(sqry);
     string recordTypeName = '';
    for(SObject obj : objs) {
		system.debug('soql: '+ obj);
        recordTypeName = (string)obj.get('developername');
    }
    system.debug('recordTypeName: ' + recordTypeName);
    return recordTypeName;   
} 
 
getRecordTypeId(sObjectType, myId);

getRecordTypeName(sObjectType, getRecordTypeId(sObjectType, myId));

Leave a Reply

Your email address will not be published. Required fields are marked *

*