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
Shilpa KambleShilpa Kamble 

Unable to execute post installation script in manage package

I have written a post install script, where on installation of package the scheduler class will get schedule. but the code is not working
please check the below code and let me know what is wrong in the code.
global class CX_InstallUninstallHandler implements InstallHandler, UninstallHandler {
    //Installation handler
    global void onInstall(InstallContext context) {
        try {
            List < CronTrigger > listCronTriggerAllScheduledJobs = new List < CronTrigger > ([SELECT CronJobDetail.Name FROM CronTrigger
                WHERE CronJobDetail.Name = 'Schedule job to send reminder email'
                limit 1
            ]);
            for (CronTrigger CronTriggerAllScheduledJobs: listCronTriggerAllScheduledJobs) {
                if ((CronTriggerAllScheduledJobs.CronJobDetail.Name).equalsIgnoreCase('Schedule job to send reminder email')) {
                    // Delete the Scheduled Job
                    System.abortJob(CronTriggerAllScheduledJobs.id);
                    break;
                }
            }

            //Schedule New job
            CX_CertificationUpdateReminderScheduler scheduler = new CX_CertificationUpdateReminderScheduler();
            String schExpression = '0 0 0 * * ?';
            system.schedule('Schedule job to send reminder email', schExpression, scheduler);

            List < CronTrigger > Jobs = new List < CronTrigger > ([SELECT CronJobDetail.Name FROM CronTrigger
                WHERE CronJobDetail.Name = 'Schedule job to send reminder email'
                limit 1
            ]);
            

          // ---------------for deugging---------------
            system.debug('Jobs=' + Jobs[0].CronJobDetail.Name);
            string jobDetails = string.valueOf(Jobs[0].CronJobDetail.Name);
            Exception ex;
            SendFailureEmail('Installation', ex, jobDetails);
          //---------------for debugging----------------------
           
        }
        Catch(Exception e) {
            SendFailureEmail('Installation', e, '');
        }
    }
    //Uninstallation handler
    global void onUninstall(UninstallContext context) {
        try {
            //retrive the scheduled jobs and abort it
            List < CronTrigger > listCronTriggerAllScheduledJobs = [SELECT CronJobDetail.Name FROM CronTrigger
                WHERE CronJobDetail.Name = 'Schedule job to send reminder email'
                limit 1
            ];
            for (CronTrigger CronTriggerAllScheduledJobs: listCronTriggerAllScheduledJobs) {
                if ((CronTriggerAllScheduledJobs.CronJobDetail.Name).equalsIgnoreCase('Schedule job to send reminder email')) {
                    // Delete the Scheduled Job
                    System.abortJob(CronTriggerAllScheduledJobs.id);
                    break;
                }
            }
        }
        Catch(Exception e) {
            SendFailureEmail('unInstallation', e, '');
        }
    }
    //Notify CertXchange Team, in case of installation and uninstallation 
    public void SendFailureEmail(String strContex, Exception e, String str) {

        Organization orgDetail = [select Id, IsSandbox, InstanceName, Organizationtype from Organization LIMIT 1];
        String strError;
        strError = e.getMessage();
        List < String > toAddresses = new List < String > ();
        //False Positive 
        //We using the hardcoded Email Id for sending the email alert when the package installtion got failed.
        toAddresses.add('Shilpa.kamble@gmail.com');
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(toAddresses);
        mail.setReplyTo(UserInfo.getUserEmail());
        mail.setTargetObjectId(UserInfo.getUserId());
        mail.setSaveAsActivity(false);
        system.debug('mail=' + mail);
        string orgId = '';
        if (orgDetail != null)
            orgId += orgDetail.Id;
        mail.setSenderDisplayName('My Package Errors');
        if (strContex == 'unInstallation') {
            //  mail.setSubject('Exception while unInstalling package for org: ' + orgId );
            mail.setPlainTextBody('Exception : ' + strError + ' test msg =' + str);
        } else if (strContex == 'Installation') {
            //  mail.setSubject('Post install script fail for org: ' + orgId);
            mail.setPlainTextBody('Exception : ' + strError + ' test msg =' + str);
        }

        Messaging.sendEmail(new Messaging.Email[] {
            mail
        });
    }
  
}

Thanks in advance!
pradeep kumar yadavpradeep kumar yadav
Before uploading any package make sure you have selected this class in "Post Install Script" option.