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 

Simple Apex scheduler : Please help!

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

 

What does this error mean? How can I trouble shoot it?

 

Here is the code: (Line 9 is where the if statement begins)

 

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 });
    }
}
  
}

 

Jerun JoseJerun Jose
Account__r.Next_Account_Review_Date__c is just a field.

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

You need to do something like
CustomObj__c co = [select Account__r.Next_Account_Review_Date__c from CutomObj__c limit 1];
then you can use the Account__r.Next_Account_Review_Date__c value for the data returned by the query by using it as
co.Account__r.Next_Account_Review_Date__c

In your if block on line 9, you can then use

if(co.Account__r.Next_Account_Review_Date__c == system.today().addDays(14)) to check that query return value with the date two weeks from now.
AngelikaAngelika

Hi Jerun,

 

Thanks for your help, I have a question about the code you wrote for me.

 

CustomObj__c co = [select Account_r.Next_Account_Review_Date__c limit 1]

 

Does this piece of code declare/create a new custom object? Is that what I need to do to make this work? I'm confused?

 

Thanks for your help!

Jerun JoseJerun Jose
Sorry for the late response. The email note got buried somewhere.

The snippet you pointed out does not create a new object. Well .... the term object is a bit overused in Apex , it relates to a number of things.

When you do
CustomObj__c co = [select Account_r.Next_Account_Review_Date__c from someObject limit 1]
co is a variable of type CustomObj__c
when we use the query [select Account_r.Next_Account_Review_Date__c from someObject limit 1] the database returns an object(as in Object oriented programming concept - the instance of a class) which has the data queried.

when we do
co = [select ......]
all we are doing is to assign this value that the DB returns to a variable in our apex code, so that we can use it later on.

so now you can access the values returned by the DB using the variable co like
co.ID