[SalesForce] call webservice class from batch apex

I want call below class for all accounts from a batch :

global class IBPortal_Manual_MT4Linker{

  WebService  static void Link_IBPortalMT4(Id ids) {
  Account ac;
   ac = [select IBPortalID__c,SF_LeadID__c  from Account where id=:ids];
   for (ADSS_Platform_Account__c ld: [select id, Name, Account__c,IB_ID__c,Instance__c from ADSS_Platform_Account__c where Account__c=:ids])
    {

       System.debug('--> IBPortal Manual MT4Linker');
         IBPortal_LinkMT4Account.Link_MT4AccountPort stub= new  IBPortal_LinkMT4Account.Link_MT4AccountPort();
        String output = stub.Link_Account(String.valueof(ac.IBPortalID__c),ld.Name,ac.id,ld.Instance__c,ld.IB_ID__c);
               System.debug('--> AccountUpdater'+output);
        }
  }
  }

I have implemented something like this:

    global class linkltoMT4batch implements Database.Batchable<sObject>,Database.AllowsCallouts{
    global set<id> accids;    
    global linkltoMT4batch(){
               // Batch Constructor
    }
    // Start Method
    global Database.QueryLocator start(Database.BatchableContext BC){
     return Database.getQueryLocator([select id from Account]);
    }
  // Execute Logic
   global void execute(Database.BatchableContext BC, List<sObject>scope){
          // Logic to be Executed batch wise   
          List<Account> acclist = (List<Account>)scope;
          for(account a :acclist){
              IBPortal_Manual_MT4Linker.Link_IBPortalMT4(a.id);
          }
   }
   global void finish(Database.BatchableContext BC){
        // Logic to be Executed at finish
   }
}

How can I do it?

Best Answer

you need to implement Database.AllowsCallouts interface in your batch which will allow you to make callout.

this will be syntax for your

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

}
Related Topic