Flow Actions
Doc AI Flow Actions

Query Document

Introduction

The CloudFiles: Query Document flow action enables to execute Natural Language Prompts on the data of the documents that have already been processed using Process Document using AI action.

Document image




What this action does

The CloudFiles: Query Document action allows you to execute a query (a natural language or English-language prompt) on the data of a processed document. The action processes the query and returns the result as a text output.

Imagine you are processing KYC files uploaded to Contact object records. Using this action, you can:

  1. Classify the uploaded document as a Driving License, Passport, or neither.
  2. Extract relevant information, such as the passport number, from the document.
  3. Update the corresponding fields on the Contact record.
  4. Classification Query:
    • Prompt: "Check the type of file. If it's a Driving License, return 1; if it's a Passport, return 2; if neither, return 0."
    • Based on the result (e.g., 2 for Passport), the flow decides the next steps.
  5. Decision Element:
    • Using the query result, the flow branches into specific paths for each document type.
  6. Follow-Up Queries:
    • For example, if the classification query returns 2 (Passport), the flow enters the "Is Passport" path and runs queries like:
      • "Extract the passport number."
      • "Extract the name on the passport."
Document image


By using multiple Query Document actions within a flow, you can build complex workflows that analyze and extract data from documents efficiently.

Input Parameters

Let us discuss each of the input parameters in detail, in the sections below.

Processed Document Id

Input the Processed Document Id of the file on which you wish to execute the Query.

The Processed Document Id is a unique identifier provided by CloudFiles that represents the data of a processed document.

How it is generated ?

How to retrieve it ?

  1. Use the Get Event Details flow action associated with the corresponding event to obtain the Id.
  2. Alternatively, if you need to execute queries on a Salesforce File later or on-demand:
    • Store the file's Processed Document Id in a custom Content Version field.
    • This allows you to query the document anytime, without relying on the event.

Note:

The same Processed Document Id can be reused multiple times to reference the document and execute queries as needed.

Query

Input the Query (as a plain text) specifying the prompt you wish to execute on the file identified by the input Processed Document Id.

You can directly pass a string as the query (e.g., "Extract the passport holder's name."). Alternatively, you can pass the query dynamically through a variable, formula etc. allowing to incorporate complex use cases.

The effectiveness of the CloudFiles: Query Document action heavily depends on the quality of the query or input prompt.

Tips for crafting effective prompts:

  • Be Specific: Avoid vague prompts like "Return Expiry Date of the Passport". Instead, use a more detailed query like: "Return the Expiry Date of the Passport in the format DD/MM/YYYY."
  • Define a return format statement as well which should cover all the expected outcomes in order to avoid garbage values and unprecedented results.
  • Test and Refine: You can test the quality of your queries and verify if they yield the desired results by using the Playground feature in the CloudFiles app.

Testing Queries in the Playground:

  1. Navigate to CloudFiles App > Document AI > Playground.
  2. Upload a sample file.
  3. Use the interactive Query side-component to experiment with different prompts against the file.
Document image


This iterative process helps you fine-tune your queries for optimal results before implementing them in your workflows.

Using AI for General Queries

Not all queries need to be executed on a specific document. If you want to run a general AI-based query that does not require document data, you can simply omit the Processed Document ID when executing the query.

This allows CloudFiles AI to process the query independently, focusing only on the text input you provide. This can be useful in scenarios where:

  • You have pre-extracted data from documents and need AI to structure or analyze it further.
  • You need AI to generate text-based responses, summaries, or formatted outputs based on user-defined parameters.

Example Use Case: Generating an Email from Extracted Data

Consider a scenario where you have processed an invoice document and extracted key details such as:

  • Invoice Number: INV-20240319-001
  • Invoice Amount: $2,500.00
  • Primary Contact Name: John Doe
  • Due Date: March 25, 2025

Now, instead of querying the document again, you want AI to generate a structured email using these extracted details.

How to Configure This in Salesforce Flow

You can create a Flow Text Formula Resource to format the extracted details into a structured prompt for AI processing.

📌 Query Example:

"Below are the identified details from an invoice: Invoice Number = {Invoice Number Query Result}, Invoice Amount = {Invoice Amount Query Result}, Primary Contact = {Client's Primary Contact Name Query Result}, Due Date = {Invoice Due Date Query Result}. Now, based on the provided details, compose a structured email to the primary contact. The email should: - Greet the contact by name. - Inform them about the invoice details in a professional and concise manner. - Politely request confirmation of the invoice details. - Provide a closing statement expressing appreciation and offer support if needed."

Generated Email Output (Expected Result):

Subject: Invoice Confirmation – INV-20240319-001 Dear John Doe, I hope you are doing well. We would like to confirm the details of your latest invoice: Invoice Number: INV-20240319-001 Invoice Amount: $2,500.00 Due Date: March 25, 2025 Please review the above details and let us know if everything is correct. If there are any discrepancies, kindly reply to this email at your earliest convenience. Looking forward to your confirmation. Thank you for your time! Best regards, [Your Name] [Your Company]

Output Parameters

The output of the CloudFiles: Query Document action is a Text (String) data type containing the result of the query as defined in the query prompt.

Where to find the output ?

  • The action's output is available in the flow resource's Action section.
  • The output is named based on the corresponding action label. For example, if the action is labeled as "Full Dimension Table Data", the output flow resource will be named "Text from Full Dimension Table Data".
Document image


See it in action

Here is the flow in action that we have used in this article as an example. In this flow we are providing a document to the AI and then prompting queries to determine whether the given document is KYC or not and classifying the type of KYC if it is either DL or passport.

Document image