I have a Map. that I built in apex class. I want to put that map output into an excel and send it as attachment. Here's the code that Im using. I get Blob variable doesn't exist error ( in the highlighted line below).

mOutput is the Map

Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
Blob csvBlob = blob.valueOf(mOutput); // <-- ERROR IN THIS LINE
string csvname= 'CodeCoverage.xls';
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {'test@test.com'};
String subject ='Code CSV';
email.setToAddresses( toAddresses );
email.setPlainTextBody('Code CSV Attached');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

All I want to know is how do I put that map content into the Excel and send as attachment.

EDIT: This is the format Im looking for in the excel. Each one in a different cell

Name    Value
ABC     10
CAR     20
OUT     30
DEG     50

What Im getting currently now is the output is in One column only!

Here's the formula

rows = rows + Name +',' + Value + '\n' ;

  • And where do you set the value of mOutput – Eric Jan 18 '15 at 15:17
  • I get it from outside system (HTTP Request ) i get in the format of (a=10, b=20, ... Etc ). (String, decimal). I put it in a map. Yes, that can be formatted however we want. – sfdcFanBoy Jan 19 '15 at 1:49

The Blob.valueof method requires a string to be passed into the object. You will first need to convert the Map to the string representation of the of the CSV data. I had ran into that issue once before, and it really should have a more meaningful error and that Blob.valueof(TYPE) is not defined.

You should be able to quickly test this by passing in a string to your Blob.valueof method.


| improve this answer | |
  • if its a string. how will the string split it into multiple rows in csv? – sfdcFanBoy Jan 19 '15 at 7:16
  • You will need to use the '\n' character as the new line character. Your first line should be the a comma separated list of header names followed by the new line character and then each additional line of data would follow the same pattern. – TC Sutton Jan 19 '15 at 7:23
  • ok got it. I used this ---> rows = rows + Name +',' + Value + '\n' ; I get in new lines great but its still single column. The comma isn't getting recognised I guess. – sfdcFanBoy Jan 19 '15 at 14:20
  • If you provide the format you would like the first record to display as in your CSV I could help you format it correctly. – TC Sutton Jan 19 '15 at 17:27
  • 1
    Well the issue is with excel in this case, as you have successfully created a csv attachment. You have a couple of options. 1) You could change the extension to .csv which is probably the safest option. 2) You could go with a tab delimited file instead of a comma delimited file as .xls files tend to parse tab files into different columns. If you decide to go this route you would want to replace the commas with the tab character: '\t' Also, don't forget to replace the comma in the header row as well. – TC Sutton Jan 20 '15 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.