Skip to content

Instantly share code, notes, and snippets.

@zaki-yama
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save zaki-yama/8df241545cd95641a538 to your computer and use it in GitHub Desktop.

Select an option

Save zaki-yama/8df241545cd95641a538 to your computer and use it in GitHub Desktop.
[Salesforce]ApexでCRUD/FLSのチェック
// すべてのSObjectを取得する場合
Map<String, Schema.SObjectType> sMap = Schema.getGlobalDescribe();
// SObjectの種類を指す
Schema.SObjectType eventType = Event.SObjectType;
// または、インスタンスから取得
SObject sObj = new Account();
Schema.SObjectType accountType = sObj.getSObjectType();
// Schema.DescribeSObjectResultというクラスを経由する
Schema.DescribeSObjectResult result = eventType.getDescribe();
// ----SObjectへのアクセス権限(CRUD)----
System.debug(result.isCreateable());
System.debug(result.isAccessible());
System.debug(result.isUpdateable());
System.debug(result.isDeletable());
// SObjectのフィールドを取得
// 以下のようにするとMap型で取得できる
Map<String, Schema.SObjectField> fieldMap = result.fields.getMap();
// ----フィールドへのアクセス権限(FLS)----
String fieldName = 'isRecurrence'; // 例
Schema.DescribeFieldResult fieldResult = fieldMap.get(fieldName).getDescribe();
System.debug(fieldResult.isCreateable());
System.debug(fieldResult.isAccessible());
System.debug(fieldResult.isUpdateable());
// Event.isRecurrenceについては、システム管理者でもisUpdateable() = false
// ある SObject の全 Field のアクセス権限を確認するスクリプト
public static testmethod void test() {
// 例:EventRelationの場合
Schema.SObjectType type = EventRelation.SObjectType;
Schema.DescribeSObjectResult sObjResult = type.getDescribe();
Map<String, Schema.SObjectField> fieldMap = sObjResult.fields.getMap();
for (String key : fieldMap.keySet()) {
Schema.DescribeFieldResult fieldResult = fieldMap.get(key).getDescribe();
System.debug('----------' + key + '---------');
System.debug(fieldResult.isCreateable());
System.debug(fieldResult.isAccessible());
System.debug(fieldResult.isUpdateable());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment