Skip to main content
La Trailblazer Community su Slack è ora in beta aperta: unisciti e connettiti in tempo reale. Unisciti a noi su Slack

Create a Record-Triggered Flow

Learning Objectives

After completing this unit, you’ll be able to:

  • Create a record-triggered flow that calls Document AI and updates the opportunity record.
  • Upload a PO to an opportunity record and see the fields auto-populate.
Note

Triggering a flow on the Content Version object is a pilot or beta service that is subject to the Beta Services Terms at Agreements–Salesforce.com or a written Unified Pilot Agreement if executed by Customer, and applicable terms in the Product Terms Directory. Use of this pilot or beta service is at the Customer’s sole discretion.

What the Flow Does

Let’s break down the flow into high-level steps.

  1. The flow triggers every time a new file is added to Salesforce.
  2. It loops through all records associated with the file and finds the opportunity record. Due to Salesforce Object Query Language (SOQL) constraints, the flow can’t find the opportunity record without looping through all records.
  3. It checks that an opportunity was found and that the uploaded file is a PDF. If not, the flow ends.
  4. It passes the file to the DocumentAICaller Apex class that calls Document AI and extracts details in raw JSON.
  5. If Document AI fails, the flow sends a failure notification email.
  6. If Document AI succeeds, the flow passes the raw JSON to the PODetailsExtractor class that parses the JSON into clean fields.
  7. The flow updates the original opportunity record with the fields.

Next, let’s build the flow.

Configure the Start Element

The start element defines when the flow is triggered.

  1. Click the grid icon to open App Launcher.
  2. Search for and select Flows.
  3. Click New.
  4. Click Record-Triggered Flow.
  5. In the Configure Start panel, for the Object, search for and select Content Version (Beta).
  6. Under Configure Trigger, leave A record is created.
  7. For the Condition Requirements, select All Conditions Are Met (AND).
  8. For the field, search and select Version Number.
  9. For Operator, leave Equals.
  10. For Value, enter 1.

Entering 1 makes sure that you only process the original upload of the file. If you didn’t have this filter, the flow would trigger if the same PDF was re-uploaded or updated.

  1. Under Optimize Flow, leave Actions and Related Records.
  2. Toggle Add Asynchronous Path.

You need an asynchronous path because you make HTTP callouts to Document AI, and elements on the Run Immediately path can’t make callouts. You configure the rest of the flow in the asynchronous path.

Check the File Type

While Document AI supports PDFs and images, for the purposes of this exercise, let’s limit it to PDFs only.

  1. On the Flow Canvas, under Run Asynchronously, click Add element.
  2. Search for and select Decision.
  3. Name the element Is PDF?. Make sure the API Name is Is_PDF.
  4. For the Outcome Label, enter Yes - Continue. Make sure the API Name is Yes_Continue.
  5. Leave All Conditions Are Met (AND).
  6. For the Resource, click Triggering ContentVersion, then search for and select File Type.
  7. Leave Equals as the Operator.
  8. For the Value, enter PDF.
  9. Click the Default Outcome tab.
  10. For the Label, enter No - Skip.
  11. Back on the Flow Canvas, under No - Skip, click Add element.
  12. Search for and select End.

Now, the flow will only process the file if it’s a PDF.

Get Records Linked to the Triggering File

This element gets every record linked to the triggering file. Later, you loop through these records and find the opportunity record.

  1. Under Yes - Continue, click Add element.
  2. Search for and select Get Records.
  3. Name the element Get All Content Document Links. Make sure the API Name is Get_All_Content_Document_Links.
  4. For Data Source, leave Salesforce Object.
  5. For Object, search for and select Content Document Link.
  6. For Filter Conditions, leave All Conditions Are Met (AND).
  7. For Field, search for and select Content Document ID.
  8. For Operator, leave Equals.
  9. For Value, click Triggering ContentVersion, then search for and select the second Content Document ID with the text type.
  10. For Sort Order, leave Not Sorted.
  11. For How Many Records to Store, click All records.
  12. For How to Store Record Data, click Automatically store all fields.

Create an OppId Variable

This variable will hold the opportunity record ID that you find later.

  1. Open the Toolbox by clicking the icon Toggle toolbox in the upper left corner.
  2. Click New Resource.
  3. For the Resource Type, click Variable.
  4. For the API Name, enter OppId.
  5. For the Data Type, click Text.
  6. Leave Available for input and Available for output unchecked.
  7. Click Done.

Find the Opportunity Record

