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
NavneethNavneeth 

Problem in Rescheduling already scheduled apex class

Hi , 

 

I have a requirement where in i need to reschedule already scheduled apex classes. I have used System.Schedule method to achieve the same. Since System.Schedule only works with the class names(Need to give classname as a third parameter) and since that would become static , i tried using "Reflection concept of java" so that i can pass the instance of an object as a parameter and thus make it work for more than one class. (I have more than once class to schedule in my organisation). 

 

I get an error which states "Attempt to de-reference a null object"

 

The following is my Controller code. 

 

global with sharing class GetAllApexClasses {
    public PageReference shedulerClicked() {
        return null;
    }
public void scheduleClicked () {
        return ;
    }
    public String asyncapexjob { get; set; }
    public string className { get; set; }
    public String schedule  { get; set; }  
    public List<AsyncApexJob> apexclassList{get; set;}
    public List<ApexClass> classList{get;set;}
    public List<CronTrigger> CronList{get;set;}
    
    public GetAllApexClasses(){    
    apexclassList = new List<AsyncApexJob>();
    apexclassList = [SELECT ApexClassID, CompletedDate,Status,CreatedDate,TotalJobItems,CreatedBy.Name from AsyncApexJob ]; 
}

  public void sheduleClicked () 
  {
    {
             System.debug('Class Name : ' +className);
        Type t = Type.forName(className); 
        String sch = '0 10 * * * ?';
Id m=system.schedule('one', sch,(Schedulable)t.newInstance());

}
}
}

 The following is my Visual force page

 

<apex:page controller="GetAllApexClasses">
<apex:form >
 <apex:pageBlock >
  <apex:pageBlockTable value="{!apexclassList}" var="apexclass">
   <apex:column headerValue="Class Name" >
   <apex:outputField value="{!apexclass.ApexClassID}"/>
   </apex:column>
   <apex:column headerValue="Completed Date" >
   <apex:outputField value="{!apexclass.CompletedDate}"/>
   </apex:column>
   <apex:column headerValue="Status" >
   <apex:outputField value="{!apexclass.Status}"/>
   </apex:column>
   <apex:column headerValue="Created By" >
   <apex:OutputField value="{!apexclass.CreatedBy.Name}"/>
   </apex:column>
   <apex:column headerValue="CreatedDate" >
   <apex:outputField value="{!apexclass.CreatedDate }"/>
   </apex:column>
   <apex:column headerValue="TotalJobItems">
   <apex:outputField value="{!apexclass.TotalJobItems}"/>
   </apex:column>
   <apex:column headerValue="Schedule ">
   
   <apex:commandButton id="cb1" action="{!sheduleClicked}" value="schedule" rerender="hiddenBlock"> 
       <apex:param name="className" value="{!apexclass.ApexClassID}" assignTo="{!className}"/>
   </apex:commandButton>
   
   </apex:column>
     </apex:pageBlockTable>

</apex:pageBlock>
 <apex:pageBlock id="hiddenBlock" rendered="false"></apex:pageBlock>
</apex:form>
</apex:page>

 

 I am not able to reschedule my class and get the following error when i click on Schedule Command button

 

System.NullPointerException: Attempt to de-reference a null object

Error is in expression '{!sheduleClicked}' in page getapexclasses

 

Class.GetAllApexClasses.sheduleClicked: line 88, column 1

Pls help me as it is urgent. Any help would be greatly appreciated.

Thanks in advance

Vinita_SFDCVinita_SFDC

Hello Navneeth,

 

In following code:

 

public void scheduleClicked () {
        return ;
    }

You are not returning anything, write it like:

 

public void scheduleClicked () {
        return null;
    }

NavneethNavneeth

No , i tried to change the code but it still gives the same error..

 

kirkevonphillykirkevonphilly

A Cron Job "Helper" Object might help.  Create one with a Custom field called "CronTriggerID."

 

When you schedule your job, also create a helper record

 

new CronHelper__c (Name = 'Your Scheduled Job Name', CronTriggerID__c = the ID returned from System.Schedule());

 

When rescheduling an already scheduled apex class, revise your process so:

1)  Prior to scheduling, query CronHelper__c for jobs with a matching name

2)  Using the CronTriggerID__c field in the results of #1, query the CronTrigger object to see if the job is still scheduled

3)  Here you can decide on issuing a system.abortJob() on the ID to remove the job or stop you attempt in scheduling a new one

4)  If you do use system.abortJob(), schedule your job, retaining the ID returned from System.Schedule();

5)  Remove the CronHelper__c record that helped you identify the CronTrigger in #2

6)  Create a new CronHelper__c record for your new job in #4

 

Hope that helps!