+ Start a Discussion
JN22JN22 

Trigger.Old issue

Hello,

 

I have a trigger that fires on OpportunityLineItem and updates the associated Opportunity with a count of Opportuity Products by grouping and a sum of their TotalPrice by grouping.  I have a custom field on the Opportunity called Coach_Count__c (gives number of products that have Prod_Grp__c = "Coaching") and one called Coach_Dol__c (sums the TotalPrice field for all products that have Prod_Grp__c = "Coaching").  I also created a custom field in the Opportunity Product called Count__c, which is simply a "1" and I sum that to get the count, and I created another field called Total_Price__c which is a formula field that has the value of TotalPrice from the Opportunity Product.  My trigger works as expected when adding or editing Products as both the Coach_Count__c and Coach_Dol__c fields get updated.  The Coach_Count__c field also updates properly when deleting products.  However, the Coach_Dol__c fails to update when there are multiple products where Prod_Grp__c = "Coaching" and will only update when there are none left.  Below is the Trigger.Old section of my trigger.  Can anyone tell me why I cannot get it to update when there are multiple products that have the same Prod_Grp__c value?

 

Map<id,id> M= new map<id,id>();
For(OpportunityLineItem deliv2:trigger.old){

oppIds.add(deliv2.OpportunityId);
m.put(deliv2.opportunityid,deliv2.id);
}
opp = [SELECT Id, Coach_Dol__c,Coach_Count__c
FROM Opportunity
WHERE Id IN:oppIds];

for(Opportunity o: opp){
if(trigger.oldmap.get(M.get(o.id)).Prod_Grp__c=='Coaching')
o.Coach_Count__c-=trigger.oldmap.get(M.get(o.id)).Count__c;

else if(trigger.oldmap.get(M.get(o.id)).Prod_Grp__c=='Coaching')
o.Coach_Dol__c-=trigger.oldmap.get(M.get(o.id)).Tot_Price__c;

}
Update (Opp);

Best Answer chosen by Admin (Salesforce Developers) 
ian b goodeian b goode

Isn't

 

if(trigger.oldmap.get(M.get(o.id)).Prod_Grp__c=='Coaching')

 

the same as 


else if(trigger.oldmap.get(M.get(o.id)).Prod_Grp__c=='Coaching')

 

therefore the else condition will never will never fire

All Answers

ian b goodeian b goode

Isn't

 

if(trigger.oldmap.get(M.get(o.id)).Prod_Grp__c=='Coaching')

 

the same as 


else if(trigger.oldmap.get(M.get(o.id)).Prod_Grp__c=='Coaching')

 

therefore the else condition will never will never fire

This was selected as the best answer
Vinit_KumarVinit_Kumar

Agreed your if and else condition are the same ,please verify.

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi JN22,

 

I think your if and else part have same condition and it never goes to else part because when if conditon met then else part does not execute.

 

if you want to update both fields with same condition then please use following code: (remove else if and put only if)

 

 

for(Opportunity o: opp){
if(trigger.oldmap.get(M.get(o.id)).Prod_Grp__c=='Coaching')
o.Coach_Count__c-=trigger.oldmap.get(M.get(o.id)).Count__c;

if(trigger.oldmap.get(M.get(o.id)).Prod_Grp__c=='Coaching')                       // Removed else if and put only if
o.Coach_Dol__c-=trigger.oldmap.get(M.get(o.id)).Tot_Price__c;

}

 

If this is your solution then please mark as a solution and it helps others who needs similar type of solution.

 

JN22JN22

Thanks Ian.  I forgot to include that one of my IF conditions had another part to it, however, it was the else condition that was causing the problem.  I removed the else and it works fine. Thanks for your help!