I have the ID of an object that could be either an account or a contact or a few other object types and want to query the name of the object regardless of the type and pass it to a string.
I am doing:
if(objectID.getSobjectType()==Contact.getSobjectType()){
name = [SELECT name FROM Contact WHERE id=:ObjectID].name;
}else if(objectID.getSobjectType()==Account.getSobjectType()){
name = [SELECT name FROM Account WHERE id=:ObjectID].name;
}...
Is there a better way to do this without using if that also works with querying custom fields?