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
Syed Ovais AliiSyed Ovais Alii 

Apex batch not committing records in production but executing without errors. Also working fine in sandbox.

I have an apex batch which i tested in sandbox and it is working as expected but in production it is executing without error but not commiting the records.

I executed the same batch code in anonymous block in production that worked fine and did the commit.

Batch Code Below
global class AppointmentAvailabilityUpdateBatch  implements 
Database.Batchable<sObject>, Database.Stateful {
    
    
    global Integer recordsProcessed = 0;
    global Database.QueryLocator start(Database.BatchableContext bc) {
        Id recordTypeId =Schema.SObjectType.Appointment__c.getRecordTypeInfosByName()
            .get('Available').getRecordTypeId();
        
        DateTime dt = Date.Today().addDays(2);
        
        return Database.getQueryLocator(
            'Select id,Medical_Expert__c,Clinic__c from Appointment__c '+
            'where recordTypeid =: recordTypeId and Appointment_Date_Time__c >=: dt'
        );
    }
    
    global void execute(Database.BatchableContext bc, List<Appointment__c> scope){
        
        Set<id> clinicIdset = new Set<id>();
        Set<id> expertId = new Set<id>();
        Set<id> expertIdAvlApt = new Set<id>();
        
        
        Map<id,List<id>> clinicsWRTExpert = new Map<id,List<id>>();
        
        List<Expert_and_Clinic_Link__c>  ecUpdatenewList = new List<Expert_and_Clinic_Link__c>();
        
        List<Expert_and_Clinic_Link__c>  ecUpdateList = new List<Expert_and_Clinic_Link__c>();
                
          for(Appointment__c apt : scope){
            
            List<id> clinicidsList = new List<id>();
            
            if(clinicsWRTExpert.containsKey(apt.Medical_Expert__c)){
                clinicidsList = clinicsWRTExpert.get(apt.Medical_Expert__c);
                
                if(!clinicidsList.contains(apt.Clinic__c)){
                    clinicidsList.add(apt.Clinic__c); 
                }
               
                clinicsWRTExpert.put(apt.Medical_Expert__c,clinicidsList); 
            }else{
                clinicidsList.add(apt.Clinic__c);     
                clinicsWRTExpert.put(apt.Medical_Expert__c,clinicidsList);   
                
            }
            
            
        }
        
        List<Expert_and_Clinic_Link__c>  ecList = [Select id,Avalability_Flag__c,Clinic__c,Contact__c
                                                  from Expert_and_Clinic_Link__c];
        
        
        if(scope != null && scope.size() != 0){
            
            
            for(Expert_and_Clinic_Link__c ec : ecList){
                
                if(clinicsWRTExpert.containsKey(ec.Contact__c)){
                    
                    List<id> clinicIds = clinicsWRTExpert.get(ec.Contact__c);
                    
                    for(Id cId : clinicIds){
                        
                        if(ec.Clinic__c == cid){
                            
                            ec.Avalability_Flag__c = true;
                            
                            if(!ecUpdateList.contains(ec)){
                                ecUpdateList.add(ec);
                            }
                            
                            recordsProcessed++;
                        }
                        
                       
                    }
                    
                }
                
            }
            
          
            
        }else{
            

            for(Expert_and_Clinic_Link__c ec : ecList){
                
                ec.Avalability_Flag__c = false; 
                ecUpdateList.add(ec);
                recordsProcessed++;
                
            }
            
        }
        
        
        for(Expert_and_Clinic_Link__c ec : ecList){
            
            if(!ecUpdateList.contains(ec)){
                
                ec.Avalability_Flag__c = false; 
                ecUpdateList.add(ec);
                
            }
        }
        
        
        
        if(ecUpdateList.size() > 0){
            
           Database.SaveResult[] results = Database.update(ecUpdateList, false);
           system.debug('results::'+ results); 
        }
                

        
        
    }
    
    
        

global void finish(Database.BatchableContext bc){
    System.debug(recordsProcessed + ' records processed. Shazam!');
    AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
                        JobItemsProcessed,
                        TotalJobItems, CreatedBy.Email
                        FROM AsyncApexJob
                        WHERE Id = :bc.getJobId()];
    
    system.debug('Total Record Updated::' + job );
    
}    
}

