Salesforce 上の「標準オブジェクト」および「カスタムオブジェクト」の項目一覧を取得したい。
- ファイル出力先は salseforce のファイル
- .csv ファイル形式で出力する(タブ区切り / UTF-8)
- オブジェクト単位にファイル出力する
String outputFileName = 'OBJECT_FILE_' + System.today();
String NEW_LINE = '\n';
String SEP = '\t';
List<ContentVersion> outputFileList = new List<ContentVersion>();
Map <String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
for(String objName : gd.keySet()) {
Schema.SObjectType sobjType = gd.get(objName);
Schema.DescribeSObjectResult sobjResult = sobjType.getDescribe();
Map<String, Schema.SObjectField> fields = sobjResult.fields.getMap();
ContentVersion cv = new ContentVersion();
cv.ContentLocation = 'S';
cv.PathOnClient = outputFileName + '_' + sobjType + '.csv';
cv.Title = outputFileName + '_' + sobjType;
String line = 'オブジェクト名' + SEP + 'オブジェクトAPI参照名' + NEW_LINE;
line += sobjResult.getLabel() + SEP + sobjType + NEW_LINE;
line += '項目名' + SEP + 'API参照名' + SEP + 'データ型' + NEW_LINE;
for(String fieldName : fields.keySet()) {
Schema.SObjectField field = fields.get(fieldName);
Schema.DescribeFieldResult fResult = field.getDescribe();
// ※ //
line += fResult.getLabel() + SEP + field + SEP + fResult.getType() + NEW_LINE;
}
cv.VersionData = Blob.valueOf(line);
outputFileList.add(cv);
}
insert outputFileList;
※ 出力値に改行等含まれる場合は別途処理が必要ex. 下記、数式の場合
String cf = fResult.getCalculatedFormula();
cf = (String.isBlank(cf) ? '' : cf.replaceAll('"', '""'));
line += … + SEP + '"' + cf + '"' … ;
ref: