You need to sign in to do that
Don't have an account?
vijaya kudupudi
how to schedule a batch class which is getting values from vf page
Hi all,
I got a requirement like delete records based on criteria( to give the criteria i should take values from vf page), and I have to delete the records using batch class. And want to run this batch class once per month. How can i schedule this batch class. and i want get records list from batch class to controller class. Please anybody can help me.
I got a requirement like delete records based on criteria( to give the criteria i should take values from vf page), and I have to delete the records using batch class. And want to run this batch class once per month. How can i schedule this batch class. and i want get records list from batch class to controller class. Please anybody can help me.
//Controller class public with sharing class DeleteSelectedRecords{ public List<Sobject> reclist {get;set;} public List<SelectOption> options{get;set;} public String Recname{get;set;} public String selectedOpt{get;set;} public boolean criteriaRend{get;set;} public DeleteSelectedRecords(){ reclist = new List<Sobject>(); options = new List<SelectOption>(); Map<String,Schema.sobjectType> objNames = Schema.getglobalDescribe(); options.add(new SelectOption('--None--','--None--')); for(String s: objNames.keyset()) { if(objNames.get(s).getDescribe().isAccessible()==True && objNames.get(s).getDescribe().isUpdateable()==True){ options.add(new SelectOption(objNames.get(s).getDescribe().getLabel(),s)); } } } public void showpb(){ if(selectedOpt!='--None--'){ criteriaRend= true; } } public void getRecords(){ //reclist = new List<Sobject>(); String query = 'select id, name from '+selectedOpt+' where name='+'\''+Recname+'\''; deleteSubscribers ds = new deleteSubscribers(query); Id batchpid = Database.executeBatch(ds); System.debug('batchpid @@@'+batchpid ); System.debug('ds.accountMap @@@'+ds.accountMap ); reclist =ds.listrecord; System.debug('reclist @@@'+reclist ); } }
//Batch class global with sharing class deleteSubscribers implements Database.Batchable<sObject>,Database.Stateful{ global final String Query; global List<Sobject> listrecord{get;set;} global Map<Id, sObject> accountmap; global deleteSubscribers(String q){ accountmap = new Map<Id, Account> (); listrecord = new List<Sobject>(); System.debug('query @@@'+q); Query=q; } global Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC,List<Sobject> scope){ // listrecord = new List<Sobject>(); listrecord = scope; System.debug('BatchableContext@@@'+BC); System.debug('sCOPE listrecord @@@'+listrecord ); for(Sobject s:scope){ accountmap.put(s.id,s); } System.debug('accountmap @@@'+accountmap); delete scope; System.debug('accountmap @@@'+accountmap); } global void finish(Database.BatchableContext BC){} }
//VF page <apex:page controller="DeleteSelectedRecords"> <apex:pagemessages ></apex:pagemessages> <apex:form > <apex:pageblock > <apex:pageBlockSection Title="Select Object" > <apex:selectList size="1" value="{!selectedOpt}" label="Select the object"> <apex:selectOptions value="{!options}" /> <apex:actionSupport event="onchange" rerender="crsh" action="{!showpb}"/> </apex:selectList> </apex:pageBlockSection> <apex:pageBlockSection Title="Enter criteria" id="crsh"> Enter criteria to delete records! <apex:inputText label="Record name is equalto" value="{!recName}" rendered="{!criteriaRend}" /> <center> <apex:commandButton action="{!getRecords}" value="Get Records" rendered="{!criteriaRend}"/></center> </apex:pageBlockSection> <apex:pageblocksection title="Selected Records to be Deleted" rendered="{!criteriaRend}" columns="1"> <apex:pageBlockTable value="{!reclist}" var="rec" rendered="{!if(reclist.size==0,false,true)}"> <apex:column headerValue="Id"><apex:outputText value="{!rec.id}"/></apex:column> </apex:pageBlockTable> </apex:pageblocksection> </apex:pageblock> </apex:form> </apex: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,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.
All Answers
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,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.
Thank you for your response.It is working.