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 {''};

//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. I just could not leave your site before suggesting that I
    actually loved the standard information an individual supply for your visitors?
    Is going to be again frequently in order to check out new posts.

    My website –

  2. Great items from you, man. I have be aware your stuff
    prior to and you’re simply extremely wonderful. I really
    like what you’ve acquired right here, really like what you are stating and the best way during which you assert it.
    You make it enjoyable and you still take care of to keep it smart.

    I can’t wait to learn much more from you. That is really a great website.

  3. I was suggested this blog via my cousin. I am now not certain whether this publish
    is written via him as no one else understand such detailed about my trouble.
    You are amazing! Thank you!

  4. I know this if off topic but I’m looking into starting my own weblog and was curious what all is required 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% sure.
    Any tips or advice would be greatly appreciated.

  5. hello there and thank you for your information ?
    I?ve definitely picked up anything new from right here.
    I did however expertise a few technical points using this web site, as I experienced to reload the
    web site lots of times previous to I could get it to load properly.

    I had been wondering if your hosting is OK? Not that I am complaining, but sluggish loading instances times will often affect
    your placement in google and can damage your quality score if ads
    and marketing with Adwords. Well I?m adding this RSS to my email and
    can look out for a lot more of your respective fascinating content.
    Make sure you update this again very soon..

    my homepage –

  6. I really like your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it
    for you? Plz answer back as I’m looking to
    create my own blog and would like to find
    out where u got this from. thank you

    Also visit my page ::

  7. My brother recommended I would possibly like this website.
    He used to be totally right. This submit truly
    made my day. You cann’t consider just how much time I had spent for this
    information! Thank you!

  8. Hello there, I do believe your web site might be having internet browser compatibility problems.
    Whenever I take a look at your website in Safari, it
    looks fine however, when opening in IE, it has some overlapping issues.
    I merely wanted to give you a quick heads up! Aside from that, excellent blog!

  9. Very efficiently written post. It will be supportive to anyone who utilizes it, as well as myself.
    Keep doing what you are doing – can’r wait to read more

    Look into my page

  10. I’m not that much of a online reader to be honest but your blogs really nice, keep it up!

    I’ll go ahead and bookmark your website to come back
    later on. All the best

Comments are closed.