+ Start a Discussion
Chidanand Magadum 24Chidanand Magadum 24 

How to create a Scheculed job

HI all,

This is my code
Date d = Date.Today();
 List<Account> AllAccounts= new List<Account>([select id,Name,Relationship_Start_Date__c,ARR__c,(Select Name from Opportunities) from Account ]);
 Map<String,Set<String>> MapAccwithOpps =new Map<String,Set<String>>();
 for(Account a:AllAccounts)
 {
 Set<String> Oppnames = new Set<String>();
    //string Oppnames='';
    for(Opportunity opp:a.Opportunities)
        {
            Oppnames.add(opp.Name);
        }
    MapAccwithOpps.put(a.Name,Oppnames);
    }
    system.debug('MapAccwithOpps==='+MapAccwithOpps);
 for(Account a:AllAccounts)
 {
    Integer numberDaysDue= a.Relationship_Start_Date__c.daysBetween(d);
 if(numberDaysDue<=60)
 {
    Opportunity O= new Opportunity();
    o.Name=a.Name;
    o.StageName='Legal';
    o.Amount=a.ARR__c;
    o.AccountId=a.ID;
    o.CloseDate=Date.today();
    o.NextStep='Won';
    if(!(MapAccwithOpps.get(a.Name).contains(o.Name)))
        insert o;
    }
 system.debug('Diff==='+numberDaysDue);
 
 }


How to create a scheduled job for this. I want to run this code every day Automatically.
Yogesh KulkarniYogesh Kulkarni
Check these URLs

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm
https://help.salesforce.com/HTViewHelpDoc?id=code_schedule_batch_apex.htm&language=en_US

 
Arunkumar RArunkumar R
Hi, 

First you need to need created a schedulable class,
global class ScheduleLogic implements Schedulable {
   global void execute(SchedulableContext SC) {
      
// add your code logic.
       
   }
}

Then you can schedule a job using developer console or you can click schedule Apex button after schedule class is save and give your prefered time and date.

Go to developer console --> Press Ctrl + E, then paste the below code, click execute
 
ScheduleLogic m = new ScheduleLogic();
String sch = '0 0 13 * * ?';
String jobID = system.schedule('Merge Job', sch, m);

The above schedule logic will run everyday at 1PM, change 13 if you want to schedule in different time.