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
rajesh k 10rajesh k 10 

Urgent:How to run a batch between Two dates below batch class?

Hi ,
           I created one batch class like below .I have one object like Renual__c,This object consist of two fields startdate__c and Enddate__c.How to run my batchclass between these two dates?

global class ExpiryDateBatchCls implements Database.Batchable<sObject>
{
    String query;
   
   
    global Database.querylocator start(Database.BatchableContext BC)
    {
        query = 'Select id,Expire_Date__c from Sales_Order__c';
       
        System.debug('***query****'+query);
       
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Sales_Order__c> scope)
{
  
    Date tempdate = system.today() + 90;
      list<Opportunity> OppList = new list<Opportunity>();
     
       for( Sales_Order__c ob1 : scope){
        if(ob1.Expire_Date__c == tempdate)
        {


            Opportunity op = new Opportunity();
             op.Name = op.Name+'Renual';
             op.ForecastCategoryName = 'Pipeline' ;
                op.CloseDate = System.today() + 15 ;
               op.StageName = 'Prospecting';
               OppList.add(op);
        }
     }    
   
            
      if(OppList.size()>0){
            insert OppList;
        }

}     

   global void finish(Database.BatchableContext BC)
{

}
}

please help me.......
Best Answer chosen by rajesh k 10
Denis VakulishinDenis Vakulishin
If you need one execution of job so just use StartDate
The string parameter(sch in my code sample) composed of 
Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
So if StartDate = 06/08/2014 string will be "0 0 0 06 08 ? 2014"
Don't forget to unschedule job if you wan't it to be executed only once
Here's the link of how you can do it http://salesforce.stackexchange.com/questions/24436/is-there-any-way-to-unschedule-an-apex-scheduled-job-programmatically

All Answers

AshlekhAshlekh
Hi,

I can't understand your proper requirment and If you want a batch should run at some speicific time than you can schedule a batch.

Schedule a batch means use scheduler class to schedule your batch at specific time.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

http://blog.shivanathd.com/2013/01/run-batch-class-hourly.html

IF it helps you than please mark it as a solution and ENJOY APEX
rajesh k 10rajesh k 10
Hi,
       I want above batch run between based on the startdate__c and enddate__c of Renewal__c object.Renual__c object and sales_Order__c both no relationships.

How to run above batch between startdate__C and Enddate__c?

please help me............
Denis VakulishinDenis Vakulishin
YOu should implement shceduler class. 
This is info from docs givven above by Ashlekh
proschedule p = new proschedule();
        String sch = '0 0 8 13 2 ?';
        system.schedule('One Time Pro', sch, p);
Expression Description
0 0 13 * * ? Class runs every day at 1 PM.
0 0 22 ? * 6L Class runs the last Friday of every month at 10 PM.
0 0 10 ? * MON-FRI Class runs Monday through Friday at 10 AM.
0 0 20 * * ? 2010 Class runs every day at 8 PM during the year 2

proschedule - class implemented "Schedulable"
rajesh k 10rajesh k 10
Hi Denis Vakulishin,
                               I will enter my dates In my object(Renual__c) fields like startdate and enddate (both fields are datetime fields).Based on these dates how to run above Batch class?

help me......
Denis VakulishinDenis Vakulishin
If you need one execution of job so just use StartDate
The string parameter(sch in my code sample) composed of 
Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
So if StartDate = 06/08/2014 string will be "0 0 0 06 08 ? 2014"
Don't forget to unschedule job if you wan't it to be executed only once
Here's the link of how you can do it http://salesforce.stackexchange.com/questions/24436/is-there-any-way-to-unschedule-an-apex-scheduled-job-programmatically
This was selected as the best answer
rajesh k 10rajesh k 10
Hi sir,
         I will enter any dates in startdate and enddate(these dates not fixed .Every time i will goto renewal__c object i will give startdate and enddate).How to run my batch class between these dates.

help me...
Denis VakulishinDenis Vakulishin
How often you want it to run between theese dates?
rajesh k 10rajesh k 10
Hi,
          Every time with in the renual Object record creation time based on the user will enter startdate and enddate .Between these two dates above batch class how will run?
rajesh k 10rajesh k 10
Hi sir,
               want  schedule batch from visualforce page?

Like below link how?
https://www.cloudclickware.com/jobbook/
Denis VakulishinDenis Vakulishin
HI,
As I see in the link you've provided there's only one date - "preferred start". So you have only one date and you shuold add Shceduled job as I described in my first response on this thread via system.schedule() method
rajesh k 10rajesh k 10
Hi sir,
This is my requirement

How to run a batch class between Two dates based on frequency(scheduled time) Dynamically from visualforce page?
In my visualforce page 4 fields
1)Job name
2)Startdate(datetime field type)
3)Enddate(datetime field type)
4)Frequency(datetime field type)

My requirement is how to run batch between Startdate and enddate based on Frequency

