Apex setReadOnlyApplicationModeでアプリケーションモードを参照のみに設定する
環境
Salesfoce
構文
public static Void setReadOnlyApplicationMode(Boolean applicationMode)
Salesforce のアップグレードおよびダウンタイム中に参照のみモードをシミュレートするには、Apex テストにおける組織のアプリケーションモードを参照のみに設定します。アプリケーションモードは、Apex テストの各実行が終了するとデフォルトのモードにリセットされます。
使用例
アプリケーションモードを参照のみに設定し、新しい取引先レコードを挿入しようとしています。
結果は例外となります。
その後、アプリケーションモードはリセットされ、正しく挿入されます。
サンプルコード
@isTest
private class ApplicationReadOnlyModeTestClass {
public static testmethod void test() {
// テストアカウントを作成
Account testAccount = new Account(Name = 'TestAccount');
insert testAccount;
// アプリケーションの読み取り専用モードを設定.
Test.setReadOnlyApplicationMode(true);
// 読み取り専用モードを確認
System.assertEquals(
ApplicationReadWriteMode.READ_ONLY,
System.getApplicationReadWriteMode());
// アカウントオブジェクトの作成
Account testAccount2 = new Account(Name = 'TestAccount2');
try {
// テストアカウントを取得
Account testAccountFromDb =
[SELECT Id, Name FROM Account WHERE Name = 'TestAccount'];
System.assertEquals(testAccount.Id, testAccountFromDb.Id);
//InvalidReadOnlyUserDmlException例外
insert testAccount2;
System.assertEquals(false, true);
} catch (System.InvalidReadOnlyUserDmlException e) {
// Expected
}
// 読み取り専用アプリケーション モード
Test.setReadOnlyApplicationMode(false);
insert testAccount2;
Account testAccount2FromDb =
[SELECT Id, Name FROM Account WHERE Name = 'TestAccount2'];
System.assertEquals(testAccount2.Id, testAccount2FromDb.Id);
}
}