I'm attempting to create a Batch class that will Stamp Opps with their Next Activity Date.
I haven't been able to figure out the relationship between Opps and Tasks? I’ve tried: Task_r, Tasks_r, Activities__r, Activity __r with no luck
Error: Variable does not exist: Activities__r
Thoughts? Code:
public class BatchNextOpportunityActivity implements Database.Batchable<sObject>, Database.Stateful, Schedulable{
//variable to hold any errors
private Set<String> apexErrors;
//List to hold Opportunities to be updated
List <Opportunity> oppsToUpdate = new List <Opportunity>();
//instantiate the class
public BatchNextOpportunityActivity(){
Set<String> apexErrors = new Set<String>();
this.apexErrors = apexErrors;
List <Opportunity> oppsToUpdate = new List <Opportunity>();
this.oppsToUpdate = oppsToUpdate;
}
public void execute(SchedulableContext SC) {
Database.executeBatch( new BatchNextOpportunityActivity() );
}
public Database.QueryLocator start(Database.BatchableContext BC){
String query = 'Select ID, Next_Activity__c,(ActivityDate FROM Activities__r WHERE ActivityDate > Date,Today() ORDER BY ActivityDate desc) from Opportunity WHERE CreatedDate = LAST 120 DAYS';
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List<Opportunity> scope){
system.debug('scope.size = '+scope.size());
for (Opportunity opp : scope){
//If the ActivityDate of the 1st Task is greater than the Opps Next_Activity__c then let's update it with the Task's ActivityDate
if (opp.Activities__r.size()>0 && opp.Activities__r[0].ActivityDate > opp.Next_Activity__c) {
opp.Next_Activity__c = opp.Activities__r[0].ActivityDate;
oppsToUpdate.add(opp);
}
//If there's no Tasks found and the Next_Activity__c field is populated lets clear it
Else if (opp.Next_Activity__c != null) {
opp.Next_Activity__c = null;
oppsToUpdate.add(opp);
}
}
}