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
Rija SanaRija 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!');
    
}
        
    }
Best Answer chosen by Rija Sana
Rija SanaRija Sana
So I was able to solve this by turning task 't' sobject variable to a list. 

All Answers

Raj VakatiRaj Vakati
You code looks good for me ... 


Can you check how may records are returning by each SOQL query .. the issue looks like its the number of records returned 
Raj VakatiRaj Vakati
try this code and see what is printing in debug logs
 
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];
      System.debug('tttt'+t);
        IF(t.ActivityDate.daysBetween(todaysDate) >30  && t.Status =='Completed')
        {
			System.debug('Going into if condition');
            acc.Assigned_SDR__c = null;
            update acc;
            recordsProcessed = recordsProcessed + 1;
			System.debug('recordsProcessed'+recordsProcessed);
        }
    }
    
}
        
global void finish(Database.BatchableContext bc){
    
     System.debug(recordsProcessed + ' records processed. Shazam!');
    
}
        
    }

 
Rija SanaRija Sana
@Raj Vakati Thanks for the help. So I tried your code and its not printing anything. (side note im running this code in sandbox with just over a 100 accounts)
Raj VakatiRaj Vakati
Looks like you dnt have data to meet the conditions .. please check
Rija SanaRija Sana
I took the soql queries from this code, added account /user id's  and tested in query editor and they are returning results but there seems to be a disconnect in between different portions of code which i can't figure out.
Rija SanaRija Sana
So I was able to solve this by turning task 't' sobject variable to a list. 
This was selected as the best answer