I am hoping someone can provide some direction. I have reviewed several postings regarding Modifying a Collection of Elements between Two Lists, however, I'm still coming up short on the solution. I have two lists; List A is well over 1 million records and List B is nearly 40,000 records.
I am creating a Map for a field in List B that I compare to List A. If the value is the same, I then update List A with List B information.
The problem I'm having is when I update List A all the values are being updated with the last record from List B.
I've reviewed the debug logs and the mapping seems to be working correctly returning true if there is a match, however, when the code runs through the loop its updating the last record from List B.
I have tested with an inner loop, which returns the correct values, however, my understanding is that is not the correct method to iterate and modify.
Any help/direction is greatly appreciated.
global class matchUpdate implements Database.Batchable {
Public string proId {get; set;}
Public string line {get; set;}
Public string catNb {get; set;}
Public decimal sortSeq {get;set;}
global Database.QueryLocator start(Database.BatchableContext BC) {
String field = 'I2_CAT_NUM__c';
String searchVar = 'EYF';
// The query returns 1.8million records
//String query = ('SELECT Id, ALT_MFR_CATALOG_NUMBER__c, I2_CAT_NUM__c, ITEM_NUM__c, MFR_CAT_NUM__c, PS_PIK__c, UPC__c, UPC_Short_Code__c, Ecl_Product_ID__c, DES_Match__c FROM trade_service__c where id != Null AND (DES_Match__c != \'Match\' OR DES_Match__c != \'No Match\')');
String query = ('SELECT Id, MFR_FULLNAME__c, MFR_SHORTNAME__c, ALT_MFR_CATALOG_NUMBER__c, I2_CAT_NUM__c, ITEM_NUM__c, MFR_CAT_NUM__c, PS_PIK__c, UPC__c, UPC_Short_Code__c, Ecl_Product_ID__c, DES_Match__c FROM trade_service__c where ID != Null AND '+field+' LIKE \'%' + searchVar + '%\' ');
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<trade_service__c> scope) {
String pLine = 'APPLETON';
Map<String, product_catalog__c> objMapB = new Map<String, product_catalog__c>();
List<product_catalog__c> proList = new List<product_catalog__c>();
proList = [Select ID, name, product_id__c, price_line__c, product_catalog_number__c, sort_sequence__c, upc_eclipse__c, match_status__c
FROM product_catalog__c
Where price_line__c Like :pLine AND product_catalog_number__c != Null ];
for (product_catalog__c p: proList) {
objMapB.put(p.product_catalog_number__c, p);
System.debug('objMapB.put: '+ objMapB.put(p.product_catalog_number__c, p));
proID = p.Product_ID__c;
line = p.Price_Line__c;
catNb = p.Product_Catalog_Number__c;
sortSeq = p.Sort_Sequence__c;
System.debug('******p: '+p);
}
List<trade_service__c> tsToUpdate = new List<trade_service__c>();
for(trade_service__c ts : scope){
if (objMapB.containsKey(ts.I2_CAT_NUM__c)){
ts.des_match__c = 'Match';
ts.Ecl_Product_ID__c = proId;
ts.Ecl_Priceline__c = line;
ts.Ecl_CatNumID__c = catNb;
ts.Ecl_Sort_Sequence__c = sortSeq;
system.debug('******ts: '+ ts);
tsToUpdate.add(ts);
}
}
update tsToUpdate;
}
global void finish(Database.BatchableContext BC){
}
}