You need to sign in to do that
Don't have an account?
Vaibhav Parashar 2
Trigger to delete a record in product when a field in opportunity is changed?
Trigger to delete a record in product when a field in opportunity is changed?
function readOnly(count){ }
You need to sign in to do that
Don't have an account?
The only problem is I am unable to delete Product records in Before update part.
trigger addProductonPriceBookChange on Opportunity (after insert,before update,after update) {
for(Opportunity opp : Trigger.new){
if( Trigger.isAfter && Trigger.isInsert )
{
//List that Queries the fields of opportunity
List<Opportunity> oppList = New List<Opportunity>();
oppList = [Select id,name,Pricebook2.id,Pricebook2.Name from Opportunity where id =: opp.id];
List<PriceBookEntry> pbE = [Select id,name,Pricebook2Id,Pricebook2.Name from PriceBookEntry where Pricebook2Id =: opp.Pricebook2Id ];
List<OpportunityLineItem> proList = New List<OpportunityLineItem>();
for(PriceBookEntry pb : pbE){
OpportunityLineItem p = new OpportunityLineItem();
p.PricebookEntryId=pb.id;
p.Quantity=1;
p.OpportunityId=opp.Id;
p.UnitPrice=100;
proList.add(p);
}
insert proList;
system.debug('The values present in proList is ------------->' +proList);
system.debug(' Is Executed after Insert ');
}
else if ( Trigger.isBefore && Trigger.isUpdate )
{
if(system.trigger.OldMap.get(opp.Id).Pricebook2Id != system.trigger.NewMap.get(opp.Id).Pricebook2Id){
system.debug(' Price Book was Changed ');
List<PriceBookEntry> pbE1 = [Select id,name,Pricebook2Id,Pricebook2.Name from PriceBookEntry where Pricebook2Id =: system.trigger.NewMap.get(opp.Id).Pricebook2Id];
system.debug(' -----------The price book entry are --- ' +pbE1);
List<OpportunityLineItem> proList1 = New List<OpportunityLineItem>();
proList1 = [Select id, name from OpportunityLineItem where OpportunityId =: opp.Id ];
system.debug(' -----------The line items are --- ' +proList1);
}
}else if ( Trigger.isAfter && Trigger.isUpdate )
{
if(system.trigger.OldMap.get(opp.Id).Pricebook2Id != system.trigger.NewMap.get(opp.Id).Pricebook2Id){
List<Opportunity> oppList = New List<Opportunity>();
oppList = [Select id,name,Pricebook2.id,Pricebook2.Name from Opportunity where id =: opp.id];
List<PriceBookEntry> pbE = [Select id,name,Pricebook2Id,Pricebook2.Name from PriceBookEntry where Pricebook2Id =: opp.Pricebook2Id ];
List<OpportunityLineItem> proList = New List<OpportunityLineItem>();
for(PriceBookEntry pb : pbE){
OpportunityLineItem p = new OpportunityLineItem();
p.PricebookEntryId=pb.id;
p.Quantity=1;
p.OpportunityId=opp.Id;
p.UnitPrice=100;
proList.add(p);
}
insert proList;
system.debug('The values present in proList is ------------->' +proList);
}
}
}
}
You don't write SOQL queries inside a for loop and perform any DML operation inside for loop, as it is bound to hit salesforce limit.
https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
Also, your delete code should run in the after context not on before context.
Your code should look something like this: Let me know if you need any help in writing the trigger.