0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Salesforce オブジェクトの詳細を取得するツールを作りたい (1)

Last updated at Posted at 2022-05-04

はじめに

学習に当たり、Salesforce の各オブジェクトの項目やリレーションを知りたくなりました。
オブジェクトマネージャなどを利用すれば、項目は確認できますが、
画面遷移を複数回する必要があり、若干めんどくさいなぁ、と思っていたので、
なにか作れないか、と思いました。

作ってみましょう

1)「標準オブジェクト」「カスタムオブジェクト」の情報を取得するにはどうすれば良いでしょうか。

SchemaクラスのgetGlobalDescribeメソッドを利用することで、オブジェクトの情報(sObjectType)を取得することができます。

Map<String, sObjectType> sObjectTypeMap = Schema.getGlobalDescribe();

2)前述の Map のキーを取り出して、sObjectType を取り出します。
さらに、sObjectType の getDescribe メソッドで DescribeSObjectResult クラスを取得します。

for (String key : sObjectTypeMap.keySet()) {
    sObjectType sObjectType = sObjectTypeMap.get(key);
    DescribeSObjectResult sObjectDescribe = sObjectType.getDescribe();

以下のように、メソッドを呼ぶことで、対象のメソッドのラベル名などの情報を取得することができます。

DescribeSObjectResult sObjectDescribe = sObjectType.getDescribe();

this.Prefix          = sObjectDescribe.getKeyPrefix();
this.Label           = sObjectDescribe.getLabel();
this.Name            = sObjectDescribe.getName();
this.IsAccessible    = sObjectDescribe.isAccessible();
this.IsCreateable    = sObjectDescribe.isCreateable();
this.IsCustom        = sObjectDescribe.isCustom();
this.IsCustomSetting = sObjectDescribe.isCustomSetting();
this.IsDeletable     = sObjectDescribe.isDeletable();
this.IsQueryable     = sObjectDescribe.isQueryable();
this.IsSearchable    = sObjectDescribe.isSearchable();
this.IsUndeletable   = sObjectDescribe.isUndeletable();
this.IsUpdateable    = sObjectDescribe.isUpdateable();

さらに、各項目は以下のように取得します。
sObjectDescribe の fields に対して、getMap メソッドで 項目の詳細情報を取得します。

Map<String, SObjectField> fields = sObjectDescribe.fields.getMap();

for (String key : fields.keySet()) {
    DescribeFieldResult field = fields.get(key).getDescribe();

    YGG_ObjectItem item      = new YGG_ObjectItem();
    item.ByteLength          = field.getByteLength();
    item.CalculatedFormula   = field.getCalculatedFormula();
    item.DefaultValue        = field.getDefaultValue();
    item.DefaultValueFormula = field.getDefaultValueFormula();
    item.Digits              = field.getDigits();
    item.InlineHelpText      = field.getInlineHelpText();
         :
         :

必要とする情報が揃いましたので、実際に組み立ててみましょう。

参考としたサイト

参考とさせていただきましたページも合わせて載せておきます。感謝。

ご了承ください

  • まだまだ勉強中の部分もあります。間違っている箇所もあるかもしれません。
  • Salesforce は、この時点のバージョン(2022年4月~5月頃) での内容になります。今後、仕様変更が変わることによって、内容の通りにならない可能性もあります。ご了承ください。
  • また、URLなども変更になっている可能性もあります。
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?