OnAddProductUpdateTotalProductAtAccount
trigger OnAddProductUpdateTotalProductAtAccount on OpportunityLineItem (after Insert,after update) {
set<id> setOpli=new set<id>();
set<id> opportunityId=new set<id>();
for(OpportunityLineItem oppLi:trigger.new){
setOpli.add(oppLi.id);
opportunityId.add(oppLi.opportunityId);
}
List<Opportunity> lstOpp = [select Id,Name,Price_and_Quantity__c from Opportunity where Id In :opportunityId];
List<OpportunityLineItem> lstOpplI = [select Id,Quantity,TotalPrice,OpportunityId from OpportunityLineItem where Id In :setOpli];
Map<id,Opportunity> mapOpp=new Map<id,Opportunity>();
Map<Id,List<OpportunityLineItem>> mapOppIdOPLI = new Map<Id,List<OpportunityLineItem>>();
for(Opportunity opp :lstOpp){
mapOpp.put(opp.Id ,opp);
}
for(OpportunityLineItem opli :lstOpplI){
if(mapOppIdOPLI.get(opli.OpportunityId)==null){
mapOppIdOPLI.put(opli.OpportunityId,new List<OpportunityLineItem>());
}
mapOppIdOPLI.get(opli.OpportunityId).add(opli);
}
List<Opportunity> lstToUpdate = new List<Opportunity>();
for(Id i :mapOppIdOPLI.keyset()){
List<OpportunityLineItem> lstOppLineItem = mapOppIdOPLI.get(i);
decimal price = 0 ;
decimal quantity = 0;
Integer NumProduct=0;
for(OpportunityLineItem opli :lstOppLineItem){
price = price + opli.TotalPrice ;
quantity = quantity + opli.Quantity ;
NumProduct++;
}
Opportunity Opp = mapOpp.get(i);
Opp.Price_and_Quantity__c = string.ValueOf(quantity) +'-'+string.valueOf(price);
Opp.NumberOfProduct__c =string.ValueOf(NumProduct);
lstToUpdate.add(Opp);
}
if(lstToUpdate.size()>0){
update lstToUpdate;
}
}
set<id> setOpli=new set<id>();
set<id> opportunityId=new set<id>();
for(OpportunityLineItem oppLi:trigger.new){
setOpli.add(oppLi.id);
opportunityId.add(oppLi.opportunityId);
}
List<Opportunity> lstOpp = [select Id,Name,Price_and_Quantity__c from Opportunity where Id In :opportunityId];
List<OpportunityLineItem> lstOpplI = [select Id,Quantity,TotalPrice,OpportunityId from OpportunityLineItem where Id In :setOpli];
Map<id,Opportunity> mapOpp=new Map<id,Opportunity>();
Map<Id,List<OpportunityLineItem>> mapOppIdOPLI = new Map<Id,List<OpportunityLineItem>>();
for(Opportunity opp :lstOpp){
mapOpp.put(opp.Id ,opp);
}
for(OpportunityLineItem opli :lstOpplI){
if(mapOppIdOPLI.get(opli.OpportunityId)==null){
mapOppIdOPLI.put(opli.OpportunityId,new List<OpportunityLineItem>());
}
mapOppIdOPLI.get(opli.OpportunityId).add(opli);
}
List<Opportunity> lstToUpdate = new List<Opportunity>();
for(Id i :mapOppIdOPLI.keyset()){
List<OpportunityLineItem> lstOppLineItem = mapOppIdOPLI.get(i);
decimal price = 0 ;
decimal quantity = 0;
Integer NumProduct=0;
for(OpportunityLineItem opli :lstOppLineItem){
price = price + opli.TotalPrice ;
quantity = quantity + opli.Quantity ;
NumProduct++;
}
Opportunity Opp = mapOpp.get(i);
Opp.Price_and_Quantity__c = string.ValueOf(quantity) +'-'+string.valueOf(price);
Opp.NumberOfProduct__c =string.ValueOf(NumProduct);
lstToUpdate.add(Opp);
}
if(lstToUpdate.size()>0){
update lstToUpdate;
}
}
Comments
Post a Comment