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. Every weekend i used to pay a visit this site, as i wish for enjoyment, as this this web site conations actually fastidious funny data too.

  2. Thank you for some other informative site. Where else may just I am getting that kind
    of info written in such a perfect method? I have a mission that I am just now
    operating on, and I have been at the look out for such information.

  3. Spot on with this write-up, I really think
    this site needs far more attention. I’ll probably be back
    again to see more, thanks for the information!

  4. Helpful info. Lucky me I discovered your website unintentionally, and
    I’m stunned why this coincidence did not came about earlier!

    I bookmarked it.

  5. Good day! Would you mind if I share your blog with my twitter group?

    There’s a lot of folks that I think would really appreciate your content.
    Please let me know. Cheers

    My site: greatwall99 for ios [918kiss-m.com]

  6. Hey just wanted to give you a brief heads up and let you
    know a few of the images aren’t loading properly.

    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different browsers and both show the
    same outcome.

    Feel free to surf to my webpage playboy2apk android

  7. Thanks a lot for sharing this with all people you actually
    recognise what you are speaking approximately! Bookmarked.
    Kindly additionally talk over with my site =). We could have a hyperlink change arrangement between us

    Also visit my homepage; akaun test king855

  8. Thank you a lot for sharing this with all people you actually know what you’re talking about!
    Bookmarked. Please also discuss with my web site =).
    We could have a link exchange contract between us

    Feel free to surf to my blog post … slot sky777

  9. Hi there, just became aware of your blog through Google, and found that it’s
    truly informative. I’m going to watch out for brussels.
    I’ll appreciate if you continue this in future. Many people will be benefited from your writing.
    Cheers!

    Here is my web-site; Live22 Download Apk

  10. It’s a shame you don’t have a donate button! I’d definitely
    donate to this fantastic blog! I suppose 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!

  11. You could definitely see your skills in the work you write.
    The sector hopes for more passionate writers like you who aren’t afraid to
    mention how they believe. At all times go after your heart.

    Here is my web-site: jom kiss918

  12. I am curious to find out what blog system you happen to be working with?
    I’m experiencing some small security issues with my latest blog and
    I would like to find something more secure. Do you have any recommendations?

    my website … sgbet99

Comments are closed.