0

I'm trying to write a scheduled class that ticks a check box if it's unchecked, and if it's checked it un ticks the box. What is it that I'm doing wrong with my code? Thanks in advance, I am a complete moron at coding!

global class birthdayScheduledApex implements Schedulable {
    global void execute (schedulableContext SC) {
        if (Send_Birthday_Email__c = False) {
            Send_Birthday_Email__c = 'True';
        } else if (Send_Birthday_Email__c = True) {
            Send_Birthday_Email__c = 'False';
        }
    }
}
1
  • You're setting it to false instead of checking to see if it's false. Use '==' instead of '=' in your if statements. I.e. if(Send_Birthday_Email__c == False){} May 20 '16 at 22:34
8

You are trying to create simple Semaphore.

  • For the first you should select records, which you wants to update.
  • In for loop make update.
  • Save Records.

Let's assume that you have field Send_Birthday_Email__c on contact object, then simple Schedulable class could look like:

global class birthdayScheduledApex implements Schedulable {
    global void execute (schedulableContext SC) {

        list<contact> contacts = [select name, Send_Birthday_Email__c from contact];

        for (contact con :contacts) {
            con.Send_Birthday_Email__c = !con.Send_Birthday_Email__c;
        }

        update contacts;
    }
}

You can test within console:

birthdayScheduledApex b= new birthdayScheduledApex();
String cronStr = '0 10 * * * ?';
System.schedule('Process Accs Job', cronStr, b);

Where 0 10 * * * ? presents every ten minutes in hour.

3
  • 1
    Upvoted. However, it is best practice for readability to add curly braces regardless of whether the content of the loop is a one-liner or not. I would amend my sample to provide the OP with a better answer. May 20 '16 at 22:31
  • 3
    @GlennDaly (1) N.B. if > 10000 Contacts selected, OP will blow up dml limits; this should be a scheduled batchable or some other way of limiting updates; (2) globals not required unless managed package; public suffices
    – cropredy
    May 20 '16 at 23:41
  • 1
    Thanks very much for your help all, note to self, don't try coding after a couple of beers!
    – Glenn Daly
    May 21 '16 at 9:37

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.