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
G2WIntegrationG2WIntegration 

How to get installHandler script to fire on push upgrades

Do upgrade scripts run on push upgrades?  This ran fine in test code, but didn't run with manual tests of push upgrades from something like 2.20 to 3.5.  The test has the optional boolean to test push upgrades as well, but it fails manually. 

 

The goal is to clean up some scheduled jobs (CronTrigger) that are no longer needed with the latest version so that uninstall can be easy if anyone decides to get rid of the app later.  

 

global class g2wInstall implements InstallHandler {
    global void onInstall(InstallContext context) {
    	//If this isn't a new install, do the work
    	if(context.previousVersion()!=null){
	    	String getRegistrantsSchedulerStartTime='47 25 20 * * ?';
    		String getRegistrantDetailsSchedulerStartTime='47 25 21 * * ?';
    		String getSessionsSchedulerStartTime='47 25 22 * * ?';
 		    String getAttendeesSchedulerStartTime='47 25 23 * * ?';
    		List<CronTrigger> cts=[SELECT Id, NextFireTime, State FROM CronTrigger WHERE (CronExpression =:getRegistrantsSchedulerStartTime OR CronExpression =:getSessionsSchedulerStartTime OR CronExpression =: getRegistrantDetailsSchedulerStartTime OR CronExpression =: getAttendeesSchedulerStartTime) AND State != 'DELETED'];

			//Abort all the old jobs that aren't needed anymore
    		if(cts.size()>0)
    			for(CronTrigger ct:cts)
                	system.abortJob(ct.Id );
    	}
    }//onInstall
}

 This test passes, but manual tests don't show the scheduled jobs deleted:

    //runTest 0
	static void testInstallScript() {
        if(runTest[0]==TRUE){
	  		g2wInstall postinstall = new g2wInstall();
	  		
	  		String getRegistrantsSchedulerStartTime='47 25 20 * * ?';
	    	String getRegistrantDetailsSchedulerStartTime='47 25 21 * * ?';
	    	String getSessionsSchedulerStartTime='47 25 22 * * ?';
	 		String getAttendeesSchedulerStartTime='47 25 23 * * ?';
	
	        system.schedule('Get Registrants', getRegistrantsSchedulerStartTime, new g2wGetRegistrantsSchedulerExecute()); 
	        system.schedule('Get Registrant Details', getRegistrantDetailsSchedulerStartTime, new g2wGetRegistrantDetailsSchedulerExecute()); 
	        system.schedule('Get Sessions', getSessionsSchedulerStartTime, new g2wGetSessionsSchedulerExecute()); 
	        system.schedule('Get Webinar Attendees', getAttendeesSchedulerStartTime, new g2wGetWebinarAttendeesSchedulerExecute());
	
	    	Test.testInstall(postinstall, null);
	
	   		List<CronTrigger> cts=[SELECT Id, NextFireTime, State FROM CronTrigger WHERE (CronExpression =:getRegistrantsSchedulerStartTime OR CronExpression =:getSessionsSchedulerStartTime OR CronExpression =: getRegistrantDetailsSchedulerStartTime OR CronExpression =: getAttendeesSchedulerStartTime) AND State != 'DELETED'];
			system.assertNOTEquals(0, cts.size());
	
	    	Test.testInstall(postinstall, new Version(3,4), true);
			cts=[SELECT Id, NextFireTime, State FROM CronTrigger WHERE (CronExpression =:getRegistrantsSchedulerStartTime OR CronExpression =:getSessionsSchedulerStartTime OR CronExpression =: getRegistrantDetailsSchedulerStartTime OR CronExpression =: getAttendeesSchedulerStartTime) AND State != 'DELETED'];
			system.assertEquals(0, cts.size());
        }
  	}//testInstallScript