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. I enjoy what you guys are up too. This kind of clever work and exposure!
    Keep up the awesome works guys I’ve added you guys to my personal blogroll.

  2. After I originally commented I seem to have clicked the -Notify me when new comments are added-
    checkbox and now every time a comment is added I receive 4 emails with the
    exact same comment. There has to be a means you are able to remove me from
    that service? Thanks a lot!

  3. Thanks for the marvelous posting! I genuinely enjoyed reading it, you are a great author.
    I will make certain to bookmark your blog and will eventually come back
    later in life. I want to encourage you continue your great work, have a nice evening!

  4. We are a group of volunteers and starting a brand new
    scheme in our community. Your website offered us with helpful information to
    paintings on. You have done a formidable task and our whole community shall be thankful to you.

    My homepage … fahl.uk

  5. I truly love your website.. Great colors & theme.
    Did you create this site yourself? Please reply back as I’m attempting
    to create my own personal site and want to learn where you got this from or just
    what the theme is called. Appreciate it!

  6. Thanks for another informative blog. Where else may I get that type of
    info written in such a perfect method? I have a venture that I’m
    just now operating on, and I’ve been at the look out
    for such info.

  7. It’s a pity you don’t have a donate button! I’d certainly donate
    to this excellent blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account.

    I look forward to new updates and will share this site with my Facebook group.
    Talk soon!

  8. Amazing! This blog looks just like my old one! It’s on a completely different topic
    but it has pretty much the same page layout and design. Wonderful choice of colors!

  9. My relatives always say that I am wasting my time here at net,
    except I know I am getting experience all the time by reading thes nice articles.

  10. Thank you, I’ve recently been looking for information about this topic
    for ages and yours is the best I’ve found out so far. However, what in regards
    to the bottom line? Are you positive concerning
    the source?

  11. hello!,I like your writing so much! share we be in contact more about your article
    on AOL? I need a specialist in this area to resolve my problem.
    May be that’s you! Taking a look ahead to
    see you.

  12. We are a bunch of volunteers and starting a brand new scheme in our community.
    Your web site offered us with helpful info to work on. You’ve performed a formidable process and
    our whole group will probably be thankful to you.

    my web page fahl.uk

  13. It’s remarkable to go to see this web site and reading the views of all mates
    about this piece of writing, while I am also keen of getting experience.

Comments are closed.