Code which i run in anonymous block.
 
Integer recordCount = 0;

Id recordTypeId =Schema.SObjectType.Appointment__c.getRecordTypeInfosByName()
            .get('Available').getRecordTypeId();
        
DateTime dt = Date.Today().addDays(2);


List<Appointment__c> scope = [Select id,Medical_Expert__c,Clinic__c from Appointment__c 
            where recordTypeid =: recordTypeId and Appointment_Date_Time__c >=: dt];


 	Set<id> clinicIdset = new Set<id>();
        Set<id> expertId = new Set<id>();
        Set<id> expertIdAvlApt = new Set<id>();
        
        
        Map<id,List<id>> clinicsWRTExpert = new Map<id,List<id>>();
        
        List<Expert_and_Clinic_Link__c>  ecUpdateList = new List<Expert_and_Clinic_Link__c>();
                
          for(Appointment__c apt : scope){
            
            List<id> clinicidsList = new List<id>();
            
            if(clinicsWRTExpert.containsKey(apt.Medical_Expert__c)){
                clinicidsList = clinicsWRTExpert.get(apt.Medical_Expert__c);
                
                
                if(!clinicidsList.contains(apt.Clinic__c)){
                    clinicidsList.add(apt.Clinic__c); 
                }
               
                clinicsWRTExpert.put(apt.Medical_Expert__c,clinicidsList); 
            }else{
                clinicidsList.add(apt.Clinic__c);     
                clinicsWRTExpert.put(apt.Medical_Expert__c,clinicidsList);   
                
            }
            
            
        }
        
        List<Expert_and_Clinic_Link__c>  ecList = [Select id,Avalability_Flag__c,Clinic__c,Contact__c
                                                  from Expert_and_Clinic_Link__c];
        
        
        if(scope != null && scope.size() != 0){
            
            
            for(Expert_and_Clinic_Link__c ec : ecList){
                
                if(clinicsWRTExpert.containsKey(ec.Contact__c)){

                    List<id> clinicIds = clinicsWRTExpert.get(ec.Contact__c);
                    
                    for(Id cId : clinicIds){
                        if(ec.Clinic__c == cid){
                            
                            ec.Avalability_Flag__c = true;
                            
                            if(!ecUpdateList.contains(ec)){
                                ecUpdateList.add(ec);
                            }
                            
                            recordCount++;
                        }
                        
                       
                    }
                    
                }
                
            }
            
            
        }else{
            

            for(Expert_and_Clinic_Link__c ec : ecList){
                
                ec.Avalability_Flag__c = false; 
                ecUpdateList.add(ec);
                recordCount++;
                
            }
            
        }
        
        
        for(Expert_and_Clinic_Link__c ec : ecList){
            
            if(!ecUpdateList.contains(ec)){
                
                ec.Avalability_Flag__c = false; 
                ecUpdateList.add(ec);
                
            }
        }
        
        
        
        if(ecUpdateList.size() > 0){
           system.debug('recordCount:'+recordCount); 
           update ecUpdateList;
        }

These line in the batch class are executing. i debugged it but seems like it rolls back after that. 
if(ecUpdateList.size() > 0){
            
           Database.SaveResult[] results = Database.update(ecUpdateList, false);
           system.debug('results::'+ results); 
        }

Same batch is working fine in sandbox without any issue. I already checked the profile permission fls and class access everything seems fine.

HELP!!!!
 
SwethaSwetha (Salesforce Developers) 
HI Syed,
Can you compile all classes in production and see if that fixes the issue.Thanks
Ramprasad VarriRamprasad Varri
1) Implement try{} catc{}
2) Activate Debugging 
3) Possibly there is a hard-coded reference say on recordId or record type which is present in dev box but was not migrated to prod org.

Implementing try catch and logging the exception will help.

Using Database.Udate() is suppressing exception. Make sure you want to allow partial processing. 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database.htm
Johnnie JenkinsJohnnie Jenkins
I am a huge fan of your blog post. I've been waiting for your next post. It's really fun to read. Free Software (https://crackedlink.com/). It's going to be helpful if you check out my blog post. I'm also curious to know more about your profile. Waiting for your reply as soon as possible
RituSharmaRituSharma
See the logs. I am sure that logic is failing somewhere.