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
Sean ClarkSean Clark 

Need to schedule a Apex Trigger to run Daily

Hi All,

I have the below Apex Trigger and need to schedule it to run daily.

I am new to coding and this is one of my first pieces of code.

Could someone please advice me on how to do this?

---------------------------------------------------------------------------------------------

trigger CloseVacancies on TR1__Job__c (after insert, after update) {
    
    Set <String> jobID = New Set <String> (); 
    For (TR1__Job__c job: Trigger.new){
        if (job.Id != Null){
        jobID.add(job.Id);    
        }
    }
    if(jobID.size ()> 0) {
        List <TR1__Job__c> upJobList = new List <TR1__Job__c> (); 
        for (TR1__Job__c vac: [SELECT Id, TR1__Status__c, TR1__Days_Open__c FROM TR1__Job__c WHERE id in: jobID AND TR1__Days_Open__c >= 42 AND TR1__Status__c != 'Closed']){
            vac.TR1__Status__c = 'Closed';
            vac.TR1__Closed_Date__c = system.Date.today();
            upJobList.add(vac);
        }
        If(upJobList.size()> 0)
            update upJobList;
    }
}

---------------------------------------------------------------------------------------------
Deepali KulshresthaDeepali Kulshrestha
Hi Sean,

No, you can schedule the trigger. Trigger always fires on event when some DML(Insert/Update/Delete) Operation is performed.
Put your logic in Apex Class and then that class can be scheduled.

To Know the Concept of Apex Schedular visit to the link:--
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_scheduled


Example Schedular Class is given.
global class TestScheduledApexFromTestMethod implements Schedulable {

// This test runs a scheduled job at midnight Sept. 3rd. 2022

   public static String CRON_EXP = '0 0 0 3 9 ? 2022';
   
   global void execute(SchedulableContext ctx) {
      CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime
                FROM CronTrigger WHERE Id = :ctx.getTriggerId()];

      System.assertEquals(CRON_EXP, ct.CronExpression);
      System.assertEquals(0, ct.TimesTriggered);
      System.assertEquals('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));

      Account a = [SELECT Id, Name FROM Account WHERE Name = 
                  'testScheduledApexFromTestMethod'];
      a.name = 'testScheduledApexFromTestMethodUpdated';
      update a;
   }   
}

The above code updates the account at a particular time.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Ajay K DubediAjay K Dubedi
Hi Sean,

A trigger only runs on DML commands which are insert, update and delete. 
You can not schedule a trigger to run according to time intervals. 

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com