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
Mitchell SpanoMitchell Spano 

First attempt at a batch Class

Hello All,
We have two custom objects called Financial_Account__c and Household__c with a junction object called household_account__c.
On the household object we have a checkbox field called Is_new_Household__c.
On the Financial Account object we have a date field called Account_Open_Date__c.
I am trying to write a class that checks all financial accounts associated to a household which has been modified within the last day and checks if any of them have an open date which is prior to the household's creation date. If so, the household will not be marked as new.

Here is what I have so far:

global class NewHouseholdBatch implements Database.Batchable<sObject>{
 
     global NewHouseholdBatch(){
                // Batch Constructor
     }
     
     // Start Method
     global Database.QueryLocator start(Database.BatchableContext BC){
         String query = 'SELECT Id, Is_New_Household__c, CreatedDate FROM Household__c WHERE SystemModStamp > :Datetime.now().addDays(-1)'
      return Database.getQueryLocator(query);
     }
    
   // Execute Logic
    global void execute(Database.BatchableContext BC, List<Household__c>scope){
        for (Household__c hh : scope){
            Boolean isnew = true;
            List<Household_Account__c> HouseholdAccounts = [SELECT Household__c.id, Financial_Account__c.id FROM Household_Account__c WHERE Household__c.id = hh.id ];
            for (Household_Account__C hha : HouseholdAccounts ){
                List<Financial_Account__c> FinAccount = [SELECT Account_Open_Date__c, AAC_Account_Status__c FROM Financial_Account__C WHERE Id = hha.Financial_Account__c.id LIMIT 1];
                For (Financial_Account__C fa : FinAccount){
                    if (fa.Account_Open_Date__c < hh.CreatedDate){
                        isnew = false;
                    }
                }
                
            }
            if (isnew==false){
                hh.Is_New_Household__c =false;
            }
        }    
   
    }
   
    global void finish(Database.BatchableContext BC){
         // Logic to be Executed at finish
    }
 }


The developer console keeps saying "unexpected token: 'return' "
I am cofused about this error.
Also, do I need to appent the '.id' to the ennd of the name of the objects in my query on the household account object?

Thank you.
Katia HageKatia Hage
The bind expression in the SOQL in your start() method doesn't work with a class (Datetime). Bind expressions are for variables. So if you change the SOQL to the following, it should work (there is also a missing semicolon at the end of the line):

Datetime dt = Datetime.now().addDays(-1);
String query = 'SELECT Id, Is_New_Household__c, CreatedDate FROM Household__c WHERE SystemModStamp > :dt';

It's doc about bind variables: http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_variables.htm

About the second question, I don't see why you need to add the Id field on the objects in the query on Household_Account__c.