+ Start a Discussion
Raj R.Raj R. 

What is the recommended way to query Accounts based on AccountHistory?

Hi,

I have an optimization and batch apex question. We will have to make HTTP callout from a batch class and the query that we will be passing in is below. In terms what is better for optimization and what would be easier, would the following be recommended? Is there another way to do this?
 
//query

String query = 'Select Name, Id, OwnerId From Account Where ID IN: (Select AccountId From AccountHistory)';
 
//batch class
SendAccounts sender = new SendAccounts(query)
Database.executeBatch(sender);


//batch class definition
global class SendAccounts implements Database.Batchable<sObject>, Database.AllowCallouts{

   global final String Query;

   global SendAccounts (String qry){

      Query=qry; 
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
     //perform logic

     //make 1 http callout
    }

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

 
Best Answer chosen by Raj R.
Alain CabonAlain Cabon
Hi,

select Accountid,   account.name,   account.ownerid   from AccountHistory

There is a trick to know if you want that works with the workbench using a new line for each field.
Otherwise, the workbench rejects with an error but it is clearly possible.

User-added image
Best regards
Alain