11

I'll be working on creating a apex schedule class and I just need a pointer to start is that possible if you can have a working small sample where I can understand how the mechanics work and I have googled but not find what exactly I want and also I looked at the salesforce site

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

  • 1
    Do you have any specific questions regarding the information at the link you provided? That page includes a working example along with test code to cover it. – JCD Jun 17 '15 at 13:47
  • I beleive you have to write a class which is "schedulable" (as noted in the docs). Basically, this schedulable class will create an instance of the method you want to run, usually with no input parameters. For example, your method can query the top 5 users that logged in this month and email the CEO. When the scheduled time occurs, the "schedulable" class will execute your method (running the SOQL Query and Email Notification. – James Jun 17 '15 at 13:52
13

Would this help? Then you'll have to set the schedule in your apex classes setup

    global class Miko_Scheduled Implements Schedulable
    {
        global void execute(SchedulableContext sc)
        {
            setToMiko();
        }

        public void setToMiko()
        {
            List<Account> listAccounts = new List<Account>();
            listAccounts = [SELECT ID,Tenant_Name__C FROM Account WHERE Tenant_Name__C = null];

            for(Account acc : listAccounts)
            {
                acc.Tenant_Name__C = 'Miko';
            }

            update listAccounts;
        }
    }

afterwards you can go to Setup > Build > Develop > Apex Classes and then press on the 'Schedule Apex' to set up your class.

Added:

It's helpful to add a few functions like the following to schedulable classes:

public static String schedmon5am = '0 00 05 ? * 2';  //Every Monday, 5AM
// used for scheduling a repeating job - currently set for Monday 5AM.
global static String scheduleMeMon5AM() {
    AutoChatterSiteManagerCheck SC = new AutoChatterSiteManagerCheck(); 
    return System.schedule('ScheduleClassNameStr - Monday 5AM', schedmon5am, SC);
}

That way, you can schedule the class from the "execute anonymous" window with the following:

YourTestClass.scheduleMeMon5AM();

without having to look up how scheduling strings work.

  • How would you call the Miko_Scheduled class to pick the apex schedule? – Nick Kahn Jun 17 '15 at 14:19
  • 2
    What do you mean? Set Miko_Scheduled as an apex scheduled class? You can go to Setup > Build > Develop > Apex Classes and then press on the Schedule Apex button. – dotmiko Jun 17 '15 at 17:46
  • 1+ ah ha i see that now.. let me work on that – Nick Kahn Jun 17 '15 at 17:49
  • sounds good! keep me posted if it works so i can help you out. mark it as an answer afterwards so you can help others too :) – dotmiko Jun 17 '15 at 17:50
  • 1
    Will do that, if you can edit your answer and add the last step you have shown like You can go to Setup > Build > Develop > Apex Classes and then press on the Schedule Apex button will help others too. – Nick Kahn Jun 17 '15 at 17:52
5

You can take help from the below example :

Batch class :

global class BatchCreateOpportunity implements Database.batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext bc){
        String query = 'SELECT id from Account limit 1';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
        List<Opportunity> oppList = new  List<Opportunity>();
         for(Account acc : scope )
     { 

         for(integer i=0;i<5;i++){
             Opportunity opp = new Opportunity();
             opp.accountId = acc.id;
             opp.name = 'version'+i;
             opp.CloseDate = system.today()+10;
             opp.StageName = 'prospecting';
              oppList.add(opp);
         } 
    }   
    insert oppList;
}
 global void finish(Database.BatchableContext BC)
    {
    }
}

Schedular class :

global class schduleBatchCreateOpportunity implements Schedulable {

   global void execute(SchedulableContext ctx) {
      BatchCreateOpportunity p = new BatchCreateOpportunity();
        database.executeBatch(p);
   }   
}

Let me know if it helps.

5

Schedule apex class for every 15 mins - Complete coding

Let's consider following class to be scheduled for every 15mins.

public with sharing class contactcreate {

     public void createcontact(){

       Contact conobj=new Contact();
       conobj.FirstName='Raj';
       conobj.LastName='K';
       insert conobj;
     }
}

Scheduler class:

global class scheduledCron implements Schedulable{
global void execute(SchedulableContext SC) {
//Non static method cannot be referenced from a static context
        contactcreate abc = new contactcreate();
        abc.createcontact();
    }
}

Go to Developer Console: Debug->Open Execute Anonymous Window

Add following code

System.schedule('Scheduled Job 1', '0 0 * * * ?', new scheduledCron ());
System.schedule('Scheduled Job 2', '0 15 * * * ?', new scheduledCron ());
System.schedule('Scheduled Job 3', '0 30 * * * ?', new scheduledCron ());
System.schedule('Scheduled Job 4', '0 45 * * * ?', new scheduledCron ());

You can check scheduler under Setup->Scheduled Jobs

protected by Community Aug 19 '17 at 9:15

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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