0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

パッケージインストール後のスクリプト実行

Posted at

第二世代管理パッケージインストール後に、特定のApexロジックを実行し、初期設定を行うことができます。
実装するにはInstallHandlerをインプリメントしたクラスを作成し、onInstallメソッドにロジックを記述することです。

global class PostInstallClass implements InstallHandler {
    global void onInstall(InstallContext context) {
        if(context.previousVersion() == null) {
            // 以前のバージョンが null である場合、つまりパッケージが初めてインストールされている場合
            PublicSetting__c publicSetting = new PublicSetting__c();
            publicSetting.TriggerEnabled__c = true;
            insert(publicSetting);
        }
    }
}

テストが必要なので、テストクラスを書きます。

@isTest
private class PostInstallClassTest {
    @isTest
    static void testInstallScript() {
        PostInstallClass postinstall = new PostInstallClass();
        Test.testInstall(postinstall, null);
        Test.testInstall(postinstall, new Version(1,0), true);
        
        System.Test.startTest();
        List<PublicSetting__c> publicSettingList = [Select id, TriggerEnabled__c from PublicSetting__c];
        System.Test.stopTest();

        System.assertEquals(publicSettingList.size(), 1, 'PublicSettingが作成されていません。');
        System.assertEquals(publicSettingList[0].TriggerEnabled__c, true, 'PublicSettingがtrueに設定されていません。');
    }
}
```Java

最後にパッケージバージョン作成時に--post-install-scriptに上記のクラスを指定します
※:第二世代管理パッケージのみ有効みたいロック解除済みパッケージに指定したらエラーで怒ってました


0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?