+ Start a Discussion
brozinickrbrozinickr 

Batch Apex Field Update

Hi,

 

I was wondering if someone might be able to take a look at a batch apex class that I wrote:

 

The gist of what's supposed to happen is this.  Whenever the Expiration Date passes for a Contact, this batch apex process needs to go in and change the BGC_Status__c field to Expired.

 

I ran this execute anon window in the Developer Console to test, but it's not changing any of the statuses.  The logic is pretty simple and I thought my code was correct, but is there something that I could be missing?

 

 

 

global class expireBackgroundChecks implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Date d = Date.today();
        String soql = 'SELECT id, BGC_Expiration_Date__c, BGC_Status__c FROM Contact WHERE BGC_Expiration_Date__c <: d';
        return Database.getQueryLocator(soql);
    }
   
    global void execute(Database.BatchableContext BC, List<Contact> scope)
    {
         for(Contact c : scope)
         {
             c.BGC_Status__c = 'Expired';
             update c;
         }
    }   
    global void finish(Database.BatchableContext BC)
    {
    AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            from AsyncApexJob where Id =
            :BC.getJobId()];
 
        // Create and send an email with the results of the batch.
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
        mail.setToAddresses(new String[] {a.CreatedBy.Email});
        mail.setReplyTo('rachelbr@angieslist.com');
        mail.setSenderDisplayName('Senor Cardgage');
        mail.setSubject('Background Check Expiration has been' + a.Status);
        mail.setPlainTextBody('Hi there, Belindas.  The batch apex job processed ' + a.TotalJobItems +
        ' batches with ' + a.NumberofErrors + ' failures.  ');
 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Vinita_SFDCVinita_SFDC

Hi,

Try like this:

 

global void execute(Database.BatchableContext BC, List<Contact> scope)
    {
        List<Contact> contactsToUpdate = new List<Contact>();
         for(Contact c : scope)
         {
             c.BGC_Status__c = 'Expired';
             contactsToUpdate.add(c);
         }
update contactsToUpdate;
    }

            
   

All Answers

Vinita_SFDCVinita_SFDC

Hi,

Try like this:

 

global void execute(Database.BatchableContext BC, List<Contact> scope)
    {
        List<Contact> contactsToUpdate = new List<Contact>();
         for(Contact c : scope)
         {
             c.BGC_Status__c = 'Expired';
             contactsToUpdate.add(c);
         }
update contactsToUpdate;
    }

            
   

This was selected as the best answer
sambasamba

1. Don't put DML to for statement.

 

2. Try this:

global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator([SELECT id, BGC_Expiration_Date__c, BGC_Status__c FROM Contact WHERE BGC_Expiration_Date__c <: Date.today()]); 
}

 Please let me know If you have any questions.

 

Thanks,

Samba

souvik9086souvik9086

Change it like this

 

global class expireBackgroundChecks implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
Date d = Date.today();
String soql = 'SELECT id, BGC_Expiration_Date__c, BGC_Status__c FROM Contact WHERE BGC_Expiration_Date__c < '+ d;
return Database.getQueryLocator(soql);
}

global void execute(Database.BatchableContext BC, List<Contact> scope)
{
for(Contact c : scope)
{
c.BGC_Status__c = 'Expired';
update c;
}
}
global void finish(Database.BatchableContext BC)
{
AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
from AsyncApexJob where Id =
:BC.getJobId()];

// Create and send an email with the results of the batch.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setToAddresses(new String[] {a.CreatedBy.Email});
mail.setReplyTo('rachelbr@angieslist.com');
mail.setSenderDisplayName('Senor Cardgage');
mail.setSubject('Background Check Expiration has been' + a.Status);
mail.setPlainTextBody('Hi there, Belindas. The batch apex job processed ' + a.TotalJobItems +
' batches with ' + a.NumberofErrors + ' failures. ');

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks