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
Edward VazquezEdward Vazquez 

System.LimitException: Apex CPU time limit exceeded???

Here is my class below:

global class scheduleLatestCampaigns implements Schedulable {

    global void execute(SchedulableContext SC) {
        //make a map of all accounts to their presentations
        Map<Id, List<Campaign>> accountToCampaigns = new Map<Id, List<Campaign>>();
        List<Campaign> cs = [SELECT Id, Account__c, Date_Time_of_Presentation_Schdeuled__c,Frequency__c,Meeting_Summary__c, Presenter__r.Name FROM Campaign WHERE Date_Time_of_Presentation_Schdeuled__c <= TODAY AND RecordType.Name = 'Presentation' ORDER BY Date_Time_of_Presentation_Schdeuled__c DESC];
        
        for (Campaign c : cs) {
            if (accountToCampaigns.containsKey(c.Account__c)) {
                List<Campaign> currentList = accountToCampaigns.get(c.Account__c);   
                currentList.add(c);
                accountToCampaigns.put(c.Account__c, currentList);
            } else {
                accountToCampaigns.put(c.Account__c, new List<Campaign> { c });
            }
        }
        
        List<Account> updateAccts = new List<Account>();
        //Find the latest campaign on the account that it is attached to and update the account
        for (Account acct : [SELECT Id FROM Account WHERE Id IN :accountToCampaigns.keySet()]) {
            //find all presentations that have already occured
            List<Campaign> campaigns = new List<Campaign>();
            Campaign latestCampaign=null;

            if (accountToCampaigns.containsKey(acct.Id)) {
                latestCampaign = accountToCampaigns.get(acct.Id)[0];
            }

            
            if (latestCampaign != null) {
                acct.Frequency__c = latestCampaign.Frequency__c;
                acct.Last_Presentation__c = date.newinstance(latestCampaign.Date_Time_of_Presentation_Schdeuled__c.year(),
                                                             latestCampaign.Date_Time_of_Presentation_Schdeuled__c.month(),
                                                             latestCampaign.Date_Time_of_Presentation_Schdeuled__c.day());
                acct.Meeting_Summary__c = latestCampaign.Meeting_Summary__c;
                acct.Presenter__c = latestCampaign.Presenter__r.Name;
                updateAccts.add(acct);
            }
        }
        
        update updateAccts;
    }
}


The code is breaking within this loop:
    List<Campaign> cs = [SELECT Id, Account__c, Date_Time_of_Presentation_Schdeuled__c,Frequency__c,Meeting_Summary__c, Presenter__r.Name FROM Campaign WHERE Date_Time_of_Presentation_Schdeuled__c <= TODAY AND RecordType.Name = 'Presentation' ORDER BY Date_Time_of_Presentation_Schdeuled__c DESC]

Any ideas on how to go about fixing this.  
 
Alain CabonAlain Cabon
Hi Edward,

You really need the ORDER BY? probably (otherwise you just use a query locator with its scope).  

Date_Time_of_Presentation_Schdeuled__c is not indexed by default.

A new number unique field is the only option for an indexed field containing a "datetime" (milliseconds since 1970) ... because the field Date_Time_of_Presentation_Schdeuled__c itself cannot be unique (not allowed for this type of field, no checkbox) and without the help of the Salesforce support, we cannot create new indexes but a date time could be indexed (CreatedDate, SystemModstamp are indexed).

A field often used in queries could be automatically indexed "sometimes" by SF (?) so you need to see the query plan.

Query Plan Tool (How To & FAQ): https://help.salesforce.com/articleView?id=000199003&type=1

https://developer.salesforce.com/blogs/engineering/2013/09/collecting-selectivity-statistics-for-force-com-queries.html

Finally, you need a unique field of type Number wich will contain the values of Date_Time_of_Presentation_Schdeuled__c.getTime()
By default, Force.com formula fields don’t have indexes so you need an update of this indexed field each time the associated date time change. Two fields if you display/enter the value Date_Time_of_Presentation_Schdeuled__c on screen.

http://resources.docs.salesforce.com/rel1/doc/en-us/static/pdf/salesforce_query_search_optimization_developer_cheatsheet.pdf

That is the theory because the updates of this indexed field could be time consuming and you need to see the query plan.

Best regards

Alain