1

I have an Account Trigger where I would like to reset some of the static variables when the trigger is re-invoked by Salesforce during partial success.

Is there any possibility of understanding or identifying if the trigger is re-invoked because of partial commit of some records? I simply don't want to reset the static variables as they stop the recursive firing of the same trigger.

0

Identification is not possible so in this case what I would suggest is - Loop through the saveresults and if there is error, take the static variable back to original state, so that the trigger executes again.
Considering the original state as false, go through the below patch of code.

triggerExecuteFlag = true;   
Database.SaveResult[] srList = Database.insert(accts, false);

        // Iterate through each returned result
        for (Database.SaveResult sr : srList) {
        if (sr.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully inserted account. Account ID: ' + sr.getId());
        }
        else {
            triggerExecuteFlag = false;   
            // Operation failed, so get all errors                
            for(Database.Error err : sr.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Account fields that affected this error: ' + err.getFields());
            }
        }
    }
5
  • By the time I will get the Database.SaveResult values, the trigger would have fired a couple of times to save only the successful processed records. Basically the first time the trigger invokes with say 10 account records all static variables inside trigger are set. Out of which 8 failed and 2 passed. The trigger will invoke again within the same transaction, but since my static flags are set, they create trouble during the re-invoking of the trigger.
    – Tilak De
    Jul 13 '15 at 6:18
  • Let's say that you have 3 records that you want to insert with a Database.insert call. If one of them fails Salesforce will roll back all database-related operations and then do the insert again with the 2 that succeeded. Hence if you reset the flag on error, the database operations are executed only once (i.e. during success). Jul 13 '15 at 6:21
  • Yes, correct. But how to know about the error? I don't have an addError. A validation rule fails(Say AccountName was left blank) and the transaction is rolled back and re-attempted with the correct subset of records.
    – Tilak De
    Jul 13 '15 at 6:25
  • Well, the 'else' loop to sr.isSuccess() is only source to conclude that an error has occurred during DML operation. Jul 13 '15 at 6:28
  • N.B. for others reaching this point -- if the transaction initiator is Data Loader or a SOAP API client, the above approach won't work as nowhere in the apex code will the partial success be caught
    – cropredy
    Dec 14 '17 at 18:03

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.