Obtain the name of the SObject from a given record ID

In Apex, you can obtain the name of the SObject from a given record ID using the getSObjectType() method provided by the Id class.

Example

Id recordId = '001XXXXXXXXXXXX'; // Replace this with the actual record ID

// Get the SObject type from the record ID
Schema.SObjectType sObjectType = recordId.getSObjectType();

// Get the name of the SObject
String sObjectName = sObjectType.getDescribe().getName();

System.debug('SObject Name: ' + sObjectName);


In this example:

recordId.getSObjectType() returns the Schema.SObjectType of the object associated with the record ID.

sObjectType.getDescribe().getName() retrieves the name of the SObject.

This approach allows you to dynamically determine the SObject name from a record ID in your Apex code.

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach