Id sampleid = '00561000000Mjya'; System.debug('object is '+ sampleid.getsobjecttype());
別の方法もあります。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;
}
}
開発者コンソールで次の Apex コードスニペットを実行して、レコード ID プレフィックスからオブジェクト名を特定します。
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
System.debug(objectName);
このクラスや次のテストクラスを使用することで、カスタム Visualforce ページを作成したり、Lightning コンポーネントで使用したりできます。
@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.