+ Start a Discussion
aquelleraqueller 

Deleting a record from within a trigger

I am looking for help on the following issue.

 

I have a trigger that is supposed to delete certain types of opportunities, identified

by a custom field   PDS__Document_Type__c  as a 'Sales Order'.

 

My code is as follows:

 

trigger DeleteSO on Opportunity(after update)
{
   // Create a list of Opportunities, newOpps, to make it bulk fiendly
   List<Opportunity> newOpps = new List<Opportunity>();

   for(Opportunity o : Trigger.new)
   {
      // If it is a sales order, delete it
      if(o.PDS__Document_Type__c == 'Sales Order')
      {
          delete o;
      }
   }

   update newOpps;
}
      

 When I run it, I get an error:   DML statement cannot operate on trigger.new or trigger.old

 

Why do I get this error?

How can I fix it?



           Thx,

 

                      aqueller

 

 

 

 

VJSFDCVJSFDC

Hi,

 

Please check the below condition which SFDC ask us to check:

 

Context Variable Considerations

 

Be aware of the following considerations for trigger context variables:

*trigger.new and trigger.old cannot be used in Apex DML operations.

•You can use an object to change its own field values using trigger.new, but only in before triggers. In all after triggers,trigger.new is not saved, so a runtime exception is thrown.

• trigger.old is always read-only.

•You cannot delete trigger.new.

 

Looks like your script is violating the Last Condition.=== •You cannot delete trigger.new.

 

Hope this helps. :)

 

Thanks

aquelleraqueller

Thank you for your quick reply.

 

This explains the problem. I am left however with the main issue: how can I delete this record?

- These records are uploaded to the system, along with many others. I am trying to identify them

and delete them programmatically once they are uploaded.

 

Any suggestions?

 

        Thanks

Starz26Starz26

This:

 

trigger DeleteSO on Opportunity(after update)
{
  List<Opportunity> tbdOpps = New List<Opportunity>();

   // Create a list of Opportunities, newOpps, to make it bulk fiendly
   List<Opportunity> newOpps = new List<Opportunity>();

   for(Opportunity o : Trigger.new)
   {
      // If it is a sales order, delete it
      if(o.PDS__Document_Type__c == 'Sales Order')
      {
          tbdOpps.add(o);
      }
   }
   if(tbdOpps.size()>0)
      delete tbdOpps;
   
update newOpps; //Not sure what you are doing here with an empty list....
}

 

VJSFDCVJSFDC

Hi,

 

Two things you can delete the SalesOrder Oppty:

 

1. Script which Starz26 has provided.

2. Write a Batch Apex which queries for SalesOrder Oppty and deletes them after the System is uploaded with the Oppty records.

 

 

Thanks.

aquelleraqueller

Hi Starz26,

 

I have two problems with your suggested code:

 

1. The statement  tbdOpps.add(o.id);

     returns an error message Incompatible element type Id for collection of SOBJECT:Opportunity

     I have changed it to tbdOpps.add(o);

 

2. I am still getting the error message: DML statment cannot operate on trigger.new or trigger.old

    on the statement delete tbdOpps;

 

Do you have any other suggestions?

 

      Thx,

 

             aqueller



Starz26Starz26

Yes I do.....

 

 

trigger DeleteSO on Opportunity(after update)
{
  List<Opportunity> tbdOpps = [Select ID FROM Opportunity WHERE ID IN: trigger.new AND PDS_Document_Type__c = 'Sales Order'];

  if(tbdOpps.size()>0)
      delete tbdOpps;
   

}