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. Aw, this was a very nice post. Taking a few minutes and actual
    effort to produce a very good article… but what can I
    say… I hesitate a lot and never seem to get nearly anything done.

  2. magnificent post, very informative. I ponder why the opposite experts of this sector
    don’t notice this. You must proceed your writing.
    I’m confident, you have a huge readers’ base already!

    My web-site … shaboxes.com

  3. Thanks for ones marvelous posting! I truly enjoyed
    reading it, you may be a great author. I will make certain to bookmark your blog and will come
    back someday. I want to encourage you to definitely continue
    your great job, have a nice day!

  4. Pretty part of content. I simply stumbled upon your website and in accession capital
    to claim that I acquire in fact enjoyed account your weblog posts.
    Any way I’ll be subscribing in your feeds and even I achievement you get
    right of entry to constantly fast.

    Here is my web page … backpageladies.com

  5. I just like the helpful information you provide in your articles.
    I will bookmark your blog and take a look at once more right here frequently.
    I’m reasonably certain I will learn many new stuff right right here!

    Good luck for the following!

    Here is my page … litdevelopments.com

  6. Aw, this was an extremely good post. Finding the time and actual effort to create a good article… but what can I say… I
    hesitate a whole lot and never seem to get anything done.

  7. Howdy fantastic website! Does running a blog like this require a massive amount work?
    I have no knowledge of programming however I had been hoping to start my own blog soon. Anyhow, should
    you have any ideas or tips for new blog owners
    please share. I understand this is off topic nevertheless I just wanted to ask.

    Thanks!

  8. Hi there, I discovered your blog by way of Google even as searching for a comparable subject, your website got here up, it seems
    to be good. I have bookmarked it in my google bookmarks.

    Hello there, simply was alert to your weblog thru Google, and located that it’s really informative.
    I’m going to be careful for brussels. I’ll be grateful if you continue this in future.
    Numerous folks will likely be benefited out of your writing.
    Cheers!

  9. Hi my loved one! I wish to say that this post is awesome,
    great written and include approximately all important
    infos. I would like to look extra posts like this .

  10. hello there and thank you for your information – I have certainly
    picked up anything new from right here. I did however expertise several technical
    issues using this site, as I experienced to reload the web site a lot
    of times previous to I could get it to load properly.
    I had been wondering if your web hosting is OK? Not that I am complaining, but slow loading
    instances times will often affect your placement in google and could damage your quality score if ads
    and marketing with Adwords. Well I am adding this RSS to my e-mail and could
    look out for a lot more of your respective exciting content.
    Ensure that you update this again very soon.

Comments are closed.