Generate CSV using apex and email as attachment

String csvHeader = 'AccountId, Latest Opp \n';
string mainContent = csvHeader;

Map opps = new Map();
for(Opportunity op: [select AccountId, Billing_Frequency__c,Customer_Type__c , closedate from Opportunity where closedate >2019-03-01 and closedate <2020-03-31 and StageName='Closed Won' order by closedate asc]){
opps.put(op.AccountId, op);
}
for (Id oid : opps.keySet()) {
mainContent+= oid+','+opps.get(oid).Id+'\n';
}
Messaging.SingleEmailMessage singEmail = new Messaging.SingleEmailMessage ();

String [] toAddresses = new list {'ayub.salsforce@gmail.com'};

//Set recipient list

singEmail.setToAddresses (toAddresses);

String subject = 'Retroactively Created Agreements';

singEmail.setSubject (subject);

singEmail.setPlainTextBody (mainContent);
Messaging.SendEmailResult [] r = Messaging.sendEmail (new Messaging.SingleEmailMessage [] {singEmail});

175 thoughts on “Generate CSV using apex and email as attachment”

  1. When someone writes an article he/she keeps the thought of a user in his/her brain that how a user can know it.
    So that’s why this article is great. Thanks!

  2. Hi there just wanted to give you a quick heads up. The words in your article seem to be running off the screen in Chrome.
    I’m not sure if this is a format issue or something to do with web browser compatibility but I thought I’d post
    to let you know. The design and style look great though!

    Hope you get the issue resolved soon. Many thanks

  3. I was suggested this website by my cousin. I am not sure whether this post is
    written by him as nobody else know such detailed about my problem.
    You are wonderful! Thanks!

  4. Great goods from you, man. I’ve understand your stuff previous
    to and you are just too excellent. I really like what you’ve
    acquired here, really like what you’re stating and the way in which you say it.
    You make it enjoyable and you still care for to keep it wise.

    I can not wait to read much more from you. This is really
    a wonderful website.

  5. Heya i am for the first time here. I found this board and I find It truly useful & it helped me out much.
    I hope to give something back and help others like you aided me.

  6. Wow, fantastic weblog format! How long have you ever been blogging for?
    you make running a blog look easy. The full look of your site is excellent, let alone the content!

  7. Hi, this weekend is good designed for me, for the reason that this point in time i am reading this enormous informative post here at my home.

  8. I am really loving the theme/design of your blog. Do you ever run into any
    internet browser compatibility problems? A couple of my blog readers have
    complained about my site not working correctly in Explorer but looks great
    in Chrome. Do you have any recommendations to help fix this problem?

  9. Hi this is kind of of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding knowledge so
    I wanted to get advice from someone with experience.
    Any help would be enormously appreciated!

  10. Great post. I was checking continuously this blog and I am impressed!

    Extremely helpful information specifically the last part 🙂
    I care for such information much. I was looking for this
    particular information for a long time. Thank you and good luck.

  11. whoah this weblog is great i really like reading your articles.
    Keep up the great work! You already know, many persons are searching round for this info, you can help them greatly.

  12. I am not certain where you’re getting your info, but great topic.
    I needs to spend some time studying much more or understanding more.
    Thanks for excellent info I used to be in search of this
    information for my mission.

  13. Howdy! This post could not be written any better! Reading through this post reminds me of my old room mate!
    He always kept chatting about this. I will forward this article to him.

    Fairly certain he will have a good read. Thank you for sharing!

Comments are closed.