You need to sign in to do that
Don't have an account?
URGENT: Help to display error message in VF Page if no records are present in Execute Method
I have a VF page where a button is present.On click of it a batchjob runs and deletes the records from custom object.
So my design was I have controller 'batchctrl' for the VF Page which will call the execute method of the batch class.
Here is the execute method in my batchClass:
public List<obj__c> objList= new List<obj__c>();
global void execute(Database.BatchableContext BC,List<sObject> scope){
for(sObject s:scope){
obj__c x= (obj__c)s;
objList.add(x);
}
delete objList;
}
Finish Method:
global void finish (Database.BatchableContext BC){
AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email, ExtendedStatus
From AsyncApexJob where Id =:BC.getJobId()];
if(a.status=='Completed'){
ObjRecordsSize = objList.size();
batchctrl b= new batchctrl();
b.updateErrorMsg(ObjRecordsSize);
}
}
In the finish method i'm trying to call updateErrorMsg method of my batchctrl class to send the recordsize value and if it is zero i want to display an error message.
But i'm getting the below exception
System.FinalException: ApexPages.addMessage can only be called from a Visualforce page
So please let me know if they is any way I could display the error message in the VF Page.
I'm sure that there must be someway :)
Many thanks in advance :)
Hi,
You can setup a JavaScript function that calls a method to check the number of batches processed after the batch is completed with an interval of 1 second.
Example:
Visualforce Page:
Apex Controller:
Hope ths helps!