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
Arash TeimoupoorArash Teimoupoor 

unable to obtain exclusive access to this record

Hi I receive this error for some of the batches. I found out that if I use "FOR Update" in my querry the problem will be solved. can someone help me to how should I add "for update" to my batch? thanks in advance


global class ProjectSettings_ChartsBatchClass implements Database.Batchable<sObject>{
    // Start Method
    global Database.QueryLocator start(Database.BatchableContext BC){
      
      
        return Database.getQueryLocator([select id,Project_Name__c,Project_Name__r.Parent_Project_Name__c,Project_Name__r.Healthplan_Contact__c,Project_Name__r.PR_Number__c,
                                 Project_Name__r.Line_Of_Business__c,Project_Name__r.Requestor__c,Project_Name__r.Project_End_Date__c,
                                 Project_Name__r.Project_Start_Date__c,Project_Name__r.ChartSecure_Account_Manager__r.Name,Project_Name__r.Extended_Project_End_Date__c,Project_Name__r.Project_Closed__c,
                                 Project_Name__r.Date_Project_Closed__c,Project_Name__r.Copy_Cost__c,Project_Name__r.Onsite_Threshold__c,
                                 Project_Name__r.Reporting_Active__c from charts__c where Project_Name__r.Total_Charts__c > 2000 and Project_Name__r.time_check__c < 3 ]);
    }
    global void execute(Database.BatchableContext BC, List<charts__c> scope){
             
        List<charts__c> Chart_All_Update = new List<charts__c>();
        for(charts__c Ps : scope){
            charts__c chart = new charts__c();
            chart.id = Ps.id;
            chart.Parent_Project_Name__c = Ps.Project_Name__r.Parent_Project_Name__c;
            chart.Healthplan_Contact__c = Ps.Project_Name__r.Healthplan_Contact__c;
            chart.Client_Number__c = Ps.Project_Name__r.PR_Number__c;
            chart.Line_of_Business__c = Ps.Project_Name__r.Line_Of_Business__c;
            Chart.Healthplan_Account_Manager__c = Ps.Project_Name__r.ChartSecure_Account_Manager__r.Name;
            chart.Healthplan_Requestor__c = Ps.Project_Name__r.Requestor__c;
            chart.Project_End_Date__c = Ps.Project_Name__r.Project_End_Date__c;
            chart.Project_Start_Date__c = Ps.Project_Name__r.Project_Start_Date__c;
            chart.Extended_Project_End_Date__c = Ps.Project_Name__r.Extended_Project_End_Date__c;
            chart.Project_Closed__c = Ps.Project_Name__r.Project_Closed__c;
            chart.Date_Project_Closed__c= Ps.Project_Name__r.Date_Project_Closed__c;
            chart.Copy_Cost__c = Ps.Project_Name__r.Copy_Cost__c;
            chart.Onsite_Threshold__c = Ps.Project_Name__r.Onsite_Threshold__c;
            chart.Reporting_Active__c = Ps.Project_Name__r.Reporting_Active__c;
            Chart_All_Update.add(chart);
        }
        if(Chart_All_Update.size() > 0){
            Update Chart_All_Update;
        }
       
    }  
    global void finish(Database.BatchableContext BC){
        
    }
}
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

You can take a look at this thread. I think it might help you with your problem:

https://developer.salesforce.com/forums/ForumsMain?id=906F000000091x4IAA (https://developer.salesforce.com/forums/ForumsMain?id=906F000000091x4IAA" target="_blank)

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Arash TeimoupoorArash Teimoupoor
Hi Zuingilio, I don't think that helpes. I'm looking to find a way to use "for Update" in my querry to lock the records that batch is updating