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
Hughbert StrongHughbert Strong 

How to schedule my Trigger?

Hello All,

I'm new to writting triggers and classes. I have a trigger I'm trying to schedule. Not sure how to complete this. I've read that I have to place the class in my trigger, but not sure how.

Trigger Code:
trigger ExpiredEntitlements on Entitlement (after update) {
    List<Entitlement> EntIds = New List <Entitlement>();
    for (Entitlement e: Trigger.new){
        if (e.EndDate == date.today() -1){
            try{
                e.Id = [Select Id from Entitlement Where EndDate = Yesterday].Id;
            }catch(QueryException ex){
                
            }
        EntIds.add(e);    
        }
    }
List<Entitlement> EntList = [Select id FROM Entitlement WHERE id in :EntIds];
    for(integer i = 0; i < EntList.size(); i++){
        
    }
    update EntList;
}

Apex Class Code:
global class scheduleExpiredEntitlements implements Schedulable{
    global static void scheduleMe(){
        scheduleExpiredEntitlements msc = new scheduleExpiredEntitlements();
        String sch= '0 00 00 * * ?';
        String jobID = System.schedule('Scheduled Job', sch, msc);
    }
    global void execute(SchedulableContext sc){
        ExpiredEntitlements e = new ExpiredEntitlements();
    }
}

Thanks,
Hieu
Best Answer chosen by Hughbert Strong
Maharajan CMaharajan C
Hi Hieu,

there is a two way to schedule

1)  If you want to schedule it via UI, Go to Setup ---> Build --> Developer --> Apex Classes 
There will be one "Schedule Apex" button available. Click on that button and chose you class to schedule it. 

After scheduling a class it will start appearing in "Scheduled Jobs" at below mentioned path

Setup -->  Monito --> Jobs --> Scheduled Jobs

2) Developer console -->Debug -->Open execute anonymous window 

scheduleExpiredEntitlements.execute();

Then Click Execute

After scheduling a class it will start appearing in "Scheduled Jobs" at below mentioned path

Setup -->  Monito --> Jobs --> Scheduled Jobs
Setup -->  Monito --> Jobs --> Apex Jobs

For your understanding: Example of my code

global with sharing class ScheduleBatchApexDemo implements Schedulable {
global void execute(SchedulableContext sc) {
  ID BatchId = Database.executeBatch(new Accountnameupdate(), 200);
}
Public static void SchedulerMethod() {
  string timeinterval = '0 30 * * * ?';
  System.schedule('BatchApexDemo-Every15mins',timeinterval, new ScheduleBatchApexDemo());
  }
}

 Developer console -->Debug -->Open execute anonymous window 
ScheduleBatchApexDemo.SchedulerMethod();
Then Click Execute

Otherwise use this Reference Links:
http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html
http://www.infallibletechie.com/2012/05/apex-scheduler.html
http://sfdccloudcomputing.blogspot.in/2014/01/different-ways-to-run-schedule-class.html


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
​Raj
 

All Answers

Maharajan CMaharajan C
Hi Hieu,

there is a two way to schedule

1)  If you want to schedule it via UI, Go to Setup ---> Build --> Developer --> Apex Classes 
There will be one "Schedule Apex" button available. Click on that button and chose you class to schedule it. 

After scheduling a class it will start appearing in "Scheduled Jobs" at below mentioned path

Setup -->  Monito --> Jobs --> Scheduled Jobs

2) Developer console -->Debug -->Open execute anonymous window 

scheduleExpiredEntitlements.execute();

Then Click Execute

After scheduling a class it will start appearing in "Scheduled Jobs" at below mentioned path

Setup -->  Monito --> Jobs --> Scheduled Jobs
Setup -->  Monito --> Jobs --> Apex Jobs

For your understanding: Example of my code

global with sharing class ScheduleBatchApexDemo implements Schedulable {
global void execute(SchedulableContext sc) {
  ID BatchId = Database.executeBatch(new Accountnameupdate(), 200);
}
Public static void SchedulerMethod() {
  string timeinterval = '0 30 * * * ?';
  System.schedule('BatchApexDemo-Every15mins',timeinterval, new ScheduleBatchApexDemo());
  }
}

 Developer console -->Debug -->Open execute anonymous window 
ScheduleBatchApexDemo.SchedulerMethod();
Then Click Execute

Otherwise use this Reference Links:
http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html
http://www.infallibletechie.com/2012/05/apex-scheduler.html
http://sfdccloudcomputing.blogspot.in/2014/01/different-ways-to-run-schedule-class.html


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
​Raj
 
This was selected as the best answer
Hughbert StrongHughbert Strong
Thanks Raj! I forgot to mention that I'm receiving the following error on my class. Invalid type: ExpiredEntitlements
Amit Chaudhary 8Amit Chaudhary 8
Hi Hughbert Strong,

You can not schedule the trigger. You need to create on Batch job and need to move trigger logic in Same batch job then you need to schedule same batch job.
Please check below post for more help
1) http://amitsalesforce.blogspot.com/2016/02/batch-apex-in-salesforce-test-class-for.html

Let us know if this will help you