You need to sign in to do that
Don't have an account?

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.
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
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
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
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 will help my requirement. Appreciate your response.