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
RICARDO PALMARICARDO PALMA 

How to Bulkify a Batch Process.

Hi,
I'm getting this error: TASKRAY:Too many SOQL queries: 201 running the following batch process. 
I think I have to bulkify the batch process taking out of the for loop this line:
Map<String,Id> retInfo = TASKRAY.trTaskBoardController.cloneProjectsReturnIds(projectInfo, cloneOptions);   
My question is what is the best way to take the line out an pass the parameters and call the class.

global class TaskRayProjectCreated implements Database.Batchable<sObject>, Database.Stateful{
public String query;
Map<String,Map<String,Object>> projectInfo = new Map<String,Map<String,Object>>();
Map<String,Object> projectDetails = new Map<String,Object>();
Map<String,Object> cloneOptions = new Map<String,Object>();
List<FRC_Batch_Integration__c> recordsToUpdate = new List<FRC_Batch_Integration__c>();

global Database.QueryLocator start(Database.BatchableContext BC) {

/* QUERY ALL FRC BATCH INTEGRATION RECORDS WITH Taskray_Project_Created  = FALSE*/   
 query = 'Select Id, Type__c, TaskRay_Project_Template_ID__c , Advertiser_FRC_Site_ID__c, Advertiser_Account_Name__c , Product_Short_Name__c, Account_SF_ID__c From FRC_Batch_Integration__c Where TaskRay_Project_Created__c = false and Type__c = \'NEW\' and TaskRay_Project_Template_ID__c  != null Order by CreatedDate asc';
 return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<FRC_Batch_Integration__c> scope ) {
  for (FRC_Batch_Integration__c  frcRecord  :scope){
    projectDetails.put('templateProjectId', frcRecord.TaskRay_Project_Template_ID__c);
    projectDetails.put('newName', frcRecord.Advertiser_FRC_Site_ID__c + '-' + frcRecord.Advertiser_Account_Name__c + '-' + frcRecord.Product_Short_Name__c);
    projectDetails.put('depCloneBasedOnProjectStart', true);
    projectDetails.put('newStartDate', Date.today());
    projectDetails.put('newAccount', frcRecord.Account_SF_ID__c);
    projectInfo.put   ('specialid1',projectDetails);
    cloneOptions.put('assignInactiveToCurrent', true);
    
    frcRecord.TaskRay_Project_Created__c = true;
    recordsToUpdate.add(frcRecord);
    system.debug ('???????? ' +  frcRecord.Account_SF_ID__c); 
   Map<String,Id> retInfo = TASKRAY.trTaskBoardController.cloneProjectsReturnIds(projectInfo, cloneOptions);   
  }
}
global void finish(Database.BatchableContext BC) {
update recordsToUpdate;
}
}
 
GauravGargGauravGarg
Hi RJ,

In the above code, all looks good. Please check the debug log and check how much queries are getting fired on update call

Thanks,
Gaurav
Skype: gaurav62990