Skip to main content
New tool CRON Expression Builder — preview next run times before you schedule Apex. Open the builder →
Designing Salesforce AR automation using a central flow orchestrating email generation and file attachment.
Flow

Salesforce AR Automation: One-Click Chaser Email

Automate the repetitive work of chasing Accounts Receivable follow-ups with standard Salesforce tools. This guide builds a single-click solution in Flow that generates dynamic emails and attaches the relevant invoice documents.

Chasing accounts receivable with one click

The manual version of an AR follow-up is a chore at every step: check the external system, look up the contact, copy a template, paste the data in, then go hunting for the right invoice PDF. What you want instead is one button on the record, an Invoice or an Account, that writes a tailored chaser email, fills in the data, and attaches the correct Invoice PDF.

If you are held to standard Salesforce functionality, Salesforce Flow is the tool for it, specifically a Screen Flow or a Quick Action Flow.

Architecture for the one-click solution

The solution has to coordinate data retrieval, conditional logic for template selection, dynamic content generation, and file management.

1. Trigger mechanism (Quick Action)

The one-click part comes from a Quick Action on the relevant object, say Invoice__c or Account. Point it at a Screen Flow, or a Record-Triggered Flow if the follow-on work can happen asynchronously. I would take the Screen Flow: it gives the user control over template selection, which you will want.

2. Data orchestration and retrieval

The Flow has to work out who to email and which Invoice PDF to attach.

What this assumes:

  • You have a custom object representing Invoices, for example Invoice__c.
  • The Invoice is related to the Account and to the primary Contact.
  • Invoice PDFs are stored as Files related to the Invoice__c record.

The steps, in a Screen Flow:

  1. Get Record for the invoice or account data. Pull every field you will need: billing address, due date, amount, contact email.
  2. Decision element for days past due. Work out the aging bucket, 15 days late, 30 days late, 60 days late, and let it drive the template and the subject line.
  3. Screen element for template selection, optional. Show the user the template the Flow picked and let them confirm it or override it. That covers the original ask: "Perhaps we could pick the template type manually."
  4. Get Records for the file. This is the step that decides whether the whole thing works. Query ContentDocumentLink on the trigger record ID and filter it down to the invoice attachment you actually want.
-- Pseudo-SOQL within the 'Get Records' element configuration for Files
SELECT ContentDocumentId FROM ContentDocumentLink 
WHERE LinkedEntityId = :TriggerRecordId 
AND ContentDocument.FileExtension = 'pdf' 
AND IsDeleted = FALSE
ORDER BY CreatedDate DESC
LIMIT 1

Store the ContentDocumentId that comes back.

3. Dynamic email generation

Standard Flow Email Alerts will not merge anything more complicated than simple fields, so the email has to come from the SendEmail action or be composed inside the Flow itself, using what the Decision element worked out in Step 2.

  • With Classic or Lightning email templates: pass the EmailTemplate fields when you call SendEmail, and make sure the template supports the merge fields you need. For basic AR notices this is usually the simpler road.
  • With content that varies by aging bucket: build the body and subject in Assignment elements fed by your Decision elements, then call SendEmail with the plain text or HTML body you constructed in the Flow.

4. Attaching the PDF

Attaching files dynamically from a Flow means working with ContentDocumentLink.

If Step 2 got you a ContentDocumentId, create a ContentDocumentLink record joining that document to whatever you are targeting: the Email Message record, if you are drafting an activity, or the Invoice record if that is the linkage you want.

The catch is on the send itself. The native SendEmail action is limited at attaching arbitrary Salesforce Files by an ID you picked up mid-flow, so if the email really has to go out with the PDF on it, what tends to hold up is generating an EmailMessage record yourself with the ContentDocumentId references on it, or making sure the File is linked correctly to an Activity the Flow created. Apex or an intermediary step usually does that work.

There is a simpler version. If the invoice is a standard attachment on an Email Message the Flow created, link the ContentDocumentId through the ContentDocumentLink on that EmailMessage record. For a Draft Email activity, complex file merging may still want Apex help, though basic attachment linking has been getting easier in recent Flow releases.

When Flow runs out of road: Invocable Apex

If the attachment mechanism for dynamic Files turns out to be too cumbersome inside declarative Flow limits, put an Invocable Apex Method behind the Flow. Apex gets at Messaging.MassEmailMessage and the ContentDocumentLink APIs directly, so the right PDF goes on the email however awkward the criteria are.

Apex takes the InvoiceId and TemplateType from the Flow, queries the file ID, builds the email with whatever template data it needs, and sends it. The user still only clicks once.

Key takeaways

A Screen Flow gives you the user interaction that template selection needs, and runs on the spot. The hard part is reliably finding and attaching the right PDF File through ContentDocumentLink. Decision elements drive the email content off the aging bucket. And if native Flow attachment cannot handle dynamic files, move the email construction and send into an Invocable Apex class called from the Flow.

Originally reported by reddit.com

Newsletter

One email every Tuesday

New guides, tool updates, and the release-note changes that break things.

No spam. Unsubscribe in one click.

Comments

Loading comments...

Leave a Comment