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
MWorldMMMWorldMM 

Timeout When Using getContentAsPDF()

Our client needs us to dump records from several large related objects into PDFs for archival use. These PDF's are then saved as record attachments in a custom object named Snapshot. The PDF's generate with no problem as long as the nuumber of records involved is below a certain number. Beyond that point we get the following error on screen:

Time limit exceeded
Your request exceeded the time limit for processing. _

Going through debug logs shows that NO exception is being thrown (despite the Try wrapper around the call). Instead the getContentAsPdf() call is returning after just over 2 minutes and processing is simply ending at that point.

Here are the relevant lines from the debug log:

 

<SNIP>
13:30:34.197 (197470000)|SYSTEM_METHOD_ENTRY|[21]|System.PageReference.getContentAsPDF() 13:32:36.249 (122249850000)|SYSTEM_METHOD_EXIT|[21]|System.PageReference.getContentAsPDF() 13:32:36.249 (122249973000)|CODE_UNIT_FINISHED|GroupOrderBackupAttach invoke(createSnapshot) 13:32:36.875 (122253275000)|CUMULATIVE_LIMIT_USAGE 13:32:36.875|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 1 out of 100 Number of query rows: 1 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 1 out of 150 Number of DML rows: 1 out of 10000 Number of script statements: 11 out of 200000 Maximum heap size: 0 out of 3000000 Number of callouts: 0 out of 10 Number of Email Invocations: 0 out of 10 Number of fields describes: 0 out of 100 Number of record type describes: 0 out of 100 Number of child relationships describes: 0 out of 100 Number of picklist describes: 0 out of 100 Number of future calls: 0 out of 10 13:32:36.875 (122253275000)|CUMULATIVE_LIMIT_USAGE_END 13:32:36.253 (122253361000)|CODE_UNIT_FINISHED|VF: /apex/GroupOrderBackupAttach 13:32:36.253 (122253383000)|EXECUTION_FINISHED

Here's the relevant code:

 

public PageReference createSnapShot()
    {
        Snapshot__c ss= new Snapshot__c(Group_Order__c=go.Id,Snapshot_Date__c=Date.today(),Snapshot_Type__c='Audit');
        insert ss;
        
        PageReference pdf1 = Page.GroupOrderBackup;
        pdf1.getParameters().put('id',go.Id);

        Blob pdf1Blob;
        try{
        	pdf1Blob = pdf1.getContentAsPdf();
        }
        catch(Exception e){
        	pdf1Blob  = Blob.valueOf('Exception: ' + e.getTypeName() + ' - ' + e.getMessage() + ' - ' + e.getCause() + ' - ' + e.getLineNumber() + ' - ' + e.getStackTraceString());
        }
 
        Attachment a = new Attachment(parentId = ss.id, name='Full Backup_'+go.Name+'_'+datetime.now() + '.pdf', body = pdf1Blob);
        insert a;
       	
       	if (go.RecordType.Name == 'RTMS')
        {
        	PageReference excel = new PageReference('/apex/CustomExcelWithBackButton');
        	excel.setRedirect(true);
        	excel.getParameters().put('groupOrderId',go.Id);
    
        	return excel;
        } 
        else 
       	{
       		return controller.view();
       	}
    }

Any ideas on how to get past this? Is there a setting (either open or "Black") that can be changed to give us more time?

WizradWizrad

Hey Mauricio,

 

Unfortunately you can't catch governor limit "exceptions".

 

Since it's for archival use (e.g. not done frequently), does it have to be done in apex?  Could you write a java app or something that doesnt care about governor limits to accomplish the task?

MWorldMMMWorldMM

Unfortunately, it's done more often than a purely archival solution would be appropriate for. I won't get into the business details but suffice to say that the client wants one-click creation of backup/archive records. Their business porcess demands it.