You need to sign in to do that
Don't have an account?
Jasjeet 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.
this code is working for single insert/update of record. But, it is not working in case of mass upsert.
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.
Let me know if it helps you..