These elements loop through every Content Document Link and find the opportunity record.

  1. Under Get All Content Document Links, click Add element.
  2. Search for and select Loop.
  3. Name the element Loop Through Links.
  4. Make sure the API Name is Loop_Through_Links.
  5. For Collection Variable, click Content Document Link from Get All Content Document Links.
  6. For Direction, leave First item to last item.

Next, configure the element that decides if the link is an opportunity. Opportunity IDs all start with 006.

  1. Under For Each, click Add element. Search for and select Decision.
  2. Name the element Is Link an Opportunity?
  3. Make sure that the API Name is Is_Link_an_Opportunity.
  4. For the Outcome Label, enter Yes - Is Opportunity. Make sure the API Name is Yes_Is_Opportunity.
  5. For the Condition Requirements, leave All Conditions Are Met (AND).
  6. For Resource, click Current Item from Loop Through Links, then Linked Entity ID.
  7. For Operator, click Starts With.
  8. For the Value, enter 006.
  9. Click Add Condition.
  10. For Resource, select OppId.
  11. For Operator, click Is Null.
  12. For Value, search for and select True.

Purchase orders are unique to one opportunity, so multiple matches shouldn’t happen. If there are multiple matches, it’s a mistake. This filter addresses that mistake by keeping the first match and not overwriting the OppId.

  1. Under Outcome Order, click Default Outcome.
  2. For the Label, enter No - Continue Loop.

Ensure the No path eventually connects back to the Loop element. This makes sure that the flow continues looping through links after it finds a non-opportunity link. It should be connected by default.

Next, set the OppId.

  1. Under Yes - Is Opportunity, click Add element.
  2. Search for and select Assignment.
  3. For Label, enter Set Opp Id.
  4. Make sure the API Name is Set_Opp_Id.
  5. For Variable, search for and select OppId.
  6. For Operator, leave Equals.
  7. For Value, select Current Item from Loop Through Links, and Linked Entity ID.

The loop continues looping through until you’ve finished looping through all links. Then, the flow checks if it found an opportunity. If it doesn't find an opportunity, the flow ends.

  1. Under After Last on the right side of the Loop element, click Add element.
  2. Search for and select Decision.
  3. Name the decision Has Opportunity Link? Make sure the API Name is Has_Opportunity_Link.
  4. Name the outcome Yes - Process File. Make sure the API Name is Yes_Process_File.
  5. Leave All Conditions Are Met (AND).
  6. For the Resource, select OppId.
  7. For the Operator, select Is Null.
  8. For the Value, select False.
  9. Click the Default Outcome tab.
  10. Name the outcome No - Skip.
  11. Under the No - Skip outcome you just created, click Add element, then search for and select End.

Call Document AI

Next, the Apex action authenticates, fetches the file, calls Document AI, and returns the extracted raw JSON.

  1. Under Yes - Process File, click Add element.
  2. Search for and select Action.
  3. Search for and select Run Document AI Image/File Recognition with Schema Configuration.
  4. Name the element Call Document AI. Make sure the API Name is Call_Document_AI.
  5. Locate the Consumer Key, Consumer Secret, and domain URL from Unit 1. If you forgot to save them, go back to unit 1, locate them, and come back to this step.
  6. For Client ID, paste the Consumer Key.
  7. For the Client Secret, paste the Consumer Secret.

You’re pasting these values directly for demo purposes. In real situations, these values are pulled from a Named Credential or Custom Metadata Type. Learn more about Named Credentials.

  1. For the Content Document Id, select Triggering ContentVersion, then search for and select the second option for Content Document ID.

  1. For the Org Domain, paste the domain URL that you saved in Unit 1. Make sure the URL contains https:// and that there are no extra spaces before or after the URL. Pasting the exact URL is very important; the flow won’t run with an incorrect URL.
  2. For the IDPConfigId, enter po_config. This is the name of the Document AI configuration you created in Unit 2.
  3. Leave the LLM Model blank.
  4. Leave the other options as the default.

Next, confirm that you successfully extracted details.

  1. Under Call Document AI, click Add element, then search for and select Decision.
  2. Name the element Extraction Successful? Make sure the API Name is Extraction_Successful.
  3. Name the outcome Success. Make sure the API Name is Success.
  4. Leave All Conditions Are Met (AND).
  5. For the Resource, click Outputs from Call Document AI, then click Status.
  6. For the Operator, leave Equals.
  7. For the Value, enter Success.
  8. Click the Default Outcome tab.
  9. Name the outcome Failed.

