Schema メソッドを使用した sObject の記述
トークンを使用する代わりに、describeSObjects Schema メソッドをコールして、記述する sObject の 1 つ以上の sObject 型の名前を渡すことで、sObject を記述することもできます。
この例では、Account 標準オブジェクトと Merchandise__c カスタムオブジェクトの 2 つの sObject 型の Describe メタデータ情報を取得します。各 sObject の Describe Result を取得したら、sObject 表示ラベル、項目数、カスタムオブジェクトであるかどうか、子リレーションの数などの返された情報をデバッグ出力に書き込みます。
1// sObject types to describe
2String[] types = new String[]{'Account','Merchandise__c'};
3
4// Make the describe call
5Schema.DescribeSobjectResult[] results = Schema.describeSObjects(types);
6
7System.debug('Got describe information for ' + results.size() + ' sObjects.');
8
9// For each returned result, get some info
10for(Schema.DescribeSobjectResult res : results) {
11 System.debug('sObject Label: ' + res.getLabel());
12 System.debug('Number of fields: ' + res.fields.getMap().size());
13 System.debug(res.isCustom() ? 'This is a custom object.' : 'This is a standard object.');
14 // Get child relationships
15 Schema.ChildRelationship[] rels = res.getChildRelationships();
16 if (rels.size() > 0) {
17 System.debug(res.getName() + ' has ' + rels.size() + ' child relationships.');
18 }
19}