15

I have this trigger on a custom object

trigger mytrigger on Merchandise__c (after insert) {
merchandise__c m = [select name from merchandise__c where id =:trigger.new[0].id];
m.name = 'hi';
delete m;
}

As per this documentation,above should have given a run time error Link: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers.htm

Additionally, if you update or delete a record in its before trigger, or delete a record in its after trigger, you will receive a runtime error. This includes both direct and indirect operations. For example, if you update account A, and the before update trigger of account A inserts contact B, and the after insert trigger of contact B queries for account A and updates it using the DML update statement or database method, then you are indirectly updating account A in its before trigger, and you will receive a runtime error

Here i am deleting the record in an after insert and when i try creating a record from standard UI and save it doesnt give me run time error but below message

Record deleted

The record you attempted to access has been deleted. The user who deleted this record may be able to recover it from the Recycle Bin. Deleted data is stored in the Recycle Bin for 15 days.

Can see record simply gets deleted ,no run time error Now my query is ,documentation is not correct? or are they talking about a completely different thing?

17

You can't delete the record in Trigger.new. You can delete the record by ID, but you can't delete the record by reference.

Error

delete Trigger.new;

Okay

delete [select id from merchant__c where id in :Trigger.new];
| improve this answer | |
1

Looks like the docs are indeed incorrect, or the functionality is incorrect. I suggest you report it to SFDC. You can provide feedback directly in the docs at the bottom of the page.

| improve this answer | |
  • No, the documentation is simply ambiguous. See my answer. – sfdcfox Jul 6 '14 at 1:49
1

I was concerned that your test didn't seem to do more than access trigger.new[0] or have any loops in it, so ran a test of my own. Initially, I also changed the name, but later removed that step and just deleted the merchandise record. Here's the final code that I used:

trigger mytrigger on Merchandise__c (after insert) {
    set<Id> mID = new set<Id>();
    for(Merchandise__c m:trigger.new){
       mID.add(m.Id);
    }

    list<Merchandise__c>merch = new list<Merchandise__c>();
    If(mID.isEmpty() == false){   
       merch = [SELECT Id, Name FROM Merchandise__c WHERE Id IN: mID];
    }

    for(Merchandise__c mr : merch){
       delete mr;
    }

}

Using the above, I obtained exactly the same results as you did.

Record deleted

The record you attempted to access has been deleted. The user who deleted this record may > be able to recover it from the Recycle Bin. Deleted data is stored in the Recycle Bin for > 15 days.

So it would appear to me, there is indeed errata in the documentation.

| improve this answer | |
  • No, the documentation is simply ambiguous. See my answer. – sfdcfox Jul 6 '14 at 1:48
  • But crmprogdev is deleting a record by Id, as the merch is queried by Id.. Or am I missing something? – Guy Clairbois Jul 7 '14 at 12:47
  • What you're missing is that trigger.new itself cannot be manipulated or deleted in an after insert trigger. Good catch sfdcfox! – crmprogdev Jul 7 '14 at 12:55
0

Hi @sfdc99999 Can you try the following code.It should works.

Trigger mytrigger on Merchandise__c (AFTER INSERT) {
LIST<Merchandise__c> deleterows  = new  LIST<merchandise__c>();
FOR(Merchandise__c m :[SELECT Id FROM Merchandise__c WHERE Id IN : Trigger.NEW])
{
    deleterows.add(m);
}
IF(deleterows.SIZE() != 0){
{
    Database.DeleteResult[] deleterows_del = Database.DELETE(deleterows);
    FOR(Database.DeleteResult del: deleterows_del)
        {
            IF(del.isSuccess())
                {
                        SYSTEM.DEBUG(' Deleted record(Merchandise__c)Id  :'+del.getId());
                }
                ELSE
                {
                    SYSTEM.DEBUG('ERROR : Error occurs unable to delete (Merchandise__c)record :'+del.getErrors());
                }
        }

}
| improve this answer | |

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.