+ Start a Discussion
SwayampravaSwayamprava 

Webservice call from a batch apex or scheduler class

Requirement : I need to query all the opportunity and make a csv file and send it to one peoplesoft server using a webservice call.

I have written the batch apex. 

global class OpportunityBatch implements Database.Batchable<sObject>{

    //global declaration of variable which contains all Opportunity
    String query;
   
    // global method for query
    global Database.querylocator start(Database.BatchableContext BC){   
   
        query='select id,AccountId,Amount,CloseDate from Opportunity';
        return Database.getQueryLocator(query);     
    }
    //which called by scheduler class to start the execution.
    global void execute(Database.BatchableContext BC, List<Opportunity> scope){
      List<Opportunity> oppListToUpdate = new List<Opportunity>();
     
      string header = 'Record Id, AccountId , Amount, Close Date \n';   
      String finalstr = header ;
      for(Opportunity opp : scope){
          string recordString = opp.Id+','+opp.AccountId+','+opp.Amount+','+opp.closeDate+'\n';
          finalstr = finalstr +recordString;              
      }
      Blob csvBlob = Blob.valueOf(finalstr);
          
    }
    global void finish(Database.BatchableContext info){    
    }
}

I have to write a scheduler class . But I am not getting how to use the csvBlob in the webservise call.
The web service call will be in batch class or in scheduler class. 

thanks in advance.
MagulanDuraipandianMagulanDuraipandian
This may help you

http://www.infallibletechie.com/2013/07/trigger-to-send-pdf-along-with-email-in.html

If this solves your problem, kindly mark it as the best answer.

Regards,
Magulan
http://www.infallibletechie.com
Akanksha JadhavAkanksha Jadhav

Hi Swayamprava,
Instead of calling class using batchable apex, call the class using schedular apex.

global class OpportunityBatch implements Database.Batchable<sObject>{

    //global declaration of variable which contains all Opportunity
    String query;
   
    
    //which called by scheduler class to start the execution.
    global void execute(SchedulableContext SC){
      List<Opportunity> scope = Database.query('select id,AccountId,Amount,CloseDate from Opportunity');
      
      List<Opportunity> oppListToUpdate = new List<Opportunity>();
     
      string header = 'Record Id, AccountId , Amount, Close Date \n';   
      String finalstr = header ;
      for(Opportunity opp : scope){
          string recordString = opp.Id+','+opp.AccountId+','+opp.Amount+','+opp.closeDate+'\n';
          finalstr = finalstr +recordString;              
      }
      Blob csvBlob = Blob.valueOf(finalstr);     
   }  
}


If this solves your problem, kindly mark it as the best answer.