Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?
share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer
1  
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
add comment

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.

share|improve this answer
add comment

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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