+ Start a Discussion
marys pindamarys pinda 

Number line attached file

Is it possible to count the number of lines in an attached file in the email?

Thank you!

Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(contenuCSV);
string csvNom = 'cases_fermes_'+Date.today().format()+'.csv';
csvPJ.setFileName(csvNom);
csvPJ.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] adressMail = new list<string> {mailSouhaite};
String subject = 'Report - '+Date.today().format();
email.setSubject(subject);
email.setToAddresses( adressMail );

email.setPlainTextBody('mail....');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ});
Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Somethig like: csvPJ.getLineNumber();

Thank you
Best Answer chosen by marys pinda
marys pindamarys pinda
Thanks Shashank!

I used the \n as you said and it worked ...
Thank you!
Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(contenuCSV);
string csvNom = 'cases_fermes_'+Date.today().format()+'.csv';
csvPJ.setFileName(csvNom);
csvPJ.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] adressMail = new list<string> {mailSouhaite};
String subject = 'Report - '+Date.today().format();
email.setSubject(subject);
email.setToAddresses( adressMail );

//HERE THE SOLUTION:::::
String lignes = contenuCSV;
List<String> parts = lignes.split('\n');
integer lineNumber = parts.size(); 

email.setPlainTextBody('Lines = lineNumber-1');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ});
Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});


All Answers

Sonam_SFDCSonam_SFDC
Hey Veronica, 

Unfortunately, Messaging.EmailFileAttachment doesn't have any method specific to one that can count the number of lines in an attached file.
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_attachment.htm#apex_Messaging_EmailFileAttachment_methods

You might have to use some code to go through the complete content of the atachment and then populate the lines used.
Shashank SrivatsavayaShashank Srivatsavaya
Hi,

You might have to look for "return" characters in your attachment text to count the number of lines.

Thanks,
Shashank
marys pindamarys pinda
Thanks Shashank!

I used the \n as you said and it worked ...
Thank you!
Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(contenuCSV);
string csvNom = 'cases_fermes_'+Date.today().format()+'.csv';
csvPJ.setFileName(csvNom);
csvPJ.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] adressMail = new list<string> {mailSouhaite};
String subject = 'Report - '+Date.today().format();
email.setSubject(subject);
email.setToAddresses( adressMail );

//HERE THE SOLUTION:::::
String lignes = contenuCSV;
List<String> parts = lignes.split('\n');
integer lineNumber = parts.size(); 

email.setPlainTextBody('Lines = lineNumber-1');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ});
Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});


This was selected as the best answer
ShashForceShashForce
Hi,

You might have to look for "return" characters in your attachment text to count the number of lines.

Thanks,
Shashank