function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Charni Wiggins 4Charni Wiggins 4 

Trigger - Mass email to Account with related records grouped

Fairly new to dev/apex. Just looking for some advice on how to start/structure my trigger as I'm struggling to get it started. 

We want to send emails when Sales Invoices are overdue, to an email address on the related Account (related via lookup from invoice to account). My first thought was to create the trigger from the Account, but can I access the records related via the lookup on Sales Invoice? I couldn't get the SOQL statement working, so thought to do it from Sales Invoice, but that makes no sense as I need to group the Sales Invoices together where there are more than one overdue for each account, which happens more than not. 

Any sugegstions greatly appreciated! 

Paul S.Paul S.
Charni - given you want to notify the customer of ALL past due invoices, have you given any thought to using batch apex instead?
Akhil ReddyAkhil Reddy
@Charni, 

BATCH - the best option 
This is a better use case to use batch and collections (our friends). Creat batch on the child, in this case Sales Invoice. Collect them in map<Id, List<Sales Invoice>> where Key is AccoutId and Value is List of all invoices that are overdue. 
karthikeyan perumalkarthikeyan perumal
Hello, 

Before sending mass email consider Salesforce Email limitation( sending from salesforce) . you may have big chance to  end up with Limit exceeded exception. design the trigger or Batch job according to this. 

Thanks
karthik