+ Start a Discussion
Jasjeet GrewalJasjeet Grewal 

before update trigger using trigger.new and trigger.old in mass upsert

Hi,

I have two objects Account, and abc_and_sale_summary__c. I am trying to implement before update trigger on Account which will update values in abc_and_sale_summary__c.

Account has fields club__c(text), created_date__c, and down_payment__c(text later converted to decimal).
abc_and_sale_summary__c has fields ABC_Down_payment__c, club__c, and date__c.

My trigger on account will update down_payment__c field with value from down_payment__c field.

for example: Account A for club vancouver, date mar 07 has down_payment__c of $100.
Account B for club vancouver, date mar 07 has down_payment__c of $50.
Account C for club vancouver, date mar 07 has down_payment__c of $150.

Trigger will update existing record in abc_and_sale_summary__c for club vancouver and date mar 07. Value of ABC_Down_payment__c should be $300.


Code of update trigger. Right now, it is not updating the ABC_Down_payment__c field. Please help me point out the problem in logic.
List<ABC_and_Sale_Summary__c> abc= New List<ABC_and_Sale_Summary__c>();
    Map<Id, ABC_and_Sale_Summary__c> abcMap= New Map<Id, ABC_and_Sale_Summary__c>();

if(Trigger.isUpdate ){
        ABC_and_Sale_Summary__c abc2= New ABC_and_Sale_Summary__c();
        List<ABC_and_Sale_Summary__c> abc3= New List<ABC_and_Sale_Summary__c>();
        
        abc3 = [Select id, club__c, date__c, sale_Summary_Revenue__c, ABC_Down_payment__c from ABC_and_Sale_Summary__c];

        for(ABC_and_Sale_Summary__c abc_sale: abc3){

        for(integer c = 0; c < Trigger.Old.size(); c++){
            Date d = date.newinstance(Trigger.New[c].created_date__c.year(), Trigger.New[c].created_date__c.month(), Trigger.New[c].created_date__c.day());
	    	Date d2 = date.newinstance(Trigger.Old[c].created_date__c.year(), Trigger.Old[c].created_date__c.month(), Trigger.Old[c].created_date__c.day());
			
            
                if((abc_sale.club__c == Trigger.Old[c].club__c) && (abc_sale.date__c == d2))
				{
                    system.debug(abc_sale.Id);
					String dp = Trigger.New[c].Down_Payment__c;
            
					if(dp.contains('$')){
						dp = dp.substring(1);
					}
					
					Double downpayment = Decimal.valueOf(dp);
            
					
						String dpold = Trigger.Old[c].Down_Payment__c;
						if(dpold.contains('$')){
							dpold = dpold.substring(1);
						}
						
						Double downpaymentold = Decimal.valueOf(dpold);
						
						double diff = downpayment - downpaymentold;
                    	if(abc_sale.ABC_Down_payment__c ==null){abc_sale.ABC_Down_payment__c = 0;}
						abc_sale.ABC_Down_payment__c = abc_sale.ABC_Down_payment__c + diff;
						
						abc.add(abc_sale);
                        
				}	
			}	
        }
        abcMap.putAll(abc);
            update abcMap.values();
    }

this code is working for single insert/update of record. But, it is not working in case of mass upsert.
Best Answer chosen by Jasjeet Grewal
sachin kadian 5sachin kadian 5
Hey i tried a rough code to give some idea how can you achieve this. 
 
if(Trigger.isUpdate){
  //first collect all the Club__c from account. why to query all the ABC_and_Sale_Summary__c
  List<String> listOfClubs = new List<String>();

  //create a map of change in Down payment with club and Date
  Map<String,Decimal> mapOfChangeDownPayments  = new Map<String,Decimal>();
  
  for(Integer x=0;x<Trigger.new.size();x++){
        var newAccount = Trigger.new[x];
        var oldAccount - Trigger.old[x];
        
        listOfClubs.add(newAccount.Club__c);

        //calculate amount difference 
        Decimal amountDifference = newAccount.Down_Payment__c - oldAccount.Down_Payment__c;

        //key for map will be date+ club . for example '7mar-vancouver'
        String key = newAccount.club__c + '-' + newAccount.date__c;

        //there may be a chance that 2 or more accounts with same key and club are updated together
        if(!mapOfChangeDownPayments.containsKey(key)){
            mapOfChangeDownPayments.put(key,amountDifference);
        }else{
            mapOfChangeDownPayments.put(key,mapOfChangeDownPayments.get(key)+amountDifference);
        }

  }

  //now you have clubs, query all the Sale summary record which belongs to these clubs
  List<ABC_and_Sale_Summary__c> listOfABC = [select id,club__c,date__c,sale_Summary_Revenue__c,ABC_Down_payment__c
                                              from ABC_and_Sale_Summary__c where club__c IN : listOfClubs];


  //now loop through the sales summary records, check if the mapOfChangeDownPayments contains amount change for them , if yes, update it
  for(ABC_and_Sale_Summary__c summary : listOfABC){
    if(mapOfChangeDownPayments.containsKey(summary.club__c + '-' + summary.date__c)){
       summary.ABC_Down_payment__c  = mapOfChangeDownPayments.get(summary.club__c + '-' + summary.date__c);
    }
  }

  //you can update the list at last
  update listOfABC;
}

Let me know if it helps you..