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. Hey there! I know this is kinda off topic however , I’d figured I’d ask.
    Would you be interested in exchanging links
    or maybe guest authoring a blog article or vice-versa?
    My blog addresses a lot of the same subjects as
    yours and I feel we could greatly benefit from each other.
    If you happen to be interested feel free to send me an e-mail.
    I look forward to hearing from you! Great blog
    by the way!

  2. Great blog! Do you have any tips for aspiring writers?

    I’m planning to start my own blog soon but I’m a little lost
    on everything. Would you propose starting
    with a free platform like WordPress or go for a paid
    option? There are so many options out there that I’m totally overwhelmed ..
    Any recommendations? Thanks!

  3. fantastic put up, very informative. I ponder why the other
    specialists of this sector do not realize this. You must proceed your writing.
    I am sure, you’ve a huge readers’ base already!

  4. Pretty element of content. I just stumbled upon your website and in accession capital to claim
    that I get actually loved account your blog posts. Anyway
    I will be subscribing for your augment and even I fulfillment you get right of entry to
    persistently rapidly.

  5. Link exchange is nothing else except it is only placing the
    other person’s web site link on your page at appropriate place and other person will
    also do same in favor of you.

  6. It’s a shame you don’t have a donate button! I’d most certainly donate to this superb blog!

    I guess for now i’ll settle for bookmarking and adding your RSS feed to my
    Google account. I look forward to brand new updates and will share this blog
    with my Facebook group. Chat soon!

  7. You are so cool! I do not believe I’ve read something like this before.
    So wonderful to discover somebody with a few unique thoughts on this issue.
    Really.. many thanks for starting this up. This site
    is something that is needed on the web, someone with a little originality!

  8. Greetings, I think your web site could possibly be having browser compatibility problems.

    Whenever I look at your website in Safari, it looks fine however when opening in I.E., it has some overlapping issues.
    I just wanted to provide you with a quick heads
    up! Aside from that, great blog!

  9. You need to be a part of a contest for one of the most useful sites on the internet.
    I am going to recommend this blog!

  10. Oh my goodness! Awesome article dude! Many thanks, However
    I am going through issues with your RSS. I don’t know why I am
    unable to join it. Is there anyone else getting the same RSS issues?
    Anyone that knows the solution will you kindly respond? Thanks!!

  11. Everything is very open with a very clear description of the challenges.

    It was definitely informative. Your site is extremely helpful.
    Many thanks for sharing!

  12. Hello there, I discovered your web site by means of Google at the same time as looking for a comparable matter, your site came up, it appears great.

    I have bookmarked it in my google bookmarks.

    Hi there, just was aware of your blog via Google, and found that it’s truly
    informative. I’m going to be careful for brussels.
    I’ll be grateful in the event you continue
    this in future. Many folks will probably be benefited
    out of your writing. Cheers!

  13. We’re a gaggle of volunteers and starting a new scheme in our community.
    Your website provided us with helpful info to work on. You
    have done an impressive activity and our whole community might be
    grateful to you.

  14. I know this if off topic but I’m looking into
    starting my own blog and was wondering what all is
    needed to get set up? I’m assuming having a blog like yours would cost
    a pretty penny? I’m not very internet smart so I’m not 100% positive.
    Any tips or advice would be greatly appreciated. Many thanks

  15. It’s actually a great and helpful piece of information. I am satisfied that you shared this useful information with us.
    Please stay us up to date like this. Thanks for sharing.

Comments are closed.