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
Dchris222Dchris222 

Apex Scheduler Problems

Have an apex class that has been tested to work, but I am having problems understanding the documentation for creating a Scheduler class. The documentation here makes it seem like all I have to do is create the following scheduler class, but when I schedule it to run in salesforce nothing happens. Any help would be appreciated.

 

Scheduler Class

 

 

global class OppPoliceScheduler1 implements Schedulable{
	
   global void execute(SchedulableContext sc) {
   	
      OppPoliceController O = new OppPoliceController();
   }
}

 

 

Apex Class

 

 

public with sharing class OppPoliceController {
 
  public void Opportunity() {
 
     List<Opportunity> Opptys2 = [SELECT id, LastActivityDate, Activity_Status__c, StageName, (SELECT id, CreatedDate from Feeds ) FROM Opportunity WHERE StageName != 'Closed Won' AND StageName != 'Closed Lost' ORDER BY LastActivityDate DESC];
     
        for(Opportunity Op : Opptys2){
       
        datetime LastActivityDate = Op.LastActivityDate; //Last Activity Date in datetimeformat
        datetime CreatedDate = Op.Feeds[0].CreatedDate;  //Created Date in datetime format 
        
        
        date d = Date.Today();  //Current Date Formated
       
            if(LastActivityDate == null || d > LastActivityDate.addDays(14) || d > CreatedDate.addDays(14)) {
              
              Op.Activity_Status__c = 'High';
              
               
            }
            
            else if((d >= LastActivityDate.addDays(7) && d < LastActivityDate.addDays(14)) 
                     || (d >= CreatedDate.addDays(7) && d < CreatedDate.addDays(14))) {
         
               Op.Activity_Status__c = 'Medium';
               
               
            }
            
            else if(d < LastActivityDate.addDays(7) || d < CreatedDate.addDays(7)) {
                Op.Activity_Status__c = 'Low';
                
            }
            	
            else {
            
                Op.Activity_Status__c = 'Error';
                   
            }
        }    
      
           update Opptys2;
  }     
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
tmatthiesentmatthiesen

Not sure what the previous post was referring to, but it looks like you forgot to call the opportunity method...

 

 

 

global class OppPoliceScheduler1 implements Schedulable{
	
   global void execute(SchedulableContext sc) {
   	
      OppPoliceController O = new OppPoliceController();
      //call the oppty method
      O.Opportunity();
   }
}

 Please note - when you invoke the System.schedule method, it will return an ID that refers to the Crontrigger table.  You can query this directly confirm execution times.  I think you'll find the scheduler did fire - but no actual logic, other than the instantiation of OppPoliceController, was fired.

 

All Answers

nasknask

i can see that u dont have database.execute metod in ur apex class.Add in ur main class

 

 public void oppPolicecontroller()
    {
      oppPolicecontroller obj = new oppPolicecontroller();
        ID batchprocessid = Database.executeBatch(obj);
    }
   

 

tmatthiesentmatthiesen

Not sure what the previous post was referring to, but it looks like you forgot to call the opportunity method...

 

 

 

global class OppPoliceScheduler1 implements Schedulable{
	
   global void execute(SchedulableContext sc) {
   	
      OppPoliceController O = new OppPoliceController();
      //call the oppty method
      O.Opportunity();
   }
}

 Please note - when you invoke the System.schedule method, it will return an ID that refers to the Crontrigger table.  You can query this directly confirm execution times.  I think you'll find the scheduler did fire - but no actual logic, other than the instantiation of OppPoliceController, was fired.

 

This was selected as the best answer
Dchris222Dchris222

Thanks for your input!