You need to sign in to do that
Don't have an account?
lizmijohn
Rollup summary using look up field
Hi,
I have written the apex code for roll up summary using look up fields. My requirement is as follows.
Opportunity line item 1: Name - Product A
Quantity - 1
Price - 100 AED
Opportunity line item 2: Name - Product B
Quantity - 6
Price - 250 AED
At Opportunity level: I created the following fields
Product A = Product A from line item 1
A Qty = Quantity from line item 1
A Price = Price from line item 1
Product B = Product B from line item 2
B Qty = Quantity from line item 2
B Price = Price from line item 2
I wrote the trigger on Opportunity product as follows:
**********************************************************************************************************************
trigger Opplnitemtrigger on OpportunityLineItem(after delete, after insert, after undelete, after update) {
Decimal sumOpplnitemqty = 0;
Decimal sumOpplnitemprice = 0.00;
String Opplnitemname =NULL;
OpportunityLineItem[] cons;
Set<ID> OppIds = new Set<ID>();
if (trigger.isInsert || trigger.isUpdate || trigger.isUndelete) {
for (OpportunityLineItem con : trigger.new) {
OppIds.add(con.OpportunityId);
}
}
if (Trigger.isDelete)
cons = trigger.old;
Map<ID,OpportunityLineItem>lineValue=new Map<ID, OpportunityLineItem>([select Id,OpportunityId,Quantity,UnitPrice,Product2.name,Tick_green__c,Tick_Defence__c,Tick_Tech__c,Tick_Smart__c from OpportunityLineItem where OpportunityId in:OppIds ]);
Map<ID, Opportunity> OppToUpdate = new Map<ID, Opportunity>([select Id,Green_qty__c,Green_price__c,Green__c,Defence_qty__c,Defence_price__c,Defence__c,Technology__c,Tech_qty__c,Tech_price__c,
Smart_Phone__c,Smart_Phone_price__c,Smart_Phone_qty__c from Opportunity where Id in :OppIds ]);
for (Opportunity coun : OppToUpdate.values()) {
Set<ID> conIds = new Set<ID>();
for (OpportunityLineItem con : lineValue.values()) {
sumOpplnitemqty = con.Quantity ;
sumOpplnitemprice = con.UnitPrice;
Opplnitemname = con.Product2.name ;
if((con.Product2.name == 'Green Basket') && (con.Tick_green__c == 1)){
coun.Green_qty__c = sumOpplnitemqty;
coun.Green_price__c= sumOpplnitemprice ;
coun.Green__c = Opplnitemname;
} else if((con.Product2.name == 'Defence Basket') && (con.Tick_Defence__c == TRUE)){
coun.Defence_qty__c = sumOpplnitemqty;
coun.Defence_price__c= sumOpplnitemprice ;
coun.Defence__c = Opplnitemname ;
}else if((con.Product2.name == 'Technology Basket') && (con.Tick_Tech__c == TRUE)){
coun.Tech_qty__c = sumOpplnitemqty;
coun.Tech_price__c= sumOpplnitemprice ;
coun.Technology__c = Opplnitemname ;
}else if((con.Product2.name == 'Smart Phone Basket') && (con.Tick_Smart__c == TRUE)){
coun.Smart_Phone_qty__c = sumOpplnitemqty;
coun.Smart_Phone_price__c= sumOpplnitemprice ;
coun.Smart_Phone__c= Opplnitemname ;
}else {
sumOpplnitemqty = 0 ;
sumOpplnitemprice = 0 ;
Opplnitemname = null ;
}
}
}
update OppToUpdate.values();
*********************************************************************************************************
On update and insert of line item trigger, the fields in Opportunity are changed correctly. But when I delete one of the line item say Product B, even then the field in Opportunity (Product B, B Qty and B Price) retains the value it took on insert or update. I would like to empty these fields on respective product's line item deletion.
Please help me for writing the code that action on line item deletion.
Thanks,
Liz
I have written the apex code for roll up summary using look up fields. My requirement is as follows.
Opportunity line item 1: Name - Product A
Quantity - 1
Price - 100 AED
Opportunity line item 2: Name - Product B
Quantity - 6
Price - 250 AED
At Opportunity level: I created the following fields
Product A = Product A from line item 1
A Qty = Quantity from line item 1
A Price = Price from line item 1
Product B = Product B from line item 2
B Qty = Quantity from line item 2
B Price = Price from line item 2
I wrote the trigger on Opportunity product as follows:
**********************************************************************************************************************
trigger Opplnitemtrigger on OpportunityLineItem(after delete, after insert, after undelete, after update) {
Decimal sumOpplnitemqty = 0;
Decimal sumOpplnitemprice = 0.00;
String Opplnitemname =NULL;
OpportunityLineItem[] cons;
Set<ID> OppIds = new Set<ID>();
if (trigger.isInsert || trigger.isUpdate || trigger.isUndelete) {
for (OpportunityLineItem con : trigger.new) {
OppIds.add(con.OpportunityId);
}
}
if (Trigger.isDelete)
cons = trigger.old;
Map<ID,OpportunityLineItem>lineValue=new Map<ID, OpportunityLineItem>([select Id,OpportunityId,Quantity,UnitPrice,Product2.name,Tick_green__c,Tick_Defence__c,Tick_Tech__c,Tick_Smart__c from OpportunityLineItem where OpportunityId in:OppIds ]);
Map<ID, Opportunity> OppToUpdate = new Map<ID, Opportunity>([select Id,Green_qty__c,Green_price__c,Green__c,Defence_qty__c,Defence_price__c,Defence__c,Technology__c,Tech_qty__c,Tech_price__c,
Smart_Phone__c,Smart_Phone_price__c,Smart_Phone_qty__c from Opportunity where Id in :OppIds ]);
for (Opportunity coun : OppToUpdate.values()) {
Set<ID> conIds = new Set<ID>();
for (OpportunityLineItem con : lineValue.values()) {
sumOpplnitemqty = con.Quantity ;
sumOpplnitemprice = con.UnitPrice;
Opplnitemname = con.Product2.name ;
if((con.Product2.name == 'Green Basket') && (con.Tick_green__c == 1)){
coun.Green_qty__c = sumOpplnitemqty;
coun.Green_price__c= sumOpplnitemprice ;
coun.Green__c = Opplnitemname;
} else if((con.Product2.name == 'Defence Basket') && (con.Tick_Defence__c == TRUE)){
coun.Defence_qty__c = sumOpplnitemqty;
coun.Defence_price__c= sumOpplnitemprice ;
coun.Defence__c = Opplnitemname ;
}else if((con.Product2.name == 'Technology Basket') && (con.Tick_Tech__c == TRUE)){
coun.Tech_qty__c = sumOpplnitemqty;
coun.Tech_price__c= sumOpplnitemprice ;
coun.Technology__c = Opplnitemname ;
}else if((con.Product2.name == 'Smart Phone Basket') && (con.Tick_Smart__c == TRUE)){
coun.Smart_Phone_qty__c = sumOpplnitemqty;
coun.Smart_Phone_price__c= sumOpplnitemprice ;
coun.Smart_Phone__c= Opplnitemname ;
}else {
sumOpplnitemqty = 0 ;
sumOpplnitemprice = 0 ;
Opplnitemname = null ;
}
}
}
update OppToUpdate.values();
*********************************************************************************************************
On update and insert of line item trigger, the fields in Opportunity are changed correctly. But when I delete one of the line item say Product B, even then the field in Opportunity (Product B, B Qty and B Price) retains the value it took on insert or update. I would like to empty these fields on respective product's line item deletion.
Please help me for writing the code that action on line item deletion.
Thanks,
Liz
OppIds.add(con.OpportunityId);
I think same you need to do for delete by looping over trigger.old
if (Trigger.isDelete)
cons = trigger.old;
Thanks for your reply. I tried that , but the fields in Opportunity still takes the old values and do not empty.