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
anshuanshu 

trigger to delete opportunity

Hi ,  opportunity should be deleted along with its related list , which is a custom object,but i m not able to delete related list

trigger deleteopportunity on Opportunity (before delete) {

 

list<string> opp1= new List<String>();

list<string> pro1= new List<String>();

list<projct__c> pro;

      for(Opportunity o : Trigger.old){

 

    opp1.add ( o.Id );

  }

  pro= [select id,name,Opportunity__c from projct__c where Opportunity__c IN:opp1];

  for(projct__c p:pro){

   pro1.add(p.id);

   }

 

clsdeleteopportunity.deleteall(pro1);

 

}

 

class code:

public with sharing class clsdeleteopportunity{

    @future

 public static void deleteall(list<string> oppid)

 {

 

list<projct__c>pro=new List<projct__c>();

list<string> pro1=new List<string>();

pro= [select id,name,Opportunity__c from projct__c where Opportunity__c IN:oppid ];

 for(projct__c p:pro){

        pro1.add(p.id);

 

    }

if(!pro.isEmpty()){

delete pro;

}

bob_buzzardbob_buzzard

By the time your future method gets executed, all the opportunities from the oppid list will have been deleted, which means all of the affected projects will have their opportunity lookups set to null.  When you then try to retrieve all projects that reference the opportunities, you get an empty list.

 

If you want the child objects to be deleted when the parent opportunity is deleted, changing your project opportunity reference from a lookup to a master-detail will give you that automatically.

 

If you can't go that route, pass the ids of the projects to delete to your future method rather than the ids of the opportunities that have been deleted.