function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Amit Singh 2702Amit Singh 2702 

how to get sobject name from list of different record id ?

VinayVinay (Salesforce Developers) 
Hi Amit,

Try below snippet.
Id recordId = '123'; 
// If record id is Account then do something
Schema.SObjectType AccountObject = Schema.Account.getSObjectType();
Schema.SObjectType record = recordId.getsobjecttype();
If(record == AccountObject){
// Do Something
}

Please mark as Best Answer if above information was helpful.

Thanks,
Amit Singh 2702Amit Singh 2702
 Hi Vinay,

 I want to get it from List<Recordid> recordids. 

Thanks 
Prateek Prasoon 25Prateek Prasoon 25
Answer :-

You can use the getSObjectType() method on the record object to get the SObject type of each record in the list. Here's an example:
List<Id> recordIds = new List<Id>{'001xxxxxxxxxxxx', '003xxxxxxxxxxxx', '006xxxxxxxxxxxx'};
List<SObject> records = new List<SObject>();

// Retrieve records
for (Id recordId : recordIds) {
    SObject record = Database.query('SELECT Id FROM ' + recordId.getSObjectType() + ' WHERE Id = :recordId');
    records.add(record);
}

// Get SObject type for each record
for (SObject record : records) {
    System.debug('SObject type: ' + record.getSObjectType());
}

In this example, we first create a list of record Ids of different SObject types, and then we retrieve each record by querying the respective SObject type using the getSObjectType() method. Finally, we loop through the list of records and get the SObject type for each record using the getSObjectType() method.
Note that this example uses dynamic SOQL to query the records, which can be less efficient than using static SOQL. If you have a known list of SObject types, you can use static SOQL with multiple OR clauses to retrieve the records.

If you find my answer helpful, please mark it as the best answer. Thanks!