You need to sign in to do that
Don't have an account?
Lynn the AFTD SysAdmin
Attempting to schedule a batch Apex on Opportunities without results
I have written the following piece of batch apex code to be run nightly (using a scheduable class call). I have tested all the stage and date conditions seperately, but I cannot seem to get the class to work. I get no error messages on compilation or run time, but nothing ever happens (the calls for follow-up or email alert aren't invoked). I am drawing a blank an have stared at the code for too long, any help would be appreciated!
Cheers!
global class UpdateOppstoDormant implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id, StageName, CloseDate, NextActivityDate__c, LastActivityDate FROM Opportunity WHERE StageName !=\''+String.escapeSingleQuotes('Received')+'\'';
return Database.getQueryLocator(query);
} //end query
global void execute(Database.BatchableContext BC, List<Opportunity> scope){
List<ID> DormantProspectIDs = New List<ID>();
List<ID> DormantRenewalIDs = New List<ID>();
for(sObject s : scope){
Opportunity o = (Opportunity)s;
//check if opportunities are prospects
if(o.Stagename == 'Prospect ID' || o.Stagename == 'Prospect Engaged'){
// check activity dates to establish Prospect dormancy
if((o.LastActivityDate == NULL || o.LastActivityDate.daysBetween(date.today()) > 30) &&
(date.today().daysBetween(o.CloseDate) < 180) &&
(o.NextActivityDate__c == NULL || o.NextActivityDate__c < date.today()) ){
DormantProspectIDs.add(o.ID);
} //end activity date check
} //end dormant prospects
//check if opportunities are renewals
if(o.StageName == 'Renewal Potential' || o.StageName == 'Renewal Likely'){
// check activity dates to establish Renewal dormancy
if((o.LastActivityDate == NULL || o.LastActivityDate.daysBetween(date.today()) > 90) &&
(o.NextActivityDate__c == NULL || date.today().daysBetween(o.NextActivityDate__c) > 90) ){
DormantRenewalIDs.add(o.ID);
} //end activity date check
} //end dormant Renewals
}
//for Dormant Prospects
List<Opportunity> DormantProspects = [SELECT id, OwnerID FROM Opportunity WHERE id in :DormantProspectIDs];
InactiveOpp.NewFollowUpTask(DormantProspects);
InactiveOpp.OwnerEmailAllert(DormantProspects);
//for Dormant Renewals
List<Opportunity> DormantRenewals = [SELECT id, OwnerID FROM Opportunity WHERE id in :DormantRenewalIDs];
InactiveOpp.RenewalFolloUpTask(DormantRenewals);
InactiveOpp.PotentialRenewalDomantAllert(DormantRenewals);
} //end execute
global void finish(Database.BatchableContext BC){
} //end finish
} //end class
Cheers!
global class UpdateOppstoDormant implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id, StageName, CloseDate, NextActivityDate__c, LastActivityDate FROM Opportunity WHERE StageName !=\''+String.escapeSingleQuotes('Received')+'\'';
return Database.getQueryLocator(query);
} //end query
global void execute(Database.BatchableContext BC, List<Opportunity> scope){
List<ID> DormantProspectIDs = New List<ID>();
List<ID> DormantRenewalIDs = New List<ID>();
for(sObject s : scope){
Opportunity o = (Opportunity)s;
//check if opportunities are prospects
if(o.Stagename == 'Prospect ID' || o.Stagename == 'Prospect Engaged'){
// check activity dates to establish Prospect dormancy
if((o.LastActivityDate == NULL || o.LastActivityDate.daysBetween(date.today()) > 30) &&
(date.today().daysBetween(o.CloseDate) < 180) &&
(o.NextActivityDate__c == NULL || o.NextActivityDate__c < date.today()) ){
DormantProspectIDs.add(o.ID);
} //end activity date check
} //end dormant prospects
//check if opportunities are renewals
if(o.StageName == 'Renewal Potential' || o.StageName == 'Renewal Likely'){
// check activity dates to establish Renewal dormancy
if((o.LastActivityDate == NULL || o.LastActivityDate.daysBetween(date.today()) > 90) &&
(o.NextActivityDate__c == NULL || date.today().daysBetween(o.NextActivityDate__c) > 90) ){
DormantRenewalIDs.add(o.ID);
} //end activity date check
} //end dormant Renewals
}
//for Dormant Prospects
List<Opportunity> DormantProspects = [SELECT id, OwnerID FROM Opportunity WHERE id in :DormantProspectIDs];
InactiveOpp.NewFollowUpTask(DormantProspects);
InactiveOpp.OwnerEmailAllert(DormantProspects);
//for Dormant Renewals
List<Opportunity> DormantRenewals = [SELECT id, OwnerID FROM Opportunity WHERE id in :DormantRenewalIDs];
InactiveOpp.RenewalFolloUpTask(DormantRenewals);
InactiveOpp.PotentialRenewalDomantAllert(DormantRenewals);
} //end execute
global void finish(Database.BatchableContext BC){
} //end finish
} //end class
Try this:
I made the code imrpovements you suggested, however, still nothing ever happens as described above.
Just for the sake of testing I went through the steps in the online Apex workbook (https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_scheduled_intro.htm), but also get the same results. The code in it's various parts tests fine, but when tested as a scheudable test run it passes without any of the action.
Here my test code:
Any ideas why I am still failing?