Id sampleid = '00561000000Mjya'; System.debug('object is '+ sampleid.getsobjecttype());
Es gibt auch noch eine alternative Möglichkeit. Erstellen Sie die folgende Klasse: SchemaGlobalDescribe.public class SchemaGlobalDescribe{
public static String findObjectNameFromRecordIdPrefix(String recordIdOrPrefix){
String objectName = '';
try{
//Get prefix from record ID
//This assumes that you have passed at least 3 characters
String myIdPrefix = String.valueOf(recordIdOrPrefix).substring(0,3);
//Get schema information
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
//Loop through all the sObject types returned by Schema
for(Schema.SObjectType stype : gd.values()){
//if (!sObj.contains('__')) to exclude managed package objects
Schema.DescribeSObjectResult r = stype.getDescribe();
String prefix = r.getKeyPrefix();
System.debug('Prefix is ' + prefix);
//Check if the prefix matches with requested prefix
if(prefix!=null && prefix.equals(myIdPrefix)){
objectName = r.getName();
System.debug('Object Name! ' + objectName);
break;
}
}
}catch(Exception e){
System.debug(e);
}
return objectName;
}
}
Führen Sie den folgenden Code in der Entwicklerkonsole aus, um den Objektnamen durch das Datensatz-ID-Präfix zu finden:
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
System.debug(objectName);
Sie können diese Klasse oder die unten stehende Klasse verwenden, um eine benutzerdefinierte VisualForce-Seite zu erstellen, oder um diese mit einer Lightning-Komponente zu benutzen.
@isTest
private class SchemaGlobalDescribeTests{
@istest
private static void testMethodPositive(){
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
System.assertEquals(objectName,'Case');
}
@isTest
private static void testMethodNegative(){
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
System.assertNotEquals(objectName,'Account');
}
@isTest
private static void testMethodNull(){
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('101');
System.assertEquals(objectName,'');
}
@isTest
private static void testMethodException(){
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('10');
System.assertEquals(objectName,'');
}
}
000388019

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.