function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nitin KunalNitin Kunal 

trigger

trigger on opportunity to delete product record of opportunity in salesforce

 

Please tell me how to do this

digamber.prasaddigamber.prasad

Hi

 

Below is simplest trigger I can think of for this:-

 

trigger deleteProduct on Opportunity(before update){
	Set<Id> setProductId = new Set<Id>();
	for(Opportunity oppty : trigger.new){
		if(oppty.Product__c != null)
			setProductId.add(oppty.Product__c);	//assuming lookup of product has name Product__c
	}
	if(setProductId.size() > 0){
		List<Product2> lstProduct2 = [Select Id from Product2 where Id in: setProductId];
		delete lstProduct2
	}
}

 

Let me know if you have any specific question about this.

 

Happy to help you!

 

 

 

hamshuhamshu

 

HI u can try some thing like this...

 

trigger Deleteopp on Opportunity (after update)
{
    set<id> ids=new set<id>();
    
    for(Opportunity  opp:trigger.old)
    {
        ids.add(opp.id);
    }
    list<OpportunityLineItem > deleteproduct1 =new  list<OpportunityLineItem >();
    for(OpportunityLineItem getproductid: [select id from OpportunityLineItem where Opportunityid=:ids])
    {
        OpportunityLineItem ooo =new OpportunityLineItem(id=getproductid.id);
        deleteproduct1.add(ooo);
    }
 
    delete deleteproduct1;
    
}

 

 

Thanks,

jaba