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
Tanner RussellTanner Russell 

SOQL query with map passed over to batch

I have a class that does some work but at the end it needs to call the batch apex and pass a query the problem I am having is I want the query to be only where id in MyMap. For example 
Database.executeBatch(new JsonMassImportBatch('Select id, Name from Contract where id in : ' (Map in the batch class with the ids keyset should be here),'Contract',new JsonMassImportBatch.CustomObj(holdLease,new List<String>{'Current_Rental_Rate__c','Renewal_Date__c'},holdDate)));
The end is me passing in the map for the batch call to use ie holdlease

How should i format the batch side to make this work?  
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query + LeaseMap.keySet());
    }
 
Best Answer chosen by Tanner Russell
Shashikant SharmaShashikant Sharma
Hi Tanner,

See this class Method runJobForParticularRecords is runs a batch for particualr records in the Map.
 
global without sharing class BatchClassPractice implements Database.Batchable<sObject>, Database.stateful, Database.AllowsCallouts {

       public Set<Id> setRecordIds;
      
       public static void runJobForParticularRecords(Integer batchSize, Map<Id, Contract> mapContract ) {
        
        Set<Id> setIds = mapContract.keySet();
        if( setIds.size() > 0 ) {
            soqlQuery =   ' Select Id '
                          + ' From Contract'
                          + ' Where Id in : setIds ';
        }

          BatchClassPractice cls = new BatchClassPractice();
          cls.setRecordIds = setIds;
          Database.executeBatch(cls, batchSize);
    }

      //Method to get the data to be proceesed
      global Database.QueryLocator start(Database.BatchableContext BC){
          return Database.getQueryLocator(soqlQuery);
      }

      //Method to execute the batch
      global void execute(Database.BatchableContext BC, List<Contract> contracts ){
       
      }

      // Method to be called after the excute
      global void finish(Database.BatchableContext BC) {
      }
}

i hope this will solve your issue.

Thanks
Shashikant

All Answers

LBKLBK
Hi Tanner,

Following code piece can help you in attaching query and the keyset of the map variable.
 
query = 'Select id, Name from Contract where id in ';
String[] sTempArray= New String[]{};

for (Id i : LeaseMap.keySet()){
  sTempArray.add(i);
}
query += '(\'' + string.join(sTempArray,'\',\'') + '\')';
Remember to remove : from your existing query.

Let me know if this helps.
 
Shashikant SharmaShashikant Sharma
Hi Tanner,

See this class Method runJobForParticularRecords is runs a batch for particualr records in the Map.
 
global without sharing class BatchClassPractice implements Database.Batchable<sObject>, Database.stateful, Database.AllowsCallouts {

       public Set<Id> setRecordIds;
      
       public static void runJobForParticularRecords(Integer batchSize, Map<Id, Contract> mapContract ) {
        
        Set<Id> setIds = mapContract.keySet();
        if( setIds.size() > 0 ) {
            soqlQuery =   ' Select Id '
                          + ' From Contract'
                          + ' Where Id in : setIds ';
        }

          BatchClassPractice cls = new BatchClassPractice();
          cls.setRecordIds = setIds;
          Database.executeBatch(cls, batchSize);
    }

      //Method to get the data to be proceesed
      global Database.QueryLocator start(Database.BatchableContext BC){
          return Database.getQueryLocator(soqlQuery);
      }

      //Method to execute the batch
      global void execute(Database.BatchableContext BC, List<Contract> contracts ){
       
      }

      // Method to be called after the excute
      global void finish(Database.BatchableContext BC) {
      }
}

i hope this will solve your issue.

Thanks
Shashikant
This was selected as the best answer