function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lam Ha TuanLam Ha Tuan 

PostInstall script doesn't work

Hello everyone! I am new to Apex coding.
I am trying to create an Apex class to create some default records for my custom Object. Here is my PostInstall class:
global without sharing class PostInstall implements InstallHandler {
global void onInstall(InstallContext context) {
Account a = new Account(Name = 'Hello');
insert a;
}
}
I create a unit test for it: 

public class UnitTestClass {
    @isTest
    static void testInstallScript() {
      PostInstall postinstall = new PostInstall();
        Test.testInstall(postinstall, null);
        Test.testInstall(postinstall, new Version(0,1,0), true);
        Account[] a = [select id from Account where Name = 'Hello'];
        System.assertEquals(a.size(), 1, 'Hehe');
      }
}

And the UnitTestClass is correct. But when I try to create a package and install it to another org, the PostInstall class isn't even called to create records for Account object. Can you help me with this problem? Thank you so much.

SwethaSwetha (Salesforce Developers) 
HI,

Is the PostInstall class added to the package.xml file? Thanks
Lam Ha TuanLam Ha Tuan
Hi Swetha, How can I add it to the package.xml? When I install it to another org, I checked and found this class already exists in the org.