We can use Schema with Describe() method and get the object api name. Find the below example.
//pass the 18/15 characters recordId as parameter
public static string getObjectAPIName( String recordId ){
String objectAPI = '';
String keyPrefix = '';
keyPrefix = recordId.substring(0,3);//get 3 characters from recordId
//get list of org sobjects using global describe
for(Schema.SObjectType obj : Schema.getGlobalDescribe().Values() ){
String prefix = obj.getDescribe().getKeyPrefix();//get sobject prefix key
if(keyPrefix.equals(prefix)){
objectAPI = obj.getDescribe().getName();//get sobject api name
break;
}
}
return objectAPI;
}
Comments
Post a Comment