+ Start a Discussion
force noviceforce novice 

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;
}

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi,

 

Try to use following trigger.

 

Apex Trigger:

 

trigger proddeltrg on Opportunity(before delete) {
    Set<Id> OppIds = new Set<Id>();
    Set<Id> sProdIds = new Set<Id>();
    List<Product2>  productsToDelete = new List<Product2>();
    for(Opportunity opp : trigger.Old) {
      oppIds.add(opp.Id);
    }
    List<OpportunityLineitem> lstOppLineitem = [select id,PricebookEntry.Product2Id from OpportunityLineitem where OpportunityId IN: oppIds];
    for(OpportunityLineitem oli: lstOppLineitem){
        sProdIds.add(oli.PricebookEntry.Product2Id );
    }
    if(lstOppLineitem.size() > 0){
        delete lstOppLineitem;
    }
    productsToDelete = [SELECT Id,Name FROM product2 where Id IN: sProdIds];
    if(productsToDelete.size() > 0){
        delete productsToDelete;
    }
}

 

 

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

GlynAGlynA

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!

 

trigger proddeltrg on Opportunity ( after delete )
{
Set<Id> set_ProductIDs = new Set<Id>(); for ( OpportunityLineItem oli : [ SELECT Id, Product2 FROM OpportunityLineItem WHERE Opportunity IN :trigger.old ] ) { set_ProductIDs.add( oli.Product2 ); } delete [SELECT Id FROM Product2 WHERE Id IN :set_ProductIDs]; }

 

hitesh90hitesh90

Hi,

 

Try to use following trigger.

 

Apex Trigger:

 

trigger proddeltrg on Opportunity(before delete) {
    Set<Id> OppIds = new Set<Id>();
    Set<Id> sProdIds = new Set<Id>();
    List<Product2>  productsToDelete = new List<Product2>();
    for(Opportunity opp : trigger.Old) {
      oppIds.add(opp.Id);
    }
    List<OpportunityLineitem> lstOppLineitem = [select id,PricebookEntry.Product2Id from OpportunityLineitem where OpportunityId IN: oppIds];
    for(OpportunityLineitem oli: lstOppLineitem){
        sProdIds.add(oli.PricebookEntry.Product2Id );
    }
    if(lstOppLineitem.size() > 0){
        delete lstOppLineitem;
    }
    productsToDelete = [SELECT Id,Name FROM product2 where Id IN: sProdIds];
    if(productsToDelete.size() > 0){
        delete productsToDelete;
    }
}

 

 

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/

This was selected as the best answer
GlynAGlynA

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

force noviceforce novice

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