Cheatsheet

Map example

Estimated reading: 1 minute 29 views
				
					public class ObjectPrefixMapper {
    // Define a map of record ID prefixes to Salesforce object names
    public static Map<String, String> objectPrefixMap = new Map<String, String>{
        '001' => 'Account',
        '002' => 'Note',
        '003' => 'Contact',
        '005' => 'User',
        '006' => 'Opportunity',
        '00Q' => 'Lead',
        '00G' => 'Group',
        '500' => 'Case',
        '00T' => 'Task',
        '701' => 'Campaign',
        '00U' => 'Event'
    };
    
    // Method to get the object type by record ID
    public static String getObjectNameFromId(String recordId) {
        if (recordId == null || recordId.length() < 3) {
            return 'Unknown';
        }
        
        // Extract the 3-character prefix from the record ID
        String prefix = recordId.substring(0, 3);
        
        // Return the object name based on the prefix
        if (objectPrefixMap.containsKey(prefix)) {
            return objectPrefixMap.get(prefix);
        } else {
            return 'Unknown';
        }
    }
}

				
			
Share this Doc

Map example

Or copy link

CONTENTS