+ Start a Discussion
Aakanksha Singh 11Aakanksha Singh 11 

Access the value of the variables of batch apex in normal apex class

Hello Everyone,

I'm trying to assing a value of a variable from an Batch class to a static variable in apex class. When I debug it though the developer console, I observe that the value is assigned succussfully, but when I access it through class I don't get any value i.e null. You can view my code below:

<pre>
//----Batch Class----//
global with sharing class fetchAccountBatch implements Database.Batchable<sObject>, Database.stateful{
    
    public list<account> acc= new list<account>();
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        
        string query = 'Select Id,Name from Account';
        return Database.getQueryLocator(query);
        
    }
    global void execute(Database.BatchableContext BC, list<Account> scope){
    
        for(account a: scope){
            acc.add(a);
        }
        
    }
    global void finish(Database.BatchableContext BC){
        
        system.debug(acc);
        
        fetchAccountClass.acc.addAll(acc);
        
        system.debug(fetchAccountClass.acc);
    
    }
    
}
</pre>

<pre>
//----Scheduled Class----//
global with sharing class fetchAccountSchedule implements Schedulable{     
     
     global void execute(SchedulableContext sc) {

        fetchAccountBatch bc = new fetchAccountBatch();
        database.executeBatch(bc,2000);
        system.debug(fetchAccountClass.acc);
    }
    
}
</pre>

<pre>
//----Apex Class----//
global with sharing class fetchAccountClass {   
    
    global static list<account> acc{get{
        
        if(acc==null)
            acc = new list<account>();
            
        return acc; 
    }set;}
    
    public void fetchAccount(){
        
        DateTime dt = system.now().addSeconds(1);
        integer h = integer.valueOf(dt.hour());
        integer m = integer.valueOf(dt.minute());
        integer s = integer.valueOf(dt.second());
        
        
        try{
            CronTrigger cr = [SELECT Id, CronJobDetail.Name FROM CronTrigger where CronJobDetail.Name ='TestJob'];
            if(cr!=null && cr!= new crontrigger()){
                     system.abortJob(cr.Id);
                 }
        }catch(exception e){}
        
        try{
            string cronExp = s+' '+m+' '+h+' ? * * *';
            system.schedule('TestJob',cronExp,new fetchAccountSchedule());            
            
        }catch(Exception e){
            
            apexpages.addMessages(e);
                    
        }
    }
}
</pre>

<pre>
<!--Visualforce Page-->
<apex:page controller="fetchAccountClass">

    <apex:form >
        
        <apex:sectionHeader Title="Account" subtitle="List View"/>
        
        <apex:pageblock title="Account List" >
            <apex:pageblockbuttons>
                <apex:commandButton value="Get Data" action="{!fetchAccount}" rerender="AccountTable"/>
            </apex:pageblockbuttons>
        
            <apex:pageMessages/>
            <apex:pageblocktable value="{!acc}" var="key" id="AccountTable">
            
                <apex:column value="{!key.Id}"/>
                <apex:column value="{!key.name}"/>
            
            </apex:pageblocktable>
        </apex:pageblock>
    </apex:form>

</apex:page>

</pre>

Please Respond.

Thanks in advance.

Regards
Aakanksha Singh
 
Best Answer chosen by Aakanksha Singh 11
Banwari kevat1Banwari kevat1
Hi Aakanksha,
    One more thing you keep in mind is that batch apex runs asynchronously and here you try to access just after apex job queued. Here we can not be sured when apex job will be run because of asynchronuos functionality of batch apex job.

All Answers

Ashish DevAshish Dev
I suspect, this is due to different contexts.
VF page runs in the context of the user who is viewing the VF page, but batch class runs in global context, you won't be able to access variables set in batch class in the VF page.

Alternatively you can store values in custom object or custom setting and access that in VF page.

Let me know if this helps you.
Banwari kevat1Banwari kevat1
Hi Aakanksha,
    One more thing you keep in mind is that batch apex runs asynchronously and here you try to access just after apex job queued. Here we can not be sured when apex job will be run because of asynchronuos functionality of batch apex job.
This was selected as the best answer
Aakanksha Singh 11Aakanksha Singh 11
Hello All,

Thank you for replying!!

Is there any way to get data through batch apex in vf page??'

Please Respond.

Regards
Aakanksha Singh