+ Start a Discussion

passing values from batch apex controller to Visualforce page

Hi Guys,


I have written a batch apex controller for my vf page.

In execute method of batch apex controller, i have a string variable "sample" and it is defined as

public String sample {get; set;} in that class.


but when i pass this variable to my page, it gives me null value.

i have used system.debug to check its value in my class and its showing the required value inside execute method.


So please help me on how this value can be passed to page correctly.Thanks

Best Answer chosen by Admin (Salesforce Developers) 

Values from batch apex cant be passed back to page.It has to be stored in some object and then call back in page.

Need to keep a refresh button on Page to see the latest values.

All Answers


not sure how you are passing the values from your batch controller to your visual force page. Batch Apex and visual force are not designed to pass data back and forth.


A little bit of code on what you are attempting to do might be very helpful to help us provide some direction.


Hi anand,


I have written the vf page and batch apex controller  since no of records are >10K . PFA the code below. 
I have a execute button which actually starts the batch class.
Now the only problem is I need to pass the string Gmap_marker from  batch apex controller to VF page, this is not working (tried many diff ways)  .
I have checked using system.debug that the value Gmap_marker in execute method gives the expected result. 
Only problem I am not able to pass it  back to vf page.Please have a look and help me.Thanks . 

<apex:page controller="BatchApexMatrixBulk" showHeader="true" sidebar="false">
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA" type="text/javascript"></script>
    <script type="text/javascript">
        function displayMap(){
            var URL = 'http://maps.google.com/maps/api/staticmap?center=' + 'USA' +'&zoom=4&size=1500x1500&maptype=roadmap';
            var M=document.getElementById('j_id0:frmMain:pb2:txtHidMarker').value;
            URL=URL + M + '&sensor=false';
            document.getElementById('j_id0:frmMain:pb2:txtHid').value = URL;

  <apex:form id="frmMain">
  <apex:pageblock id="pb2">
      <apex:inputHidden id="txtHid" value="{!GMap_URL}"/>
      <apex:inputtext id="txtHidMarker" value="{!Gmap_marker}"/>
      <center><apex:commandButton action="{!navigate}" value="Execute"/></center><br/><br/>
      <table CELLPADDING="8">
      <td bgcolor="Silver"><b><center>Low</center></b></td>
      <td bgcolor="Silver"><b><center>High</center></b></td>
      <th rowspan="2">TAM</th>
      <td bgcolor="Silver"><b><center>High</center></b></td>
      <td bgcolor="#EA744E"><center>{!matrix1}</center></td>
      <td bgcolor="#6699ff"><center>{!matrix2}</center></td>
      <td bgcolor="Silver"><b><center>Low</center></b></td>
      <td bgcolor="#A85400"><center>{!matrix3}</center></td>
      <td bgcolor="#7EE34B"><center>{!matrix4}</center></td>

  <apex:pageBlock id="pb3">
      <img id = "img1" src="{!GMap_URL}" height="100%" width="100%" />
      <script type="text/javascript">
            function addLoadEvent(func)
                var oldonload = window.onload;
                if (typeof window.onload != 'function')
                {window.onload = func();}
                        window.onload = function()
                        {if (oldonload) {oldonload();}}


global class BatchApexMatrixBulk implements Database.Batchable<SObject>,Database.AllowsCallouts,Database.Stateful
  public String GMap_URL {get; set;}
  global String Gmap_marker {get; set;}
  public List<String> lstX0Y0= new List<String>();
  public List<String> lstX0Y1= new List<String>();
  public List<String> lstX1Y0= new List<String>();
  public List<String> lstX1Y1= new List<String>();
  public list<string> getmatrix1(){return lstX0Y0;}
  public list<string> getmatrix2(){return lstX1Y0;}
  public list<string> getmatrix3(){return lstX0Y1;}
  public list<string> getmatrix4(){return lstX1Y1;}
  public void navigate()

  global database.Querylocator start(Database.batchableContext bc)
   return Database.getQueryLocator([select id,Total_Sales__c,HP_SoW__c,MSA__c from Account]);

 global void execute(Database.BatchableContext BC, List<SObject> scope)
  Configuration_Parameter__c conpar=[select Plot_MSA_TAM__c,Plot_SoW__c from Configuration_Parameter__c limit 1];
  Map<String, List<Account>> mapMSAAcc = new Map<String, List<Account>>();

  for(sObject s : scope){
        Account A = (Account)s;
        if(mapMSAAcc.get(A.MSA__c) == null){
            mapMSAAcc.put(A.MSA__c, new List<Account>());
  if(mapMSAAcc.size() > 0){
      for(String MSA : mapMSAAcc.keySet()){
          String mapcolor= '';        
          Double totTam=0;
          Double avgSoW=0;
          List<Account> msaaccs=[select id,Total_Sales__c,HP_SoW__c,MSA__c from Account where MSA__c=:MSA];
          for(Account A : msaaccs){
                   totTam = totTam + A.Total_Sales__c;
                   avgSoW = avgSoW + A.HP_SoW__c;
          avgSoW = avgSoW / msaaccs.size();
          if(totTam>conpar.Plot_MSA_TAM__c && avgSoW <= conpar.Plot_SoW__c)
          else if(totTam>conpar.Plot_MSA_TAM__c && avgSoW > conpar.Plot_SoW__c)
          else if(totTam<=conpar.Plot_MSA_TAM__c && avgSoW <= conpar.Plot_SoW__c)
          else if(totTam<=conpar.Plot_MSA_TAM__c && avgSoW > conpar.Plot_SoW__c)
          for( string s: lstX1Y1)
          { if(s.compareto(MSA)==0){ mapcolor = 'green' ; }}
          for (string t: lstX0Y1)
          { if(t.compareto(MSA)==0){ mapcolor = 'brown' ; }}
           for (string u: lstX1Y0)
          { if(u.compareto(MSA)==0){ mapcolor = 'blue' ; }}
           for (string v: lstX0Y0)
          { if(v.compareto(MSA)==0){ mapcolor = 'red' ; }}
          Gmap_marker = Gmap_marker +'&markers=color:'+ mapcolor+ '|' +MSA ;

 global void finish(Database.BatchableContext BC)

 webservice static void BatchApexMethod()
  BatchApexMatrixBulk accBulk = new BatchApexMatrixBulk();

You cannot pass data from a Apex batch process back to Visual force. Apex Batch is asynchronous and you cannot tie that back to your visual force page. 




Values from batch apex cant be passed back to page.It has to be stored in some object and then call back in page.

Need to keep a refresh button on Page to see the latest values.

This was selected as the best answer

I  want to pass integer value from apex batch class to vf page class.u told that store in sobject how it is possible and y cant we pass integer value directly to the class, is it limitation for apex batch?