1

I am written one trigger as below it processing certain condition has been stratified then delete the current record.But I am Unable to delete the current record.

Code:

Trigger OrderDelete on Company__c (AFTER INSERT,AFTER UPDATE){
 LIST<Company__c> deletecom = NEW LIST<Company__c>();
    FOR(Company__c com : Trigger.new)
    {
        IF((com.Delete__c == TRUE)&&(com.Order_Source_Code__c == 'DUM'))
            deletecom.add(com);                            
    }
    IF(deletecom.SIZE() != 0)
        DELETE deletecom;
} 

It throw an exception why I don't no.

EXCEPTION :

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger OrderDelete caused an unexpected exception, contact your administrator: OrderDelete: execution of AfterInsert caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.OrderDelete: line 21, column 1

Some one please help me .....

  • It would be much easier to help you if you provided the exception you are getting. Also, in the for-loop you are adding Company records to the list deleteord, but you are passing list deletecom to the delete operation - use same variable in both cases. – IvanR May 2 '14 at 7:42
3

I expect you're receiving this error (including spelling mistake, well done SF):

System.SObjectException: DML statment cannot operate on trigger.new or trigger.old

That's because the record you are trying to delete is in the Trigger.new collection. You would need to re-query the records first:

List<Company__c> companies = [Select Id From Company__c Where Id IN :Trigger.new];

Then, loop companies instead of Trigger.new. The error message is self explanatory.

| improve this answer | |
  • I have been modified my question and added exception also – Ramesh S May 2 '14 at 7:54
  • Yes, that is the same error as I describe above. Make the changes I have suggested and everything should be peachy. However, looks like you're trying to delete a record a user has just inserted....thats not going to look nice when the trigger finally does work – Phil Hawthorn May 2 '14 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.