7

I am trying to automate the sending of sales invoices generated through financial force. I would like a one click process which fetches the outstanding invoices, bulk emails them and then marks them as sent.

The problem I have run up against is the bulk email service wont let me specify an invoice id for the WhatIds. The TargetObjectIds are a list of contacts and the WhatIds need to be the invoices so the template can successfully generate the attachment.

It seems like I can use the single email service to do this as it doesn't have the same restrictions on the WhatIds but I have hundreds to send at a time.

Does anyone know how this can be achieved or how Financial Force does it with their mass email service?

Thanks

EDIT: The reason I am doing this rather than using the FF bulk sender is because after a successful send I need to have the invoices marked as printed.

2
  • One for @Andy Fawcett! Nov 2, 2012 at 16:19
  • 4
    @techtrekker I was just talking to him on skype to distract him so I could answer first. Nov 2, 2012 at 16:37

1 Answer 1

7

I work for FinancialForce in their developer support team and the way we do it in our mass email system is with large numbers of the Messaging.SingleEmailMessage object. The Messaging.sendEmail method takes a list/array of these, and as far as I know the only limit on size is that there's a cap of 1,000 emails sent from apex, per day (unless you convince salesforce to raise that).

Sample code:

List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for(integer i = 0; i < 15; i++){
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    email.setSubject('email '+i+' of 15');
    email.setToAddresses( new List<String>{'[email protected]'} );
    email.setPlainTextBody('this message was generated from apex');
    emails.add(email);
}
Messaging.sendEmail(emails);

and look what happens:

The results

5
  • You can currently only send 10 emails per job/request so do you batch them as the previous commenter suggested?
    – ArthurGuy
    Nov 2, 2012 at 16:37
  • You certainly can send more than 10. You're limited to 10 calls of Messaging.sendEmail, but not 10 total emails sent. Pass in a list of, say, 20 emails into a single call and it will work properly. Basically, you have to bulkify your email the same way you bulkify your SOQL. Nov 2, 2012 at 16:38
  • OK, so rather than using the bulk email process you pass a load of single emails to the sender. Thanks
    – ArthurGuy
    Nov 2, 2012 at 16:40
  • I feel so dumb now, this parameter is indeed an array but all this time I was passing 1 email :D The "single" word is too suggestive I guess.
    – eyescream
    Nov 2, 2012 at 16:46
  • 1
    The docs could certainly be better to make it clear what the actual limits are. There's lots of talk of SOQL bulkification but no mention of email bulkification. Nov 2, 2012 at 16:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .