6

I have created a trigger for a custom object for the event "After Insert" only. When running the test for this trigger the test fails with the following error:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Assign_Serial_To_Software: execution of AfterInsert

Now, inside the trigger there is an update statement (the entire reason for the trigger in the first place).

  1. Is the error caused by the update statement? (It looks like it is)
  2. If so is there anyway to make the object update itself after insert?
4

You can not use an after update trigger to update the same object again. This could potentially create an infinite trigger loop.

Try switching to a before update trigger to make the required change.

  • 2
    I actually figured this out before i checked back on this thread, but your answer is correct. Just to note for others as well, you cannot do any update operations on an after statement, it has to be before. You also don't state "update" you just change the record on new.trigger or old.trigger. – Mattisdada Mar 12 '13 at 3:14
1

You can only make updates to the same object in a before update trigger so you're out of luck using the after update trigger for this.

If you absolutely need it to be after a record is updated you could use a timebased workflow to update some other field, and then take some action in a trigger when that field changes state. That would then be a before update trigger but would definitely be happening after your initial update.

0

Well to add here you can update the same Object/record from a After update / After insert trigger. You have to keep few things in mind

  • Write the code in a away that it doesnt fall in a recursive loop
  • Most Important : Query for the record again using SOQL and use that reference to update the record. You cant update the record reference that is retrieved by trigger.new or trigger.old.

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.