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
Alex YhapAlex Yhap 

Apex CPU Time Out & Time limit exceeded Error

When I click a button to call this Visualforce page, I get this error

Apex CPU time limit exceeded
Error is in expression '{!init}' in component <apex:page> in page confirmstudents: (APXTConga4)

An unexpected error has occurred. Your solution provider has been notified. (APXTConga4)
Here is the code for my VF Page
<apex:page standardController="JA_Event__c" extensions="ConfirmStudentsController" action="{!init}">
  
</apex:page>
The code for the controller:
 
public with sharing class ConfirmStudentsController {

    public ConfirmStudentsController(ApexPages.StandardController controller) {

    }

    public PageReference init() {
        string eventId = ApexPages.CurrentPage().getParameters().get('id');
        List<Student_Registrations__c> srs = [SELECT Id, Status__c FROM Student_Registrations__c WHERE Ja_Event__c = :eventId AND Status__c = 'Confirmed'];
        if (srs!=null && srs.size()>0) {
          for(Student_Registrations__c sr : srs)
            sr.Status__c = 'Confirmed - Emailed';
          update srs;
        }
        PageReference page = null;
        if (eventId != null && eventId != '') {
            page = new PageReference('/'+eventId);
            page.setRedirect(true);
        }
        return page;
    }
    
    @IsTest(SeeAllData=true)
    static void testMe() {
        Student_Registrations__c reg =  [SELECT id, Name FROM Student_Registrations__c Limit 1];
        ApexPages.StandardController std = new ApexPages.StandardController(reg);
        ConfirmStudentsController cnt = new ConfirmStudentsController(std);
        
        cnt.init();
        
    }

}
Can someone help me to determine weather the controller's coding could be causing this error or if something else might be doing it? The debug logs didnt have much to go on but it seems that is was entering Conga's APXTConga4 package & firing off WF_RULE_INVOCATION between DML_BEGIN and DML_END log entries. 

Here is a sample of the debug logs (Dont want to give the whole thing because of client information):
14:50:56.500 (36500006418)|WF_EMAIL_SENT|Template:00XC0000001ScJk|Recipients:victoriaayela@hotmail.ca |CcEmails:
14:50:56.500 (36500136190)|WF_ACTION| Field Update: 200; Task: 200; Email Alert: 200;
14:50:56.500 (36500141408)|WF_RULE_EVAL_END
14:50:56.502 (36502658216)|ENTERING_MANAGED_PKG|APXTConga4
14:50:56.521 (36521887566)|CODE_UNIT_FINISHED|Workflow:01IC0000000y2zI
14:52:40.379 (140379062327)|DML_END|[13]
14:52:40.379 (140379250658)|SYSTEM_MODE_EXIT|false
14:52:40.379 (140379374343)|CODE_UNIT_FINISHED|ConfirmStudentsController invoke(init)
14:52:40.385 (140385527191)|CUMULATIVE_LIMIT_USAGE
14:52:40.385 (140385527191)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1142 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1142 out of 10000
  Maximum CPU time: 13357 out of 10000 ******* CLOSE TO LIMIT
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

14:52:40.385 (140385527191)|CUMULATIVE_LIMIT_USAGE_END

14:52:40.385 (140385601052)|CODE_UNIT_FINISHED|VF: /apex/ConfirmStudents
14:52:40.394 (140394356868)|EXECUTION_FINISHED
If anyone can help it would be much appreciated!


 


 

Andy BoettcherAndy Boettcher
At first blush, it appears that you have either a lot of data being processed, or your table is configured in such a way where that query is taking too long to run and DML to process.

You can talk to support about potentially enabling some indexing on the sObject to help the query side of things, but there are also other factors such as master/detail rollups, lookups, and other relationship things that can be tripping you up as well.

A workaround could be to place this functionality in some BATCH or FUTURE Apex framework where the timeouts are higher.
Shashi PatowaryShashi Patowary
Hi Alex,

Can you please remove this code snippet from your controller and check .You have defined a test class within a class (which is not best practice) and it ahs one method without declaring it with TestMethod.
 
@IsTest(SeeAllData=true)

	    static void testMe() {
	        Student_Registrations__c reg =  [SELECT id, Name FROM Student_Registrations__c Limit 1];
	        ApexPages.StandardController std = new ApexPages.StandardController(reg);
	        ConfirmStudentsController cnt = new ConfirmStudentsController(std);         
	        cnt.init();         
	    }

Please let us know if it helps.

Regards,
Shashi