You need to sign in to do that
Don't have an account?
Rija Sana
Help with batch Apex code
The idea is to run the following script once a month (haven't implemented the scheduling part yet) to monitor activities and remove if a sdr is listed on an account but they have no activity in last 30 days. In last line its printing 0 records processed. Can someone help me and let me know what I am doing wrong.
global class UpdateLastSdrActivity implements
Database.Batchable<sObject>, Database.Stateful {
global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
'SELECT id,assigned_sdr__c FROM account WHERE assigned_sdr__c != null'
);
}
global void execute(Database.BatchableContext bc, List<Account> scope){
Task t;
Date todaysDate = system.today();
List<user> i1= [SELECT id FROM user Where user.profile.name='Sales Development Representative'];
//System.Debug(i1[0].id);
for (Account acc : scope) {
t = [SELECT Id, ActivityDate,Status, OwnerId, Owner.Name FROM Task where accountid = :acc.Id AND OwnerID IN :i1 order by activitydate desc Limit 1];
IF(t.ActivityDate.daysBetween(todaysDate) >30 && t.Status =='Completed')
{
acc.Assigned_SDR__c = null;
update acc;
recordsProcessed = recordsProcessed + 1;
}
}
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
}
}
global class UpdateLastSdrActivity implements
Database.Batchable<sObject>, Database.Stateful {
global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
'SELECT id,assigned_sdr__c FROM account WHERE assigned_sdr__c != null'
);
}
global void execute(Database.BatchableContext bc, List<Account> scope){
Task t;
Date todaysDate = system.today();
List<user> i1= [SELECT id FROM user Where user.profile.name='Sales Development Representative'];
//System.Debug(i1[0].id);
for (Account acc : scope) {
t = [SELECT Id, ActivityDate,Status, OwnerId, Owner.Name FROM Task where accountid = :acc.Id AND OwnerID IN :i1 order by activitydate desc Limit 1];
IF(t.ActivityDate.daysBetween(todaysDate) >30 && t.Status =='Completed')
{
acc.Assigned_SDR__c = null;
update acc;
recordsProcessed = recordsProcessed + 1;
}
}
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
}
}
All Answers
Can you check how may records are returning by each SOQL query .. the issue looks like its the number of records returned