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
Amish RanjitAmish Ranjit 

intergration using batch class (API call to external service)

Hello ,
      I am working on salesforce integration with external system.  I  have a scenerio where nightly batch-class has to run to update inventory in both salesfroce and external- system. I do have some  experience with integration but not with integration using batch class. I am wondering if it is possible to write a batch class to make a HTTP request  and run in nightly basis ?
Best Answer chosen by Amish Ranjit
Ankit SehgalAnkit Sehgal
Yes, you can create a batch class and call it inside a schedulable class on a nightly basis.

Here's an example:
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<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
  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 to cal it:
 
batchAccountUpdate a = new batchAccountUpdate();
    database.executebatch(a,10);

 

All Answers

Ankit SehgalAnkit Sehgal
Yes, you can create a batch class and call it inside a schedulable class on a nightly basis.

Here's an example:
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<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
  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 to cal it:
 
batchAccountUpdate a = new batchAccountUpdate();
    database.executebatch(a,10);

 
This was selected as the best answer
Amish RanjitAmish Ranjit
Hello Ankit,
                   Really appreciate  for the answer ! I will go throug this !
Lorenzo Calò 2Lorenzo Calò 2
Thank you for clarification!
Kavya Reddy 33Kavya Reddy 33
@ Ankit Sehgal  : I have a requirement to invoke/run a salesforce batch class from SAP system. I am new to integration, can you please help me with the solution?