+ Start a Discussion
woodmanzeewoodmanzee 

Apex query in schedulable class returning only one record

I'm running this code in a schedulable class and expecting it to return at least 50. I had it filtering by a RecordTYpeId, but took it out to just query all contacts and am still only getting one contact returned. Any explanation for why or have any suggestions? Here is the code in question:

 

public void execute(SchedulableContext SC) {
        Date today = date.today();
        Integer numberDays = date.daysInMonth(today.year(), today.month());
        
        Id careProvider = [SELECT Id FROM RecordType WHERE Name = 'Care Provider'].Id;
        
        if (today.day() == 2 || today.day() == (numberDays-1)) {
            System.debug('Update scheduled.');
            // get all care providers
            List<Contact> careproviders = [SELECT Name, RecordTypeId, Email_Trigger__c FROM Contact];
            
            System.debug('Contact list size: ' + careproviders.size());                                  
            List<Contact> updateList = new List<Contact>();
            for (Contact c : careproviders) {
                System.debug('Added new care provider to update list.');
                c.Email_Trigger__c = true;
                updateList.add(c);
            }
            System.debug('Size of list to be updated: ' + updateList.size());
            update updateList;
            System.debug('Update List: ' + updateList);
        }
    }    

 

Thanks!

hisalesforcehisalesforce

I don't see any filter is used while doing query for contact.

List<Contact> careproviders = [SELECT Name, RecordTypeId, Email_Trigger__c FROM Contact];
//add a filter criteria Where Recordtypedid=:careprovider///

//Also try to add Limit 50 in your query for testing purpose only.Since you are not using any filter criteria chances are that you may be reaching governor limit.Can you let us know how may contacts are there in your org.
woodmanzeewoodmanzee

I'm aware that the filter criteria isn't being used, when I had the WHERE clause it still was only returning one contact so I removed it for testing. But with the WHERE filter or not, it still only returns one contact. If I do the same query in the Developer Console, it returns all the contacts I need. There are about 350 contacts currently in my org.