-1

I have a detail page button on a Custom object which updates child collection records. In the extension I'm calling a batch to update the child records on a given criteria. The batch completes with no errors and the logs show that an update List<Sobject> call is made against an expected list of records with updated field values, but the changes are not persisted. Please advice!

Here's the code block:

List<MySObject__c> sobjectsToUpdate = new List<MySObject__c>();
        for (MySObject__c obj : objects) {             
            if ('Some complex condition here') {

                 if (obj.Type__c == 'Type 1' ) {
                     obj.Status__c = 'Status 1';                      
                 }
                 else {
                     obj.Status__c = 'Status 2';
                 }
                 obj.Date__c = Datetime.now();
                 sobjectsToUpdate.add(obj);                                 
            }
        }

        if (!sobjectsToUpdate.isEmpty()) {        
            System.debug(sobjectsToUpdate); // I have this collection logged, and the records are as expected
            Database.SaveResult[] res = Database.update(sobjectsToUpdate, false);
            System.debug('All save results: ' + res); //This one is not logged.
            for (Database.SaveResult sr : res) {
                System.debug('Save result: ' + sr); //Neither this one
            }               
        }

Note: When I execute the batch logic directly in my extension, the collection is updated.

Edit: After further troubleshooting I noticed that the provided code block works for every other value except for: obj.Status__c = 'Status 1';. I don't have Validation rules.

vortex
  • 117
  • 1
  • 15
  • Maybe the batch doesn't find any record. Could you please post your code? – Martin Lezer Aug 07 '17 at 09:14
  • The batch finds recrds, and I can see in the logs that an update call is made. Like I mentioned, when I execute the batch logic directly in my extension, it works. /Same query, same logic, against the same record/ – vortex Aug 07 '17 at 09:16
  • Have you wrapped this code in a try/catch? FYI you don't need to check if the collection is empty. – Adrian Larson Aug 07 '17 at 17:18
  • Yes, I have and I got nothing logged in the catch. Thanks for the note! – vortex Aug 07 '17 at 17:20
  • 1
    The object has no triggers, validation rules, process builder processes, or workflow field updates (I swear I'm forgetting another thing)? Is your sObject the detail record in a master-detail relationship with a master record that has a roll-up summary field being updated? Is Status__c a restricted picklist, and if so is Status 1 one of the available options? – IllusiveBrian Aug 07 '17 at 17:43
  • This one is true: Is your sObject the detail record in a master-detail relationship with a master record that has a roll-up summary field being updated? – vortex Aug 07 '17 at 17:52

1 Answers1

1

Consider this more of an extended comment that a clear-cut answer. But your comments suggest...

Your code says:

System.debug(sobjectsToUpdate); // I have this collection logged..
Database.SaveResult[] res = Database.update(sobjectsToUpdate, false);
System.debug('All save results: ' + res); //This one is not logged.

and there is mention of try/catch in the comments.

A governor limit exception thrown from the Database.update would be consistent with the code comments - logic such as triggers caused to run by the update would make that more likely - and a governor limit exception can't be caught so a try/catch would not help there.

I suggest you triple check that the batch does indeed "completes with no errors " and turn on logging and review the log file generated for each invocation of the start/execute/stop methods for reported errors.

Keith C
  • 135,775
  • 26
  • 201
  • 437