You need to sign in to do that
Don't have an account?

Trigger to delete a record in product when a record in opportunity is deleted.
I have written the trigger like this..plz correct me
trigger proddeltrg on Opportunity (after delete) {
Set<Id> OppIds = new Set<Id>();
List<Product2> productsToDelete = new List<Product2>();
for(Opportunity opp : trigger.Old) {
oppIds.add(opp.Id);
}
productsToDelete = [SELECT Id,Name FROM product2 ];
delete productsToDelete;
}
Hi,
Try to use following trigger.
Apex Trigger:
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/
All Answers
Try this version. The OpportunityLineItem (labeled as 'Opportunity Product') object is the junction between an Opportunity and a Product2, so you have to query those to get the Product2 IDs. I used a set for the IDs in case a product appears on the Opportunity more than once. The set elements will be unique.
I'm curious - why do you want to do this, anyway?
-Glyn
Please mark as answered if this works for you. And give kudos (click on the star) if you think I deserve them. Thanks!
Hi,
Try to use following trigger.
Apex Trigger:
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/
According to the schema in my Winter 14 dev org, 'Product2' is the lookup to a Product2 from OpportunityLineItem. If this doesn't work for you, you might need to use 'Product2Id' or 'PricebookEntry.Product2Id' as Hitesh has suggested above.
Also, it's my understanding that OpportunityLineItems have a Master-Detail relationship to Opportunities, so you shouldn't need to delete them - they will be deleted automatically. Please let me know if this turns out to be wrong. Thanks!
-Glyn
Hi Glyna,
Your Trigger works fine when i have modified like this.Anyways thanks for the guidance,
trigger proddeltrg on Opportunity ( after delete )
{
Set<Id> set_ProductIDs = new Set<Id>();
for ( OpportunityLineItem oli :
[ SELECT Id,PricebookEntry.Product2.id FROM OpportunityLineItem WHERE Opportunityid IN :trigger.old]
)
{
set_ProductIDs.add( oli.PricebookEntry.Product2.id );
}
delete [SELECT Id FROM Product2 WHERE Id IN :set_ProductIDs];
}
Regards,
Forcenovice