4

In the salesforce docs under the table - Considerations about certain actions in different trigger events Context Variable Considerations.

For the Trigger event after Insert Under the column Can delete original object using a delete DML operation It is given Allowed, but unnecessary.The object is deleted immediately after being inserted. Here does Original object mean the record that is being inserted ? If possible can someone please elaborate what the statement means ?

3 Answers 3

3

that is an interesting question. Here's my read on it: I think it's a documentation bug.

Note the equally odd and identical phrasing wording for the After Undelete context.

2

Let's consider what this would look like:

trigger PointlessTrigger on Account (after insert) {
  list<Account> toDelete = new list<Account>();
  for (Account a : Trigger.new) {
  if (a.BillingCity == 'Forbidden City')
    toDelete.add(a)
  }

  delete toDelete;
}

So what happens when we insert a new account with billing address city = 'Forbidden City'? Working from this handy page, we know what comes next:

  1. A new Account record is initialized
  2. Fields are assigned, including Billing City. If the record is being saved from a standard UI edit page, system validations run.
  3. Before triggers run
  4. Validation rules are re-run (to check any changes made in the before trigger).
  5. The Account record is saved to the database, but not committed yet.
  6. After triggers run...

... and our after trigger deletes the record. So what was the point of inserting the record? If we want to disallow accounts with BillingCity of 'Forbidden City', we'd be better served using a Validation Rule, or performing validation in the before trigger, and using sObject.addError (or sObject.field.addError) to fail the insert and provide feedback on the error to the calling user or process. This is what I think is meant by "Allowed, but unnecessary" - there's no good use case for deleting a record in an after trigger that was inserted in the DML transaction that triggered the trigger.

2
  • I think you're right about what the documentation means, but I also think there could be a good reason to insert and then immediately delete a record. Record de-duplication is an example, especially for Leads and Contacts. I also don't know why the writer chose to say this is unnecessary. I mean, everything is unnecessary if you don't feel like using it. Oct 4, 2013 at 7:41
  • Even for de-duplication, I think it is a better practice to do the validation in the before trigger, and avoid the work of saving the record to the database only to remove it immediately. By setting an error on the record (or a field of the record) you can prevent the insert entirely. Oct 4, 2013 at 14:34
1

In after insert trigger, you can delete object which is created. Reason is after insert triggers fired when object is successfully inserted. So here the original object record is record which is available in after insert trigger execution context and also availabe through web interface, SOQL and other methods.

Original records can be available in after type trigger only but not in after delete.

In case if trigger is before insert then we can't say that object a original object record because it has not been inserted and there may be chances that some exception occur during its insertion or in trigger context.

1
  • Here the statement speaks about Original object rather than Original object record if I am correct.Also, it states that the object is deleted immediately after being inserted, which is what confusing me.
    – codebandit
    Oct 3, 2013 at 13:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .