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
Akshita SinghAkshita Singh 

Can anyone help suggesting how to make a VisualForce Page to show all the scheduled batches? Preferably using CronTrigger.

Best Answer chosen by Akshita Singh
Khan AnasKhan Anas (Salesforce Developers) 
Hi Akshita,

Greetings to you!

You can query from CronTrigger object: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_crontrigger.htm

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="CronTriggerListC">
    <apex:form>
    	<apex:pageBlock>
        	<apex:pageBlockTable value="{!cronList}" var="c">
            	<apex:column value="{!c.Id}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class CronTriggerListC {

    public List<CronTrigger> cronList{get;set;}
    public CronTriggerListC(){
        cronList = [SELECT Id FROM CronTrigger];
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Akshita,

Greetings to you!

You can query from CronTrigger object: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_crontrigger.htm

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="CronTriggerListC">
    <apex:form>
    	<apex:pageBlock>
        	<apex:pageBlockTable value="{!cronList}" var="c">
            	<apex:column value="{!c.Id}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class CronTriggerListC {

    public List<CronTrigger> cronList{get;set;}
    public CronTriggerListC(){
        cronList = [SELECT Id FROM CronTrigger];
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Akshita,

//You can get it from CronTrigger object query. Say for example: 

[SELECT Id,CronJobDetail.Name,CronJobDetail.Id, State FROM CronTrigger where CronJobDetail.Name =:<Add filter here> AND State !='COMPLETE']

//This will give you the scheduled jobs if any:

See : https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_crontrigger.htm

//You can also check jobs in batch finish method :

AsyncApexJob apexJob = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email
          FROM AsyncApexJob WHERE Id = :BC.getJobId()];

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com