function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NJDeveloper019NJDeveloper019 

Trigger seems to fire twice

I have a simple trigger written that everytime there is an insert or update on a custom object record, that record gets inserted into another object.  This is for transaction history but currently, the only data being copied is the name and Id.  The problem is that I have Name on the history object setup as an autonumber {0000000000} starting at 1.  But everytime I make an update or insert and the trigger fires, a record is inserted into the history object but the auto number is incrementing by 2, instead of one.  If I am to go into the submit history object and create a new record by hand, the auto number increments by 1 like its supposed to.  This means to me that the trigger must be firing twice.  But it is not inserting 2 records into the history object, only 1 and its skipping the insert for the first record.

 

I have read that this is the order of execution for updates / creates:

 

1. The original record is loaded from the database (or initialized for an insert statement)
2. The new record field values are loaded from the request and overwrite the old values
3. System validation occurs, such as verifying that all required fields have a non-null value, and running any user-defined validation rules
4. All before triggers execute
5. The record is saved to the database, but not yet committed
6. All after triggers execute
7. Assignment rules execute
8. Auto-response rules execute
9. Workflow rules execute
10. If there are workflow field updates, the record is updated again
11. If the record was updated with workflow field updates, before and after triggers fire one more time (and only one more time)
12. Escalation rules execute
13. All DML operations are committed to the database
14. Post-commit logic executes, such as sending email

 

I currently have no workflows activated and even if I did, none of them make changes to the submit record.  I also have two other triggers neither update or insert into the submit object thus firing off the trigger again.  I do not understand why the trigger would be firing twice and if it is, why only the second of the two is saved in submit history thus skipping over an auto number.  If anybody has any insight on this issue, I would be glad to hear it.

 

Thanks,

 

Ralph

 

Below is a copy of the trigger I wrote to save the history.

 

 trigger submitHistoryTrigger on Submit__c (after insert, after update)
{
    Submit_History__c submitHistory = new Submit_History__c();

    for (Submit__c s : Trigger.new)
    {
        submitHistory.Submit_ID__c = s.Name;
        submitHistory.Submit__c = s.Id;
        
        insert submitHistory;
        
    }

}

 

 

AmphroAmphro

Try add a debug statement after the insert of submitHistory to see the autonumber. This will also show you if the trigger is firing twice.

Also, you should edit the trigger to handel batches. So something like this

trigger submitHistoryTrigger on Submit__c (after insert, after update) {
List<Submit_History__c> subHisList = new List<Submit_History__c>();
Submit_History__c submitHistory;

for (Submit__c s : Trigger.new) {
submitHistory = new Submit_History__c();
submitHistory.Submit_ID__c = s.Name;
submitHistory.Submit__c = s.Id;
subHisList.add(submitHistory);
}
insert subHisList;
}
NJDeveloper019NJDeveloper019
How do I add a debug statement.  Where does this debug get logged?
AmphroAmphro

Here is the trigger with a debug statement, if you used my trigger. If not, just copy the debug statement out.

trigger submitHistoryTrigger on Submit__c (after insert, after update) {
List<Submit_History__c> subHisList = new List<Submit_History__c>();
Submit_History__c submitHistory;

for (Submit__c s : Trigger.new) {
submitHistory = new Submit_History__c();
submitHistory.Submit_ID__c = s.Name;
submitHistory.Submit__c = s.Id;
subHisList.add(submitHistory);
}
insert subHisList;
for (SubmitHistory__c his : subHisList) {
System.debug('*** History AutoNumber: ' + his.Name);
}
}

 Then when you are on the page to create a Submit__c, click on the "System Log" link in the top right corner. Then create the object and it should show all logs in the system log window. If you are using the force.com IDE, create a test method and run that.

NJDeveloper019NJDeveloper019
Thank you.  I will try that out.