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
iSqFt_ADiSqFt_AD 

Need to schedule apex class to fire trigger at 12:00AM EST every day

Context: Our sales users have a finite amount of time to work an Account before it can be pulled from their ownership and reassigned. I currently have a formula field that identifies if an Account can be reassigned by populating "YES" or "NO".

 

Desired Outcome: Run a trigger at midnight everyday that takes all Accounts where the custom forumla field Up_for_Reassignment__c says "YES" and reassigns them to the user's manager.

 

I know this involves a fairly simple trigger, but then it also requires an Apex Class that uses the Scheduler. I am not well versed in Apex Coding. Does someone have a standard Apex class built with the scheduler in it that I can insert my trigger into?

 

Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
TheIntegratorTheIntegrator
Try this code, I haven't tested it, but I think it will work.
global class AccountCronJob implements Schedulable{
    global void execute(SchedulableContext SC) {
     List <Account> acc = [Select OwnerId, Owner.ManagerId From Account Where Up_for_Reassignment__c='Yes'];         

    for(Account a: acc){
        if(a.Owner.ManagerId != null)
            a.OwnerId = a.Owner.ManagerId;
    }
    update(acc);
} 

 

All Answers

TheIntegratorTheIntegrator
Try this code, I haven't tested it, but I think it will work.
global class AccountCronJob implements Schedulable{
    global void execute(SchedulableContext SC) {
     List <Account> acc = [Select OwnerId, Owner.ManagerId From Account Where Up_for_Reassignment__c='Yes'];         

    for(Account a: acc){
        if(a.Owner.ManagerId != null)
            a.OwnerId = a.Owner.ManagerId;
    }
    update(acc);
} 

 

This was selected as the best answer
iSqFt_ADiSqFt_AD

Hey, Thanks!

 

EDIT: Disregard previous message I just deleted... I figured out my issue.

 

Now that I have this new Apex Class created and saved, how do I actually go about scheduling it to run?

 

TheIntegratorTheIntegrator

Setup -> App Setup  ->Develop -> Apex Classes

 

Click on the Schedule Apex button, give it a name, choose this Apex Class, check Weekly and select all days, if you want to schedule daily. Select the start and end date, the time of execution and save.

iSqFt_ADiSqFt_AD

Top notch man. I appreciate your help.