Dynamically from visualforce page?
Denis VakulishinDenis Vakulishin
I think there're 2 options: 
  1. Create another 1 scheduled batch job which checks StardDate fields and create new jobs with needed frequency.
  2. After creating object you create scheduled job from startDate field. After job is done you just delete current and create new based on needed frequency. 
But anyway you should delete jobs: in another batch as in 1st oprtion or in the end of created scheduled job. 
Here's the some info from the web http://penrodcrm.com/blog/how-create-system-job-cron-job-forcecom-salesforce
rajesh k 10rajesh k 10
Hi,
        Here What is Cronid__c
Denis VakulishinDenis Vakulishin
Hi,
This is ID of created job. After you scheduled you job you have CronId.
Id cronId = System.schedule(/*params goes here*/);


Denis VakulishinDenis Vakulishin
In the article this is Custom field on Vendor__c custom object
rajesh k 10rajesh k 10
Hi sir ,
            This is my code please help me 1 week to i wa try to this only

page:
..........

<apex:page standardController="Renewal__c" extensions="RSCls" sidebar="false">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<Script>
function resetForm(ele)
{
$(ele).closest('form').find("input[type=text], textarea").val("");
}

</script>
<apex:form >
<apex:sectionHeader title="Renewal"/>
    <apex:pageBlock >
    <apex:pageBlockButtons location="bottom">
    <apex:commandButton value="Renewal" action="{!dosave}"/>
<!--    <apex:commandButton value="Save & New" action="{!doNewSave}"/>-->
<!--   <apex:commandButton value="Cancel"/> -->
    <apex:commandButton value="reset" onclick="return resetForm(this);"/> 
   
    </apex:pageBlockButtons>
    <apeX:pageBlockSection columns="1">
    <apex:inputField value="{!Renewal__c.Name}"/>
    <apex:inputField value="{!Renewal__c.Start_Date__c}"/>
    <apex:inputField value="{!Renewal__c.End_date__c}"/> 
    <apex:inputField value="{!Renewal__c.Frequency__c}"/>
    </apeX:pageBlockSection>
 
    </apex:pageBlock>
</apex:form>
 
</apex:page>

Controller:
...................
public class RSCls {

    public Renewal__c renewal;
    public RSCls(ApexPages.StandardController controller) {
    this.renewal=(Renewal__c)controller.getRecord();
   

   
    Datetime Sdate1=renewal.Start_date__c;
   
    Datetime Edate1=renewal.End_date__c;
   
    Datetime Fdate=renewal.Frequency__c;
   
    String Rname=renewal.name;
    }
    public pageReference doNewSave()
    {
    doSave();
    pageReference ref=new pageReference('/apex/jobschedular');
   
    return ref.setRedirect(true);
   
    }
    public void doSave()
    {
  // callRenewal.getX();
   

    Datetime Sdate1=renewal.Start_date__c;
        integer d = Sdate1.day();
        integer m = Sdate1.month();
        integer h = Sdate1.hour();
        integer s = Sdate1.second();
        integer min = Sdate1.minute();
        integer yr = Sdate1.year();
          ExpiryDateBatchClsSchedular addmerge = new ExpiryDateBatchClsSchedular();
    
      String sch = string.valueof(s) + ' ' + string.valueof(min) + ' ' + string.valueof(h) + ' ' + string.valueof(d) + ' ' + string.valueof(m) + ' ' + '?' + ' ' + string.valueof(yr);
         
   
    String Rname=renewal.name;
   
    Id cronId=System.schedule(Rname,sch,new ExpiryDateBatchClsSchedular());//'1 0 * * * ?'
   
    Renewal__c[] ren=[SELECT name,Start_date__c,End_date__c,Frequency__C FROM Bisoftsols__Renewal__C];

   
     CronTrigger ct=[SELECT StartTime,EndTime,NextFireTime,TimesTriggered,State FROM CronTrigger];
   
   
        DateTime starttime=ct.StartTime;
       


        DateTime Endtime=ct.EndTime;


        DateTime NextFireTime=ct.NextFireTime;


       
        Integer TimesTriggered=ct.TimesTriggered;


            for(Integer i=0;i<ren.size();i++)
            {
             ren[i].start_date__C=starttime;
            
       
             ren[i].End_date__C=Endtime;
            
          
             ren[i].Frequency__c=NextFireTime;
            
           
             update ren;  
         
            }
           
        /*     List<CronTrigger> cronTriggersReadyToFireAgain = [SELECT Id FROM CronTrigger WHERE NextFireTime != null]; //create a list of CronTrigger records that are scheduled to fire again
          
           System.debug('******cronTriggersReadyToFireAgain********'+cronTriggersReadyToFireAgain);
          
            if (!cronTriggersReadyToFireAgain.isEmpty()) { //if the list is not empty
           
          
           
                for (CronTrigger t : cronTriggersReadyToFireAgain) { //for each record
               
                 System.debug('******t******'+t);
                    System.abortJob(t.Id); //abort the job
                   
                    // System.debug('****** System.abortJob(t.Id)******'+System.abortJob(t.Id));
                }
            }*/
               


    }

}

please help me..............