Q:

What is an easy (and fast) way to find a object named based on a parentid?

I have an app which is fired from a trigger on FeedItem and FeedComment. I can easily grab the posting user, date, parentid and type. However I am finding it difficult to rapidly determine the object name associated with that parent ID.
Ideally this would be written as a method within a class which would be called and return 2 things, the object Name and one of the key identifying fields from the object, like: 

Object: Account, Record: ABC Company, Inc.
Object: Case, Record: Case # 1234
Object: Group, Record: Group Name
Object: User, Record: Mike Smith

Thoughts?

A:

When treated as a String, the first three characters of ParentId represent the object prefix, which can be used to search for the object in the getGlobalDescribe map.
for example.
// This will be set to the object description metadata if found
Schema.DescribeSObjectResult object_desc = null;

// Get the Id we're searching for
String parent_id = feed_item.ParentId;

// Search every object in the getGlobalDescribe() map to check key prefixes
for( Schema.SObjectType t : Schema.getGlobalDescribe().values() ){
    Schema.DescribeSObjectResult desc = t.getDescribe();

    // If the Id starts with this objects prefix, then we know the type
    if( parent_id.startsWith( desc.getKeyPrefix() )){
        object_desc = desc;
        return;
    }
}

if( object_desc != null ){
    System.debug('Object type is: ' + object_desc.getName() );
    SObject record = Database.query( 'SELECT Name FROM ' + object_desc.getLocalName() + ' WHERE Id = :parent_id" );

    System.debug('Object name is: ' + record.get('Name'));
}

If you wanted to return a different field for each object, you'd have to check the object type before building the query, include the relevant field, and then return that.
i.e. 
if( object_desc.getLocalName() == 'Contact' ){
    String name = [SELECT LastName FROM Contact WHERE Id = :parent_id].LastName;
}

A:

You can obtain the object type from an Id directly using the Id.getSObjectType method (which I don't think was around the first time this question was answered btw). Without having to describe all objects and filter by the key prefix (which is also still technically a correct answer though is less direct now).

Returns the token for the sObject corresponding to this ID. This method is primarily used with describe information.

This is an example from the docs here.
    // Get the sObject token from the first ID
    // (the List contains IDs of sObjects of the same type).
    Schema.SObjectType token = objIds[0].getSObjectType();

    // Using the token, do a describe 
    // and construct a query dynamically. 
    Schema.DescribeSObjectResult dr = token.getDescribe();
    String queryString = 'SELECT ownerId FROM ' + dr.getName() + 
        ' WHERE ';
    for(ID objId : objIds) {
        queryString += 'Id=\'' + objId + '\' OR ';
    }    
    // Remove the last ' OR'
    queryString = queryString.subString(0, queryString.length() - 4);

A:

I think James answered well to the answer, however I've blogged which answer the same. Please take a look over it and let me know if you face any problem.
http://forceguru.blogspot.in/2011/12/fetching-sobject-type-from-record-id.html

