PHP: Salesforce Object Type Determination by Key Prefix

´╗┐Problem

If you export your data from Salesforce you will find yourself with a bunch of cross-relationships and no idea of how to map them.  For example, a dump of Documents will give you a list of object IDs each document is related to.  You might look at that and find yourself lost or sifting through data in dozens of tables.  No need for all of that.  Salesforce adds what they call a keyPrefix to every Object ID.  This is used to instantiate the correct object type for that ID.  If, in your code, you would like to build a factory to realize objects from serialized Salesforce data dumps you need a mapping function that reads the keyPrefix and instantiates the correct object in your code.

Solution

<?php
/**
 * Salesforce Object Type Lookup
 * 
 * Function for determining the object type of the provided salesforce object key.
 * @author David L Norris
 * @version 1.0
 * @package salesforce_hacks
 */
 
/* See also: http://blog.teamlazerbeez.com/2009/05/17/salesforcecom-api-gotchas-2/
 * This is not a comprehensive list as I have only found fragments of this info.  I 
 * have reconstructed most of the object prefix IDs based on real data.
 * A full list of Salesforce objects can be found at:
 * http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_list.htm
 */
 
function getObjectType( $key ){
    /* mapping of primary key prefix to object type */
    $_typemap = array(
        '000' => 'Null',
//      '###' => 'AccountContactRole',
        '001' => 'Account',
//      '###' => 'Approval',
        '02i' => 'Asset',
        '00P' => 'Attachment',
        '019' => 'BusinessProcess',
        '701' => 'Campaign',
        '00V' => 'CampaignMember',
        '500' => 'Case',
        '00a' => 'CaseComment',
        '02D' => 'CaseHistory2',
        '010' => 'CaseSolution',
        '003' => 'Contact',
//      '###' => 'ContentContent',
//      '###' => 'ContentReference',
//      '###' => 'ContractContactRole',
        '800' => 'Contract',
        '00N' => 'Custom_Field_Definition',
        '015' => 'Document',
//      '###' => 'EmailDisclaimer',
//      '###' => 'EmailRoutingAddress',
        '017' => 'EntityHistory',
        '0E8' => 'EntitySubscription',
        '00U' => 'Event',
//      '###' => 'FeedComment',
        '0G1' => 'FeedFieldHistory',
        '0F7' => 'FeedPost',
        '0D6' => 'FeedTrackedChange',
        '737' => 'FieldHistory',
//      '###' => 'FileFieldData',
//      '###' => 'FiscalYearSettings',
        '00l' => 'Folder',
        '00G' => 'Group',
        '00Q' => 'Lead',
        '0D5' => 'NewsFeed',
        '002' => 'Note',
        '006' => 'Opportunity',
        '00J' => 'OpportunityCompetitor',
        '00K' => 'OpportunityContactRole',
        '008' => 'OpportunityHistory',
//      '###' => 'OpportunityLineItem',
//      '###' => 'OrgWideEmailAddress',
        '00D' => 'Organization',
//      '###' => 'Partner',
//      '###' => 'Period',
        '01s' => 'Pricebook2',
//      '###' => 'PricebookEntry',
//      '###' => 'ProcessInstance',
//      '###' => 'ProcessInstanceStep',
//      '###' => 'ProcessInstanceWorkitem',
        '01t' => 'Product2',
        '012' => 'RecordType',
//      '###' => 'RichTextAreaFieldData',
        '501' => 'Solution',
        '00T' => 'Task',
        '005' => 'User',
        '00E' => 'UserRole',
    );
 
    /* key prefix is the first 3 chars */
    $key_prefix = substr( $key, 0, 3 );
    if( array_key_exists( $key_prefix, $_typemap ) ){
        return( $_typemap[ $key_prefix ] );
    }
    else {
        throw new Exception( 'Unknown Object Type: ' . $key );
    }
}
 
---