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
Jay reddyJay reddy 

Time limit exceeded to load a visualforce page

Hello Everyone,

We have a button "Send Email" at the Opportunity level. Upon on clicking it a field at the opportunity will automatically checked and the page would re-direct to standard "Send Email" page. After sending the email with attachments the page re-directs to the Opportunity main page and a VF page with controler runs/replace the main page url to save the attachments to the Notes & Attachment section. But sometimes it take ages to reload and even throws time limit exceeded error. Could someone help me out on this, please?

Below is the Visualforce page and it's controller to save the Attachments in the Send an Email to the Opportunity's Notes & Attachments section.
 
<apex:page standardController="Opportunity" extensions="CopyAttachmentToOpptyController" action="{!autoRun}">
  <apex:sectionHeader title="Auto-Running Apex Code"/>
  <apex:outputPanel >
      You tried calling Apex Code from a button.  If you see this page, something went wrong.  You should have 
      been redirected back to the record you clicked the button from.
  </apex:outputPanel>
</apex:page>
public class CopyAttachmentToOpptyController {
 
    // Constructor - this only really matters if the autoRun function doesn't work right
    private final Opportunity o;
    public CopyAttachmentToOpptyController(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    // Code we will invoke on page load.
    public PageReference autoRun() {
    
        List<Attachment> attachmentsToInsert = new List<Attachment>(); 
        String theId = ApexPages.currentPage().getParameters().get('id');
 
        if (theId == null) {
            // Display the Visualforce page's content if no Id is passed over
            return null;
        }      
        
        Set<ID> emailMsgID = new Set<ID>();
        for (EmailMessage em:[SELECT Id, ParentId, relatedtoid FROM EmailMessage where relatedtoid =:theId ]) {
            emailMsgID.add(em.Id);
        }
        system.debug('emailMsgID-->'+emailMsgID);
        
        List<Attachment> attachmentList = [SELECT Id, ParentId, name, body from Attachment where ParentId IN: emailMsgID];
        List<Attachment> alreadyAttached = [SELECT Id, ParentId, name, body from Attachment where ParentId =: theId];
        
        system.debug('attachmentList -->'+attachmentList);
        system.debug('alreadyAttached -->'+alreadyAttached);
               
        Set<ID> skipAttachment = new Set<ID>();
      /*  for (Attachment a : attachmentList){
            for(Attachment al :alreadyAttached){  
                if(a.name == al.name){      
                    skipAttachment.add(a.Id);
                }
            }
        } */
        
        for (Attachment a : attachmentList){  
            if(!skipAttachment.contains(a.Id)){      
                Attachment att = new Attachment(ParentId = theId, Name = a.name, Body = a.body);
                attachmentsToInsert.add(att);
            }
        }
        
        system.debug('attachmentsToInsert-->'+attachmentsToInsert);
        
        if (attachmentsToInsert.size() > 0) {
            insert attachmentsToInsert;
        }        
         
          
        // Redirect the user back to the original page
       /* PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
        pageRef.setRedirect(true);
        return pageRef; */
        
        return new PageReference('/' + theId);
 
    }