0

First time posting and I'm quite new to SF. I've ended up on this site a lot when Googling to help me with issues - so thanks!

On my org, we're uploading some opportunities via DataLoader.io that will then send emails out to external contacts.

This was done via process builder - the opportunity has a specific record type and stage for the process to be followed. While it does complete the first action for all the records(which is a simple record update), the 2nd action is to call an invocable method to send the email out. In this case it's only actioning the first 3 records in the upload for the invocable method.

Global class sendInvoiceSurvey {


    @InvocableMethod
    public static void sendInvoiceEmail(List<opportunity>InvoiceContact){

   Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
   mail.setTargetObjectId(InvoiceContact[0].Fee_Contact__c);
   mail.setTemplateId('00X24000000F1iy'); 

   mail.setSaveAsActivity(true); 
   mail.setWhatId(InvoiceContact[0].Id);
   mail.setBccSender(true);
   mail.setSenderDisplayName('INSP');

         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

} }

I'm really lost as to why this is only working the first 3 records. Are there any limits that is preventing other emails going out? Or an issue with my code/process?

Any help would massively appreciated!

  • Can you update your code in your question. It seems that all of your lines are commented because there were problems with the line breaks. – Martin Lezer Jan 23 '17 at 13:56
  • There's something I don't understand. In your function you receive a list of InvoiceContact. But you only send an email to the first record of your list. – Martin Lezer Jan 23 '17 at 14:14
  • Could this be where my issue is? So the process builder should work on creation of a new opportunity record, which I thought would make sense in only sending 1 email. – Niamh Jan 23 '17 at 14:30
2

As I said as comment, you don't loop through the list of InvoiceContact.

Here's the solution:

Global class sendInvoiceSurvey {


    @InvocableMethod
    public static void sendInvoiceEmail(List<opportunity>InvoiceContact){

   List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
   for(Opportunity opp : InvoiceContact){
   Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
   mail.setTargetObjectId(opp.Fee_Contact__c);
   mail.setTemplateId('00X24000000F1iy'); 

   mail.setSaveAsActivity(true); 
   mail.setWhatId(opp.Id);
   mail.setBccSender(true);
   mail.setSenderDisplayName('INSP');

   mails.add(mail);
   }

         Messaging.sendEmail(mails);

} }

With this code, you instanciate one email object for each of opportunity received and send all the emails.

  • Thanks so much - I can clearly see where I've gone wrong. – Niamh Jan 23 '17 at 14:45
  • You're welcome. Glad I could help. – Martin Lezer Jan 23 '17 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.