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
Felix LeinertFelix Leinert 

Scheduled apex class does not update all records

Hello together,

I have a scheduled an apex class (billonDailyUpdatesSched) that creates lists on different objects and pushes those lists into another apex class (billonMethods) in order to perform actions on all records collected in the lists.
However, apparently not all records were updated accordingly, although they meet the criteria to be included in the lists.
  • When I manually update the records, it works just fine. So there are no validation rules preventing the apex class from updating the records
  • I have the feeling that we hit the governor limits as too many records are touched by the apex classes at the same time. However, I do not know how to validate that.
Below is the code of the scheduled apex class. Thank you for any help in advance !
global class billonDailyUpdatesSched implements Schedulable 
{
    global void execute(SchedulableContext SC) 
    {

List<ONB2__Subscription__c> ActivatedSubscriptions = Database.query('Select Id, ... 
	from ONB2__Subscription__c 
	where((plReasonForInactive__c = \'CSD in future\' AND ONB2__Status__c = \'Inactive\' AND ONB2__StartDate__c < today) OR (ONB2__StartDate__c = today AND ONB2__Status__c = \'Inactive\' AND plReasonForInactive__c = \'CSD in future\'))');     

billonMethods.updateActiveSubscription(ActivatedSubscriptions);

List<ONB2__Item__c> terminatedItems = Database.query('Select '+Helper.fetchObjectfieldnames('ONB2__Item__c') +', ...
	from ONB2__Item__c 
	Where ((ONB2__Subscription__r.ONB2__AutoRenewal__c = null AND ONB2__Subscription__r.ONB2__EndDate__c = today) OR (ONB2__EndDate__c = today AND bRenewItem__c = false)) AND ONB2__Active__c = true');

billonMethods.updateSubscriptionItemToEndDate(terminatedItems);

List<Account> terminatedAccounts = Database.query('Select Id, ...
	from Account 
	where refActiveSubscription__c != null and refActiveSubscription__r.ONB2__AutoRenewal__c = null and refActiveSubscription__r.ONB2__EndDate__c = Today');

billonMethods.updateAccountTypeLifecycle(terminatedAccounts);

List<ONB2__Subscription__c> terminatedSubscriptions = Database.query('Select Id, ... 
	from ONB2__Subscription__c 
	where ONB2__AutoRenewal__c = null and ONB2__EndDate__c = Today');

billonMethods.updateterminatedSubscription(terminatedSubscriptions); 

Date dateMerchantExit = System.today().addDays(28);    

List<ONB2__Subscription__c> SubscriptionsOfExitMerchants = Database.query('Select Id, ...
	 from ONB2__Subscription__c 
	where ONB2__Status__c = \'Terminated in Due Time\' and ONB2__EndDate__c = :dateMerchantExit');

System.debug('========================================> This is the found Subscription: '+SubscriptionsOfExitMerchants);

Map <Id,Messaging.SendEmailResult> mapTargetObjectIdToResult = new Map<Id, Messaging.SendEmailResult>();

For (ONB2__Subscription__c sub : SubscriptionsOfExitMerchants) {
        if ((sub.ONB2__Account__r.ParentId != null && (sub.ONB2__Account__r.RecordType.DeveloperName == 'SME2015' && sub.ONB2__Account__r.Parent.RecordType.DeveloperName == 'SME2015')) ||
            (sub.ONB2__Account__r.ParentId == null && sub.ONB2__Account__r.RecordType.DeveloperName == 'SME2015'))   
        {
            
            List<id> listContacts = new List<id>();
            listContacts.add(sub.ONB2__Account__r.OwnerId);
            listContacts.add(sub.ONB2__Contact__c); 
    
            User userSender = [Select Id, Name, Email from User where Id =: sub.ONB2__Account__r.OwnerId LIMIT 1];
    
            systemEmail smReminderOfAccountClosing = new systemEmail('Reminder_account_closure', sub.ONB2__Account__r.plTemplateLanguage__c);
            mapTargetObjectIdToResult = smReminderOfAccountClosing.sendTemplateAsUser(sub.Id, userSender, listContacts);
        
        }
    }

Database.executeBatch(new billonCalculateMRROnAccountBatch());

if (Limits.getDMLStatements()>120||Limits.getDMLRows()>40000){Helper.sendErrorMail('Nearing DML Limits in billon Daily Updates: pls start preparing for Batch');}

   }
}




 
Best Answer chosen by Felix Leinert
Felix LeinertFelix Leinert

Hello manj,
thank you for your response.
I checked this point as well. However, I do not receive any email.
We "sovled" this problem by setting up reports that use the same criteria as the apex class, so that all records that were not updated by the apex class will show up in the reports.
A proper solution would be to split up the apex class in order to recude the number of records included in the scheduled job in order to prevent hitting the governor limits.

Best wishes,
Felix
 

All Answers

Manj_SFDCManj_SFDC
May be an exception occurred is causing this problem you can check in the 
debug logs or you can print the job status in your finish method 
Felix LeinertFelix Leinert

Hello manj,

thank you for getting back!
I set up a debut log around the time when the jub is running (runs every day at 11pm CET = GMT+2h)

User-added image

I noticed that this job is never displayed in the list of apex jobs in the setup (Setup -> Jobs -> Apex Jobs).
Can you think of a reason why?

Thanks for your help in advance!
Best wishes,
Felix

Manj_SFDCManj_SFDC
Hi Felix, when you execute do you receive any email (as per line 59 )
Felix LeinertFelix Leinert

Hello manj,
thank you for your response.
I checked this point as well. However, I do not receive any email.
We "sovled" this problem by setting up reports that use the same criteria as the apex class, so that all records that were not updated by the apex class will show up in the reports.
A proper solution would be to split up the apex class in order to recude the number of records included in the scheduled job in order to prevent hitting the governor limits.

Best wishes,
Felix
 

This was selected as the best answer