[SalesForce] Batch Apex with Webservice Callout

My current project requires me to grab a WAV file (voice attestation) from a 3rd party server and store it as an attachment on a case. Salesforce apparently limits callouts to 10 per class so I figured this would be a good time to learn batch. Unfortunately my attempt has failed completely so I am reaching out to you nice people.

Here's some background: We have a weekly report of people who have called into our phone center and a link to their voice attestation. We want to pull the wav file from a 3rd party server and store it against the case that was opened when they called. As this requires quite a few callouts I have decided to go the batch route.

This is the batch I created:

global class batchAccountUpdate implements Database.Batchable<sObject>, Database.AllowsCallouts{

global Database.QueryLocator start(Database.BatchableContext BC)
{
    string obj = 'ACA';
    String query = 'Select Id, CreatedDate, CreatedBy.Name, Attest_ID__c from Case where Ticket_Type__c = :obj';
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Account> scope)
{
   List<Voice_File_Loader__c> searchVFL =  [Select Id, Call_Date_Time__c, End_Window__c, Agent_Name__c, Voice_File_Location__c from Voice_File_Loader__c];

    for (Account checkCase : scope){

  for (Voice_File_Loader__c matchVFL :searchVFL){
    boolean after = (checkCase.CreatedDate >= matchVFL.Call_Date_Time__c);
    boolean before = (checkCase.CreatedDate <= matchVFL.End_Window__c);
    boolean timeCheck = (after && before);
    boolean nameCheck = (checkCase.CreatedBy.Name.equalsIgnoreCase(matchVFL.Agent_Name__c));
    if (timeCheck && nameCheck){
      Attachment att = new Attachment();
        Http binding = new Http();
        HttpRequest req = new HttpRequest(); 
        req.setMethod('GET');                                                                                  
        req.setEndpoint(matchVFL.Voice_File_Location__c); 
        HttpResponse res = binding.send(req);
        Blob b = res.getbodyasblob();
        att.name = 'Voice Attestation.wav'; 
        att.body = b;
        att.parentid = checkCase.Id;
        system.debug('#############'+ att);
        insert att;
      delete matchVFL;
    }
  }
}



}   
global void finish(Database.BatchableContext BC)
{
}

}

and this is how i'm calling it:

    batchAccountUpdate a = new batchAccountUpdate();
    database.executebatch(a,10);

I get a success when I run it, but nothing changes. When I check the logs it says only 3 code statements were executed.

Thanks for the help!
Ross

Best Answer

Looks like there is a mis-match between the records you pass in (Cases) and the records you then try and process in the Batch (Accounts)?

Assuming you are trying to process Cases, I would start by changing the first part of the execute section:

global void execute(Database.BatchableContext BC, List<sObject> scope) {
   List<Voice_File_Loader__c> searchVFL =  [Select Id, Call_Date_Time__c, End_Window__c, Agent_Name__c, Voice_File_Location__c from Voice_File_Loader__c];

List<Case> Cases = (List<case>)scope; //Case the generic sObject to Cases
    for (Case checkCase : Cases){//then loop through cases
Related Topic