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. My brother recommended I might like this web site.

    He used to be entirely right. This publish truly
    made my day. You can not consider just how so much time I had spent
    for this information! Thank you!

  2. What’s up it’s me, I am also visiting this site on a regular
    basis, this website is truly fastidious and the viewers are really
    sharing nice thoughts.

  3. Howdy! I know this is somewhat off-topic but I had to ask.
    Does building a well-established website like yours require a large amount of
    work? I’m completely new to operating a blog however I do write in my journal daily.
    I’d like to start a blog so I can easily share my own experience and feelings online.
    Please let me know if you have any kind of ideas or
    tips for brand new aspiring blog owners. Thankyou!

  4. I have been surfing online greater than 3 hours today, but I by no means
    discovered any attention-grabbing article like yours.
    It is beautiful price enough for me. In my view, if all website owners and bloggers made just right content as
    you probably did, the web shall be much more helpful than ever before.

  5. I’m amazed, I must say. Seldom do I encounter a blog that’s both equally educative and amusing, and let me tell you, you have
    hit the nail on the head. The issue is an issue that too
    few folks are speaking intelligently about. I’m very happy that I came across this during my search for something concerning this.

  6. I’m not that much of a online reader to be honest but your
    sites really nice, keep it up! I’ll go ahead and bookmark your site to
    come back later on. All the best

  7. Incredible! This blog looks exactly like my old one!

    It’s on a completely different subject but it
    has pretty much the same layout and design. Wonderful choice of colors!

  8. After I initially commented I appear to have clicked on the -Notify me when new
    comments are added- checkbox and now each time a comment is added I receive 4 emails with the
    exact same comment. Is there a way you are able to remove me from that
    service? Kudos!

  9. We stumbled over here different page and thought I may as well check things out.
    I like what I see so i am just following you. Look forward to finding out
    about your web page again.

  10. Great post. I used to be checking constantly this blog and I’m inspired!
    Extremely helpful info particularly the last phase šŸ™‚ I maintain such info much.
    I used to be seeking this particular info for a long time. Thank you
    and best of luck.

  11. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is
    added I get four e-mails with the same comment.
    Is there any way you can remove people from that service?
    Cheers!

  12. I got this site from my buddy who told me concerning this website and
    at the moment this time I am visiting this website and reading very informative posts at this place.

    My website: xe88 game

  13. Hey! I could have sworn I’ve been to this website before but after reading
    through some of the post I realized it’s new to me. Nonetheless, I’m definitely happy I found
    it and I’ll be bookmarking and checking back frequently!

  14. Please let me know if you’re looking for a author for your blog.
    You have some really great posts and I think
    I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some material for your blog in exchange for a link back
    to mine. Please send me an e-mail if interested. Cheers!

  15. I’m not sure exactly why but this blog is loading incredibly slow
    for me. Is anyone else having this problem or is it a issue on my end?
    I’ll check back later and see if the problem still exists.

  16. I’m extremely impressed with your writing skills and also with
    the layout on your weblog. Is this a paid theme or did
    you modify it yourself? Either way keep up the nice quality writing, it’s rare to see a great blog like this one these days.

Comments are closed.