8

I'm a very much new to apex coding and I'm stuck with this problem kindly help to come out of this problem.

Scenario : I have a one custom object called child__c and i have a custom field by name child_stage__c which is a picklist (picklist vales are 25%, 50%, 75% and 100%), so my requirement was to update this child_stage__c field to 100% for all the record whose stage is 25%, it should update on April 1st of every year.

Here is my Batch apex code which will which will do that but i'm finding it difficult to schedule it to run on April 1st of every year


global class batchAUpdate_based_on_stage implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Name,child_stage__c FROM Child__c ';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Child__c> scope)
    {
        for (child__c child : scope){

         if(child.child_stage__c == '25%'){
             child.child_stage__c = '100%';
         }
          update child;

        }
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}

1
  • 2
    Welcome to the Salesforce Stack Exchange. I edited your post to include all of your code in a code format block. The toolbar above your edit box can be used to format. The '{}' can be applied to highlighted text to format it as code. – Peter Knolle Jan 17 '14 at 2:27
10

Take a look at the CronTrigger options using the System.Schedule method (halfway down the page):

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

Cron Options

Based on this, you can see that you are able to choose both the month and day of the month. You'll need to either run this Apex snippet via Anonymous Apex (the GUI for scheduling Jobs is much more restrictive).

system.schedule('April 1 Job', '0 0 0 1 4 ?, new MyJob());

In addition, if you read through that document, you'll find that the class that you schedule to invoke your job needs to implement the Schedulable class.

3
  • global class Child_stage_update_sheduler implements Schedulable{ global void execute (SchedulableContext SC){ batchAUpdate_based_on_stage b1 = new batchAUpdate_based_on_stage(); database.executeBatch(b1); } } can u just tell me where i have to update in this schedulable interface class. – Sharat Jan 17 '14 at 8:07
  • @user5017 I noticed you posted a separate question about the Schedulable implementation. Did this response answer the question you had here about scheduling? – Michael Welburn Jan 17 '14 at 14:15
  • no it din't so i posted another question – Sharat Jan 17 '14 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.