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
AngelikaAngelika 

How to create a custom object with apex for apex scheduler?

I am new to apex and am trying to build an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

I'm almost done with the program but I keep getting this error:

Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, Date at line 9 column 9 where the if statement is

 

Here is my code:

global class AccountReviewScheduler implements Schedulable
{
    global void execute (SchedulableContext ctx) 
    {
        sendEmail();
    }
public void sendEmail()
{
    if (Account__r.Next_Account_Review_Date__c == System.today().addDays(14))
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        Mail.setTemplateId('00XF0000000LfE1');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
    }
}
  
}

 

I did some research and a commenter on another discussion board told me that it's not working because Account__r.Next_Account_Review_Date__c is just a field.

I have to use it with a record to make it have a value. Then I can compare that value with system.today().

I'm not sure what that means....create a custom object? I'm not sure how to do this. Here is my revised code I'm having trouble with. Here is my revised code:

Here is the revised code(asterisks ** denote change. Not included in actual code.)

global class AccountReviewScheduler implements Schedulable 
{ 
    global void execute (SchedulableContext ctx)  
    { 
        sendEmail(); 
    } 
public void sendEmail() 
{ 
    **CustomObj__c co = [select Account__r.Next_Account_Review_Date__c from CutomObj__c limit 1];** 
    **if(co.Account__r.Next_Account_Review_Date__c == system.today().addDays(14))**  
    { 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
        Mail.setTemplateId('00XF0000000LfE1'); 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail }); 
    } 
} 
 
} 

 

Changes are denoted by 2 asterisks ** Not included in real code...I know the custom obj code might be wrong...I got it off someone else and I'm not sure what to do.

NOTE: Time based workflows will not work here because this email has to occur every year (for many years) 14 days before the account review date. Time based workflows will work ONCE after they are set to trigger one year, but do not repeat. They do not recur by design. See: http://success.salesforce.com/questionDetail?qId=a1X30000000JnPMEA0

I have read the scheduling documentation: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

and gone over the Apex workbook tutorial:http://blogs.developerforce.com/developer-relations/2010/02/spring-10-saw-the-general-availability-of-one-of-my-favorite-new-features-of-the-platform-the-apex-schedulerwith-the-apex-s.html as well as tutorial 14 in the official Apex guide but I still am having trouble.

MY REAL QUESTION IS: How do you declare/create a custom object in Apex? Do you think this is what is wrong with my code?

Any help/insight into my code/links to new resources would be greatly appreciated.

Thank you!