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
Natalya Murphy 8Natalya Murphy 8 

Batch job works from developer console but not from scheduler

I have a batchable class that's called via a scheduler class nightly.   It's failing from the scheduler with the error message "First error: Read access denied for ChargentSFA__Transaction__c, controller action methods may not execute."

BUT: when I invoke the batchable class directly from Developer Console via Execute Anonymous, the class runs just fine. 

Any ideas on why this would run fine via anonymous execute but fail from the scheduler?

Scheduler class:
global class RecepitBatchScheduler implements Schedulable {
   global void execute(SchedulableContext SC) {
      Database.executeBatch(new Receipt_Batch());
   }
}

Batchable class (with identifying information modified).   When running from the scheduler, the job fails at line 26 (pdf.getContent()).  
global class Receipt_Batch implements Database.Batchable<SObject>,database.stateful {
        
   	global List<ChargentSFA__Transaction__c> start(Database.BatchableContext BC){
        
        return [select id,ChargentSFA__Opportunity__r.name,ChargentSFA__Opportunity__r.Costumer__r.name,
              ChargentSFA__Opportunity__r.Costumer__r.email__c 
              from ChargentSFA__Transaction__c
             where To_Process_Receipt_with_Batch__c=true];
        
        
   	}

   	global void execute(Database.BatchableContext BC, List<ChargentSFA__Transaction__c> scope){
       OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'example@example.com'];
       Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage>();
    	List<Attachment> L_attachments=new List<Attachment>();
        List<ChargentSFA__Transaction__c> L_transup = new List<ChargentSFA__Transaction__c>();
    	for(ChargentSFA__Transaction__c tra: scope){
        	Blob b;
			PageReference pdf = Page.Receipt;
    		pdf.getParameters().put('id',tra.id);
    		pdf.setRedirect(true);
            if (Test.IsRunningTest())
            	b = Blob.valueOf('UNIT.TEST');    
           	else
				b = pdf.getContent(); //Batch jobs fails at this line when run from scheduler
        
        	Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName('Customer Receipt.pdf');
    		efa.setBody(b);
        
        	Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.toAddresses = new String[] {tra.ChargentSFA__Opportunity__r.Costumer__r.email__c};
            message.subject = 'Customer Receipt';
            String Body='Dear '+tra.ChargentSFA__Opportunity__r.Costumer__r.name+',<br/><br/>Thank you for entrusting us with your wellness. Please find attached your receipt --feel free to contact your Coach with any questions or concerns!';
            Body=Body+'<br/><br/>Best Regards,';
            Body=Body+'<br/><br/>The Company';
            message.sethtmlbody(body); 
            
            if (Test.IsRunningTest() == false)
            	message.setOrgWideEmailAddressId(owea.get(0).id);
			message.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
            messages.add(message);
        
        	Attachment att=new Attachment();
			att.Body=b ;
			att.Name='Receipt ' + System.now().format('yyyy_MM_dd_hh_mm_ss') + '.pdf';
			att.parentId=tra.id;
			L_attachments.add(att);
            
            L_transup.add(new ChargentSFA__Transaction__c(id=tra.id,To_Process_Receipt_with_Batch__c=false));

    	}
    	Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
    
    	insert L_attachments;
        
        update l_transup;
        
     
    }

   	global void finish(Database.BatchableContext BC){
   	}
}

 
PawanKumarPawanKumar
you make your controller global and without sharing, then see if this error disappears. Otherwise please share you VF and controller as well here.