I have this batch apex class where in the START method I am pulling all the workOrders in the Org. As of now what we have in the org is 30000 records and will keep increasing in the future. For each work order there is a vendor field which is a lookup to the account record and it also indicates to what vendor that work order is assigned to.
Here is my START method -
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'SELECT Id, Vendor__c, Purchase_Order_Created_Date__c, Date_Repair_Completed__c FROM WorkOrder WHERE Status = \'Billing Finalized\'';
return Database.getQueryLocator(query);
}
In the execute method, I’m grouping work orders by vendor account and updating specific fields on each account based on related work order data. Given that execute processes only 200 records at a time, will the map include all work orders for each vendor to ensure accurate updates? If not, how can I ensure the map contains all the work order records per vendor account before proceeding?
Here is my EXECUTE method -
global void execute(Database.BatchableContext BC, List<WorkOrder> scope)
{
Map<Id, List<WorkOrder>> vendorWorkOrders = new Map<Id, List<WorkOrder>>();
for (WorkOrder wo : scope) {
if (wo.Vendor__c != null) {
if (!vendorWorkOrders.containsKey(wo.Vendor__c)) {
vendorWorkOrders.put(wo.Vendor__c, new List<WorkOrder>());
}
vendorWorkOrders.get(wo.Vendor__c).add(wo);
}
}
for (Id vendorId : vendorWorkOrders.keySet()) {
List<WorkOrder> workOrdersForVendor = vendorWorkOrders.get(vendorId);
// Processing logic here for each vendor's work orders
}
}
Please advise!