0

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){
}

}

0

Change your code as follows. My comments are inline. Apologies for semantic errors as I am using my handphone.

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)); 

//not required so commented
//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 =objMapB.get (ts.I2_CAT_NUM__c).Product_ID__c 
//proId; 

//change the next 3 statements just like the statement above

 ts.Ecl_Priceline__c = line;
ts.Ecl_CatNumID__c = catNb;
 ts.Ecl_Sort_Sequence__c = sortSeq;
 system.debug('******ts: '+ ts);
 tsToUpdate.add(ts); 
0

@tomc0910, In the first for loop of execute method, you are assigning the values to the global variables. So, the values they contain will be that of the last record. You can get away with those assignments in the first for loop proID = p.Product_ID__c; and etc.,. since the Map contains the product_catalog__c records.

Try this in the 2nd for loop:

product_catalog__c p = null;

   for(trade_service__c ts : scope){ 
        if (objMapB.containsKey(ts.I2_CAT_NUM__c)){
          p = objMapB.get(ts.I2_CAT_NUM__c);
            ts.des_match__c = 'Match';
            ts.Ecl_Product_ID__c = p.proId;
            ts.Ecl_Priceline__c = p.line;
            ts.Ecl_CatNumID__c = p.catNb;
            ts.Ecl_Sort_Sequence__c = p.sortSeq;
            system.debug('******ts: '+ ts);
            tsToUpdate.add(ts); 
        } 

    }
  • thank you both! it always seems to make perfect sense after seeing the solution : ) both solutions worked well. I ended up using @salesforceMann to update the larger of the two objects. There was an additional requirement to update MapBObj, shalini's solution of p=objMapB.get(key); was used in combination with solution. Again thank you both! – tomc0920 Nov 18 '16 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.