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
Jan DippenaarJan Dippenaar 

Apex Code assistance

Hi all, 
We need to improve the code on the below so that our batch job does not time out, this is as per SF support advise. 

What would be the best way to go about this? see code below

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public opportunityUtils(list<Opportunity> newOppys, map<Id,Opportunity> oldOppysMap, string operation){
    
// Do some validation on the Opportunity Amount & Record Type
    if(operation == 'validation'){
      for(Opportunity oppy :newOppys){
        
        // TO-DO:  Need to add in the Batch__c Lookup to the below logic - also need to consider other fields
        if(oppy.Batch_Closed__c == true && (oppy.Amount != oldOppysMap.get(oppy.Id).Amount || oppy.RecordTypeId != oldOppysMap.get(oppy.Id).RecordTypeId)){
          oppy.addError('ERROR');
        }  
      }
    }
  }
​--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Thanks all

J
Rufus RodenRufus Roden
Is there any more information Jan?  I'll totally admit that I don't understand the semantics of using addError inside a batch job - I would only have used it, if ever, to display errors on the edit page which isn't applicable to this case - so I find it strange it being in a batch process.
 
So, my ignorance aside, there is nothing in that code sample that is causing me any concern.  I'm wondering if either SF support has sent you off on a wild goose chase or is it that it's the rest of the code pattern in the batch process that is the cause.  I.e. has SF support said the code is timing out and when it times out it's in this method?  If that's the case then it may just be the victim rather than the culprit.