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
chubsubchubsub 

Schedule Apex Class - Need help

I'm trying to run a scheduled class and I need help with building the list of records that I need to update.  Below is my class, which will be referenced by a scheduled class: 

 

I'm getting an error : Save error: Initial term of field expression must be a concrete SObject: LIST<Guest_Card__c>

 

global class GuestCardsCheck {


/*
Purpose: - On a nightly basis, any Guest Cards that meet certain conditions will be
automatically changed to "Inactive" status. A scheduled class will call this
class method to perform this check.

*/

public static String checkGCQuery = 'select Id from Guest_Card__c where Inactive_Reason__c = "Independent Lead Source" AND ((Move_In_Date__c != Null AND LastModifiedDate >= Today.addDays(-30)) OR(Move_In_Date__c = Null AND LastModifiedDate >= Today.addDays(-60)))';

public static void checkGuestCards ()
{

//collect a list of records that need to be inactivated

List <Guest_Card__c> InactiveGCs = Database.query(checkGCQuery);

InactiveGCs.Status__c = 'Inactive';

update InactiveGCs;


}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
dmchengdmcheng

Also Today.addDays(-30) is incorrect - TODAY is a literal and does not have any methods.

 

You cannot use :date.today() either.  You will have to set a variable to the values you need and then include the variables in the where condition.

All Answers

dmchengdmcheng

The line InactiveGCs.Status__c = 'Inactive'; is incorrect.  You need to loop through the List and set the field value for record.

dmchengdmcheng

Also Today.addDays(-30) is incorrect - TODAY is a literal and does not have any methods.

 

You cannot use :date.today() either.  You will have to set a variable to the values you need and then include the variables in the where condition.

This was selected as the best answer
chubsubchubsub

thanks dmcheng, here's what I have now, and it's not giving me an error anymore, can I use the :system.Today() like this:

 

 

public static String checkGCQuery = 'select Id from Guest_Card__c where Initial_Lead_Type__c = \'Independent Lead Source\''
+ 'AND ((Move_In_Date__c != Null AND LastActivityDate <= :system.Today().addDays(-30))'
+ ' OR (Move_In_Date__c = Null AND LastActivityDate <= :system.Today().addDays(-60)))';


public static void checkGuestCards ()
{

system.debug('\n\n30 checkGCQuery ' + checkGCQuery);

//collect a list of records that need to be inactivated

List <Guest_Card__c> InactiveGCs = Database.query(checkGCQuery);

for (Guest_Card__c gc :InactiveGCs)
{
gc.Status__c = 'Inactive';
gc.Inactive_Reason__c = 'Initial Lead Source is Inactivated';
}

update inactiveGCs;
}
}

 

dmchengdmcheng

Interesting.  When I try system.today() or date.today() in query string in execute anonymous, I get an error "unexpected '('"  

chubsubchubsub

Very weird, when I used this in execute Anonymous, ithe execution was successful yesterday, but then when I tried it again today, it threw that same error message and didn't execute.  So, I changed it to a variable like you suggested and it worked.

 

Thnaks for your help with this again, below is the code I ended up with:

 

public static Date todayMinusThirty = system.Today().addDays(-30);
public static Date todayMinusSixty = system.Today().addDays(-60);


public static String checkGCQuery = 'select Id from Guest_Card__c where Initial_Lead_Type__c = \'Independent Lead Source\''
+ ' AND ((Move_In_Date__c != Null AND LastActivityDate <= :todayMinusThirty)'
+ ' OR (Move_In_Date__c = Null AND LastActivityDate <= :todayMinusSixty))';