If Document AI failed, send a notification email. For the purposes of this badge, you’ll send the email to yourself.

  1. Under Failed, click Add element, then search for and select Action.
  2. Search for and select Send Email.
  3. Name the element Send Failure Notification. Make sure the API Name is Send_Failure_Notification.
  4. For Recipient Addresses, enter your email. Leave CC Addresses and BCC Addresses blank.
  5. Under Configure Sender Details, for Sender Type, click CurrentUser.
  6. For Sender Email Address, enter your email.
  7. Under Configure Email Content, leave Compose Email Content.
  8. For the Subject, enter Document AI Extraction Failed.
  9. At the top of the Body field, click the View as Rich Text dropdown and change it to View as Plain Text.
  10. For the Body, enter the following.
Document AI extraction failed.
Error:          {!Call_Document_AI.ErrorMessage}
Opportunity Id: {!OppId}
File Id:        {!$Record.ContentDocumentId}
  1. Leave the rest of the options blank.

Parse the Extracted JSON

If Document AI succeeded, use the other Apex action to parse the raw JSON and transform it into structured fields: Amount, Close Date, Description, PO Number, and Payment Terms.

  1. Under Success, click Add element, then search for and select Action.
  2. Search for and select Extract PO Details from Document AI JSON.
  3. Name it Extract PO Details. Make sure the API Name is Extract_PO_Details.
  4. For Extracted Data JSON, click Outputs from Call Document AI, then click Extracted Data (JSON).
  5. Leave the rest of the options as the default.

Update Opportunity Record

Finally, update the opportunity with the extracted fields.

  1. Under Extract PO Details, click Add element.
  2. Search for and select Update Records.
  3. Name the element Update Opportunity Fields. Make sure the API Name is Update_Opportunity_Fields.
  4. Click Specify conditions to identify records, and set fields individually.
  5. For the Object, search for and select Opportunity.
  6. Leave All Conditions Are Met (AND).
  7. For Field, search for and select Opportunity ID.
  8. For Operator, leave Equals.
  9. For Value, search for and select OppId.
  10. Under Set Field Values for Opportunity Records, for Field, search for and select Amount.
  11. For Value, click Outputs from Extract PO Details, then search for and select Total Amount.
  12. Click Add Field.
  13. Repeat steps 10-12 until all following fields are added.

Field

Value

Close Date

Outputs from Extract PO Details > Close Date

Description

Outputs from Extract PO Details > Line Item Summary

PO Number

Outputs from Extract PO Details > PO Number

Payment Terms

Outputs from Extract PO Details > Payment Terms

  1. Click Save.
  2. For the Flow Label, enter PO Enrichment Flow. Make sure the API Name is PO_Enrichment_Flow.
  3. Leave the description blank.
  4. Click Save.

Test the Flow

  1. Click Debug.
  2. For the Path for Debug Run, click Run Asynchronously.
  3. For ContentVersion, click PO_NTO_2026.
  4. Click Run.
  5. Confirm that the flow ran without errors.
  6. Refresh the page, and click Activate. It might take a few minutes for the Activate button to appear.

Now your flow is ready to process POs and update opportunities automatically!

See the Flow in Action

Let’s see the flow in action by adding a PO to an opportunity.

  1. From the App Launcher, search for and select Opportunities.
  2. Click Northern Trail Outfitters. If you don’t see it, click Recently Viewed and click All Opportunities.
  3. Click the Details tab. Notice the values in Amount, Close Date, Description, PO Number, and Payment Terms. Most are blank.
  4. In the Related tab, under Notes & Attachments, click Upload Files.
  5. Upload the sample PO you saved in Unit 2. It should be named PO_NTO_2026.
  6. Click Done.
  7. Wait a few seconds, then refresh the page.

Now the fields are updated with details extracted from the PO! If you don’t see updated fields, you likely input the URL incorrectly in step 9 of Call Document AI. Go back and try again.

Wrap Up

In this badge, you learned how to use Document AI to extract details from unstructured documents. You learned how Apex and flow work with Document AI to enrich records. Now NTO can intelligently process POs and automatically update records, saving valuable time and keeping their opportunities updated with the latest data.

Resources

Condividi il tuo feedback su Trailhead dalla Guida di Salesforce.

Conoscere la tua esperienza su Trailhead è importante per noi. Ora puoi accedere al modulo per l'invio di feedback in qualsiasi momento dal sito della Guida di Salesforce.

Scopri di più Continua a condividere il tuo feedback