Tutorials
Document AI Tutorials

Classify and Rename Uploaded Files

Overview

In this tutorial, we will see how CloudFiles can be used with Salesforce flow automations to classify incoming docs and rename them appropriately. Each time a file is uploaded into Salesforce or external storage, the following actions will occur:

  • The file is processed using CloudFiles Document AI.
  • A Natural Language Query executes to classify the file as Passport or Driving License.
  • File is automatically renamed with a prefix based on the classification.

In order to setup this automation, you first need to have some pre-requisites. After this you can start setting up some query automations and updates to achieve your use-case. Check the sections below for more information.

Pre-requisites

Before starting with this tutorial, you need to ensure that CloudFiles Document AI is properly installed & configured in your system. Additionally, you also need to create some initial automations to process the files before you can query them. Check the 2 sections below carefully -

Installation & Configuration

Ensure the following before starting:

  1. CloudFiles Document AI Installation and Configuration are complete.
  2. You have an Active Subscription or Trial of CloudFiles Document AI.
  3. The CloudFiles Event Mode setting is set to Custom Object
Document image


Initial Automation Setup

There are 2 flows you will need to setup to process the uploaded files. The first flow is triggered when the document is uploaded into Salesforce or external storage. This flow sends the file to AI for processing. This might take a few seconds. Once the AI is done with processing, the second flow will trigger which will contain the processed document. You can then run queries on this processed document.

  • Flow 1: Send Document For Processing - Triggered when file is uploaded into Salesforce or external storage. Sends the file for processing which may take a few seconds to a minute.
  • Flow 2: Document Processing Complete - Triggered when the file has been processed by AI. Performs queries on the file to extract the data.

All information related to setting up these flows is given in the AI Flows - Initial Setup Guide article. Please go through this article carefully to set up these flows.

Once your flow setup is complete, your flows should look like the following.

Document image


Read the article on AI Flows - Initial Setup Guide carefully to setup both the flows. Once your initial flows are setup as shown above, you are ready to move to the next step.

Query Automation Setup

Now that you have the initial automation setup, we can take the document processed flow and extend it to query the document and update the required fields. In this section, we show how to query the processed document, check the results and perform the necessary updates.

Querying the document

In order to query the document, we will simply use the Query Document flow action. This action taken in a processed document ID and a text query as input. The processed document ID is available as output of the Get Event Details action. Here are the full inputs used in the image below.

Processed Document Id = Triggering cldfs__CloudFilesEvent__c" ( {!Get_Triggering_Event_Details.DocumentProcessed.ProcessedDocumentId} )

Query = Enter a clear Natural Language Classification Prompt such as:What type of File is this ? If this is a Passport Return "Passport", If this is a Driver's License Return "DL", If anything else Return "NA".

Document image


Checking the answers

The Query document action above outputs a text variable which is the full response of AI for the given query. You can use this text response along with flow branching logics to perform complex things. Below, we have used the response from AI to create 2 separate branches which we will use in the next step.

Document image


This step ensures the flow accurately identifies the type of document processed, allowing appropriate subsequent record updating actions.

Create File Name Formula

Now that we have the AI categorization and the file name, we will create a simple formula to concatenate the two together and create the final desired file name. Shown below is a text resource variable where this formula has been used. The file name is available as an output of the Get Event Details action : Get_Triggering_Event_Details > DocumentProcessed > File > name.

Document image


Updating File Name

Now that our flow knows which document is which type, we are simply going to append it's name with the AI output and and then update the file. Based on whether your file was uploaded in Salesforce or in external storage, we will use one of the following approaches to update the file name -

  1. For Salesforce Files - Simply update the Content Document Title using Update Records Action.
  2. For External Files - Use CloudFiles' Update Resource flow action to update external file name

Refer to the sections below based on your use-case.

For Salesforce Files

Here we will use the Salesforce Update Records action to update the title of the uploaded content document. The ID of the content document to be updated is provided in the output of Get Event Details : Get_Triggering_Event_Details > DocumentProcessed > File > id. The corresponding action is shown below.

Document image


For External Files

We will use the CloudFiles' Set Resource Property flow action to update the external file's name. In order to use this action, we first need a create an Apex-Defined variable of the class cldfs_Resource.

Document image


We need to assign some values to this variable using an Assignment action. These values help identify the location of the file in the external library. These may include the id, driveID, library & type of the resource. These values are available as outputs of the Get Event Details action.

  1. Var_Resource > driveId = Get Triggering Event Details > Document Processed > File > driveId
  2. Var_Resource > id = Get Triggering Event Details > Document Processed > File > id
  3. Var_Resource > library = Get Triggering Event Details > Document Processed > File > library
  4. Var_Resource > type = Get Triggering Event Details > Document Processed > File > type
Document image


Finally, we use this variable in the Set Resource Property action along with the desired file name to make the update. We need to provide the resource as an input and define a property value pair. Use 'name' as Property name and the formula we created above for the proeprty value. This is shown below.

Document image


When Updating a Resource's 'Name' Column/Property with Set Resource Property

  • To update the 'Name' column of a SharePoint Resource (File or Folder), you need to follow a special configuration:
    1. Separate action for 'Name'
      • You must create a separate Set Resource Property action for updating the 'Name' column.
      • Only the 'Name' column can be updated within this action.
      • No other columns (properties) can be updated in the same action when modifying the 'Name' column.
    2. Updating 'Name' and other columns
      • If you need to update both the 'Name' column and other columns simultaneously, you must use two separate actions:
        • One action should be dedicated solely to updating the 'Name' column.
        • A second action should update all other columns.

See it in Action

Now that everything is set up, you can test your flows. When a Passport or Driver's License is uploaded as Salesforce Files onto a Contact record, the corresponding Salesforce File should be renamed automatically.

Note: As Flow Automation executions and document processing via AI may take a short time, the field updates will not reflect immediately. To view the updates, wait briefly and refresh the page.

Flow 2 Debug

Whenever a file is processed using the Process Document using AI action, a Document Processed object record is published.

You can query these event object records to verify successful file processing and debug the flow.

Example SOQL Query:

To check Document Processed events sorted by the most recent:

SELECT Id, Name, CreatedDate, cldfs__Data__c FROM cldfs__CloudFilesEvent__c WHERE cldfs__Type__c = 'document-processed' ORDER BY CreatedDate DESC

You can check the Context and File Details in the Data Field of the record.

When a flow runs in debug mode and executes Query Document action, you can check the results and modify the queries if required.