LoginSignup
0
1

More than 1 year has passed since last update.

Salesforceの項目一覧を一括出力する

Posted at

実現したいこと

Salesforceのオブジェクト(今回は取引先担当者)に登録されている項目を整理したい
弊社環境では何のまちがいだかで661項目もあるので、
項目の名前、API名などを一覧で自動取得できないかなと思った

解決方法

  1. 開発者コンソールを開く
    image.png

  2. Open Excecute Anonimous Windowを選択
    image.png

  3. 以下を貼り付け

ContentVersion cv = new ContentVersion();
cv.ContentLocation = 'S'; // ファイルをSalesforce上に保存する
cv.PathOnClient = 'MyObject.csv'; // ファイル名
cv.Title = 'MyObject'; // ファイルタイトル
String myContent = '項目名,API参照名,データ型,バイト数,文字数' + '\n'; //出力ファイルの項目を作成

//List<String> reqFields = new List<String>();
Map <String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
Schema.SObjectType sobjType = gd.get('********');
Schema.DescribeSObjectResult r = sobjType.getDescribe();
Map<String, Schema.SObjectField> mapOfField = r.fields.getMap();

for(String fieldName : mapOfField.keySet()) {
    Schema.SObjectField field = mapOfField.get(fieldName);
    Schema.DescribeFieldResult fDescribe = field.getDescribe();
    System.debug('field-->' + field);
    System.debug('field describe-->' + fDescribe);
    myContent += fDescribe.getLabel() + ',' + field + ',' +  fDescribe.getType() + ',' + fDescribe.getByteLength() + ',' + fDescribe.getLength() + '\n';
}

cv.VersionData = Blob.valueOf(myContent); // ファイルデータ
insert cv;

Schema.SObjectType sobjType = gd.get('********');
の部分は取得したいオブジェクトによって変える。今回は
Schema.SObjectType sobjType = gd.get('Contact');
※カスタムオブジェクトの際は__cも忘れずに

  1. 最後にExcuteを押下
    image.png

  2. ファイルを検索して、しっかり出力できていることを確認する
    image.png

参考にしたサイト
https://zenn.dev/okapi/articles/a77633cef12390

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