前書き
オワコン化しているリストカスタム設定については扱いません。
本記事において「カスタム設定」は階層型カスタム設定を指します。
カスタム設定を定義する
↓これを作る
表示ラベル | API参照名 | データ型 | |
オブジェクト名 | テストカスタム設定 | TestCustomSetting__c | – |
項目名 | テストカスタム項目1 | TestCustomField1__c | テキスト |
項目名 | テストカスタム項目2 | TestCustomField2__c | 数値 |
組織のデフォルト | 標準ユーザ | |
テストカスタム項目1 | OWDだよ | 標準ユーザだよ |
テストカスタム項目2 | 12345 | 67890 |
①設定>カスタムコード>カスタム設定から新規の「カスタム設定」を作成

②新規の「カスタム項目」を作成

③「Manage」ボタンから「デフォルトの組織レベルの値」を設定

④ 「Manage」ボタンから”標準ユーザ”プロファイル向けの値を設定

Apexでカスタム設定の値を取得する
「デフォルトの組織レベルの値」を取得する場合
TestCustomSetting__c mc = TestCustomSetting__c.getOrgDefaults();
String teststr = mc.TestCustomField1__c;
Decimal testint = mc.TestCustomField2__c;
system.debug(teststr); //OWDだよ
system.debug(testint); //12345
「特定のプロファイル向けの値」を取得する場合
TestCustomSetting__c mc = TestCustomSetting__c .getInstance(Profile_ID);
String teststr = mc.TestCustomField1__c;
Decimal testint = mc.TestCustomField2__c;
system.debug(teststr); //標準ユーザだよ
system.debug(testint); //67890
「特定のユーザ向けの値」を取得する場合
TestCustomSetting__c mc = TestCustomSetting__c .getInstance(User_ID);
String teststr = mc.TestCustomField1__c;
Decimal testint = mc.TestCustomField2__c;
system.debug(teststr);
system.debug(testint);
カスタムメタデータを定義する
↓これを作る
表示ラベル | API参照名 | データ型 | |
オブジェクト名 | テストカスタムメタデータ | TestCustomMetaDeta__mdt | – |
項目名 | テストカスタム項目1 | TestCustomField1__c | テキスト |
項目名 | テストカスタム項目2 | TestCustomField2__c | 数値 |
値 | |
表示ラベル | テストレコ-ド |
テストカスタムメタデータ名 | TestName |
テストカスタム項目1 | テストテキストだよ |
テストカスタム項目2 | 12345 |
①設定>カステムコード>カスタムメタデータ型から新規の「カスタムメタデータ型」を作成する

②新規の「カスタム項目」を作成

③「Manage」ボタンから新規レコードを作成

Apexでカスタムメタデータの値を取得する
通常のレコ-ドと同様にSOQLを利用して値を取得することが可能です。
TestCustomMetaDeta__mdt cmd = [SELECT Id,DeveloperName,TestCustomField1__c,TestCustomField2__c FROM TestCustomMetaDeta__mdt WHERE DeveloperName = 'TestName' LIMIT 1];
String teststr = cmd.TestCustomField1__c;
Decimal testint = cmd.TestCustomField2__c;
system.debug(teststr);
system.debug(testint);