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
Mahatma VijayMahatma Vijay 

Apex script unhandled exception by user/organization

Hi, I created a Schedulable class to send a CSV file to an email. While I'm testing it I got this error to my e-mail instead of a CSV file. Can anyone help me with this? I'm attaching the code here.
global class ExporterCSV implements System.Schedulable {
    
	global void execute(SchedulableContext sc) {
	List<Contact> conList = [Select id , Lastname, FirstName , CreatedDate , lastModifiedDate from Contact];
	string header = 'Record Id ,Last Name, First Name , Created Date , Modified Date \n';
	string finalstr = header ;
        
	for(Contact a: conList)
	{
   string recordString = a.id + ',' + a.Lastname+',' + a.FirstName + ',' + a.CreatedDate + ',' + a.LastModifiedDate + '\n';
   finalstr = finalstr + recordString;
	}
        
	Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
	blob csvBlob = Blob.valueOf(finalstr);
	string csvname= 'Contacts.csv';
	csvAttc.setFileName(csvname);
	csvAttc.setBody(csvBlob);
	Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
	String[] toAddresses = new list<string> {'xxxxxxxxxxxxx@gmail.com'};
	String subject = 'Contacts Report CSV';
	email.setSubject(subject);
	email.setToAddresses( toAddresses );
	email.setPlainTextBody('The Contacts report is attached here.');
	email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
        
	//Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
	Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
	//system.debug(r);
  }
}
Error message I got :

Sandbox
 
Apex script unhandled exception by user/organization: xxxxxxxxxxxxx/yyyyyyyyyyyyyy Source organization: zzzzzzzzzzzzzz (null) Scheduled job 'Email Contact test' threw unhandled exception.
 
caused by: System.LimitException: Apex CPU time limit exceeded
 
Class.ExporterCSV.execute: line 14, column 1

 
Best Answer chosen by Mahatma Vijay
Naval Sharma4Naval Sharma4
Hi Mahatma,

Your organization has plenty of records so it hit the salesforce governer limits. In order to get rid of this you have to use batch apex. In Batch apex you can process records in batch and send your data in CSV after executing all the batches i.e. finish method.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

All Answers

Naval Sharma4Naval Sharma4
Hi Mahatma,

Your organization has plenty of records so it hit the salesforce governer limits. In order to get rid of this you have to use batch apex. In Batch apex you can process records in batch and send your data in CSV after executing all the batches i.e. finish method.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
This was selected as the best answer
Mahatma VijayMahatma Vijay
Thanks Naval,

This will help my requirement. Appreciate your response.