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
GRStevenBrookesGRStevenBrookes 

Batch Apex

I have the following Apex trigger on the Activites object:

 

trigger TaskAfter on Task ( after insert, after update) {

Set<Id> leadsToUpdate = new Set<Id>{};
String leadPrefix = Lead.SObjectType.getDescribe().getKeyPrefix();

for(Task t : trigger.new)
if(t.WhoId != null && ((String)t.WhoId).startsWith(leadPrefix))  //if not null and whoId belongs to a Lead
leadsToUpdate.add(t.WhoId);
List<Lead> updateLeads = new List<Lead>{};

for (Lead currLead : [Select Id, Phone_Call_Attempts__c, Successful_Phone_Calls__c,  Future_Dated_Task_Scheduled__c, Overdue_Task_Flag__c, (Select Id, Status, ActivityDate, Outcome__c from Tasks ORDER BY CreatedDate DESC) from Lead where Id IN :leadsToUpdate ])
{
updateLeads.add(currLead);
currLead.Phone_Call_Attempts__c = 0;
currLead.Successful_Phone_Calls__c = 0;
currLead.Future_Dated_Task_Scheduled__c = false;
currLead.Overdue_Task_Flag__c = false;

for(Task t : currLead.Tasks )
{

if(t.Status == 'Completed')
{
currLead.Phone_Call_Attempts__c += 1;

if(t.Outcome__c == 'Call Successful - Spoke with contact')
currLead.Successful_Phone_Calls__c += 1;
}//end if

else if(t.ActivityDate > Date.Today() && t.Status == 'Not Started')
currLead.Future_Dated_Task_Scheduled__c = true;

else if (t.ActivityDate <= Date.Today() && t.Status == 'Not Started')
currLead.Overdue_Task_Flag__c = true;

}//end inner for
}//end outer for

if (updateLeads != null && !updateLeads.isEmpty())
Database.update(updateLeads);

}

 

However this only runs after insert and after update (as stipulated) I need this to run automaticallyy, a number of times per day, say 09:00 12:00 15:00 and 17:00 I understand this can be done through a batch apex, however do not have a clue how to do. 

 

Any assistance would be most apprecaited.

 

Steve

RaumilRaumil

Hello Steve,

The basic Syntax for batch class is as follows

 

global class batchClassName implements Database.batchable ,Schedulable{

   // Apply Logic here

}

 

Here schedulable interface can be used to schedule run for Batch class Also documentation can be available refer it.

 

Hope this helps

Regards

Raumil Setalwad

GRStevenBrookesGRStevenBrookes

Hi Raumil,

 

I didnt actually write the above Apex - I dont have enough apex knowledge - would you be kind enough to show me exactly how I could acheive what I am looking for?

 

Steve

RaumilRaumil

Hello Steve

Take help of  the following link

 

 

http://blog.sforce.com/sforce/2009/05/batch-apex-a-powerful-new-functionality-in-summer-09.html

 

Also help will be available on our discussion board and also the following link is useful

 

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm

GRStevenBrookesGRStevenBrookes

thanks again for your reply.

 

However without any developer type knowledge of apex I am not going to be able to do this.

many thanks any how.