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

Schedule Trigger to Run Daily

I currently have a trigger "UpdateInTouchLead" shown below: 
trigger UpdateInTouchLead on Task (after insert, after update) {
Set<String> whoIds = new Set<String>();
    for (Task t : {
   	List<Lead> leads = [SELECT Id, First_in_touch__c FROM Lead WHERE Id =: whoIds];
   	Map<String, Task> taskMap = new Map<String, Task>();
    for (Task t :{
    	if (!(t.Type.equals('Eloqua Activity'))) {
    		if (t.Status.equals('Completed')) {
    			taskMap.put(t.WhoId, t);
    for (Lead l : leads) {
  		if (taskMap.containsKey(l.Id)) {
  			System.debug('Activity Date: ' + taskMap.get(l.Id).ActivityDate); 
  			System.debug('First In Touch before: ' + l.First_in_touch__c); 
  			l.First_in_touch__c = taskMap.get(l.Id).ActivityDate;
  			System.debug('First In Touch after: ' + l.First_in_touch__c); 
			l.Status = 'In Touch';
			l.POC_Communications__c = 'Muted'; 
	update leads;
This trigger runs whenever a Task in inserted or updated, but I would like to have this trigger also run once a day (at midnight). I have tried creating a scheduled class but I am receiving the folowing error:  "Invalid Type: UpdateInTouchLead". 

The schedulable class is shown below: 
global class scheduledUpdateInTouchLead implements Schedulable {
global static void scheduleMe()
    scheduledUpdateInTouchLead msc = new scheduledUpdateInTouchLead();
    String sch = '0 00 00 * * ?'; // everyday at midnight
    String jobID = System.schedule('Scheduled Job', sch, msc);


  global void execute(SchedulableContext sc)
	UpdateInTouchLead u = new UpdateInTouchLead();
Any advice on how to fix this is appreciated. 
Balaji BondarBalaji Bondar
Hi Cola,

Update the trigger in the Apex class and call this class in the trigger as well as your scheduled class.You cann't call  UpdateInTouchLead trigger in scheduledUpdateInTouchLead .

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
Thanks Balaji for your quick response. But I don't understand what you mean by "update the trigger in the Apex class". Do I need to create a separate Apex class and call the trigger from there? 
Balaji BondarBalaji Bondar
1. You have to write the custom logic in a apex class(which is there currently in Trigger).
2. Call this class in Trigger by passing Task ids from trigger
3. In case of scheduled class , query all the eligible Task and call the same Apex class with custom logic and pass the Task records/Ids.