Overview

In this tutorial, you will build a CloudFiles Document AI workflow that renames identity documents after the Contact they belong to. Each time a file is uploaded to a Contact, a record-triggered flow sends it to the workflow, which:

  • Classifies the file as a passport, a driving license or another document.
  • Builds a new name from the document type and the Contact's name, such as Passport - Erika Mustermann.
  • Renames the file where it is stored: in Salesforce Files, or in external storage such as SharePoint when it was uploaded through the CloudFiles widget.
Files classified as anything else keep their names.

A driving license uploaded to a Contact through the CloudFiles widget, renamed after the Contact a few seconds later

Pre-requisites

Before you start, make sure that:

  • CloudFiles Document AI is installed and connected, with an active subscription or trial.
  • The CloudFiles Event Mode setting is set to Custom Object, so that uploads create CloudFiles Event records a flow can trigger on.
  • To rename files in external storage, the storage is connected to CloudFiles and the Contact record page has the CloudFiles record widget with a folder attached to the Contact. Salesforce Files need neither.

CloudFiles Settings with the CloudFiles Event Mode set to Custom Object

Build the Workflow

The workflow does the classifying and the renaming. Open the CloudFiles app, click the Document AI tab and choose Workflows. For everything a workflow can do, see Workflows.

Create the Workflow and its Launch Form

Click New workflow, name it Classify and Rename Files and leave the review policy on Straight through, so files are renamed without a person approving each run. Under When this runs, it starts with, rename the document slot to uploaded_file and keep One document. Then click Add a field four times and add these Text fields, each Required. The Salesforce flow fills all of them.

Field nameLabelWhat the flow sends
contact_idContact IdThe Contact the file was uploaded to
file_idFile IdThe Content Document Id of a Salesforce file, or the external file's id
libraryLibrarysalesforce for a Salesforce file, or the storage library, such as sharepoint
drive_idDrive IdThe external drive's id, or none for a Salesforce file
Creating the Classify and Rename Files workflow and its launch form
The launch form with the uploaded_file slot and the four text fields

Find the Contact

Click Add the first step and choose Find a Salesforce record. Name it Find the contact and choose the Contact object. Add the condition Id equals the trigger.contact_id chip, keep Only the first record, and add Name under Return fields. Later steps read the name as sf_getRecords_1.record.Name.

A Find a Salesforce record step returning the Contact's Name

Classify the Document

Add a Classify step named Classify the document with the labels Passport, Driving License, Other. The step always returns one of these labels, exactly as typed, and the label becomes the start of the new file name.

A Classify step with the labels Passport, Driving License and Other

Skip Other Documents

Add a Decision step named Skip other documents. Click Add branch, set the left value to the ai_classify_1.category chip, keep equals, type Other and leave then go to on End run. Every other file takes the no-match route to the next step.

A Decision step that ends the run when the document is classified as Other

Build the New File Name

On the no-match path, add a Set a value step named Build the new file name and add three assignments. Type file_name as the target of each; the first assignment creates the variable and the next two add to it.

Assign toOperatorValue or ref
file_nameequalsthe ai_classify_1.category chip
file_nameadd - (a space, a hyphen and a space)
file_nameaddthe sf_getRecords_1.record.Name chip
A Set a value step that builds file_name from the document type and the Contact name

A passport uploaded to Erika Mustermann gets the name Passport - Erika Mustermann. Later steps read it as self.file_name.

Branch on Where the File is Stored

Add a Decision step named Where is the file stored?. Click Add branch, set the left value to the trigger.library chip, keep equals and type salesforce. The branch handles Salesforce Files, and the no-match route handles files in external storage.

Adding a Decision step that checks whether the library is salesforce

For Salesforce Files

On the branch_1 path, add Update Salesforce record named Rename the Salesforce file. Choose the Content Document object, set Record to update to the trigger.file_id chip, and under Field map map Title to the self.file_name chip from Variables.

An Update Salesforce record step that sets the Content Document Title to file_name

Always insert a value by clicking its chip. A reference typed by hand with a typo is saved as plain text, and the step then writes that text instead of the value.

For External Files

A workflow renames an external file by running a Salesforce flow that calls the CloudFiles Set Resource Property action. Build that flow first, because a workflow can only run a flow that is active.

In Setup, create an Autolaunched Flow (No Trigger) named CloudFiles: Rename a file in storage. Create four Text variables available for input, library, driveId, fileId and newName, and an Apex-defined variable Resource of class cldfs__Resource. Add an Assignment named Point at the file with these rows.

VariableOperatorValue
Resource > libraryEqualslibrary
Resource > driveIdEqualsdriveId
Resource > idEqualsfileId
Resource > typeEqualsfile
The Point at the file assignment that fills the Resource variable

Then add the CloudFiles: Set Resource Property action named Rename the file. Set Resource to Resource and add one property with the name name and the value {!newName}. Save the flow and activate it.

The Set Resource Property action setting the name property to newName

To change a SharePoint file's Name, the Set Resource Property action must update the name and nothing else. To update other columns as well, use a second Set Resource Property action for them.

Back in the workflow, on the no-match path of Where is the file stored?, add Run a Salesforce Flow named Rename the file in storage and choose CloudFiles: Rename a file in storage. The step lists the flow's input variables; fill them as below. These boxes take a reference as text, so clicking a chip and typing the reference give the same result.

Flow inputValue
driveIdtrigger.drive_id
fileIdtrigger.file_id
librarytrigger.library
newNameself.file_name
The Run a Salesforce Flow step passing the file location and file_name to the rename flow

CloudFiles keeps the file's extension, so scan.jpg becomes Driving License - Erika Mustermann.jpg.

Test and Publish the Workflow

To test the Salesforce Files path, upload a sample passport to a test Contact's Files and copy its Content Document Id, which starts with 069. Click Test run, upload the same sample, and enter the Contact Id, the Content Document Id, salesforce as the library and none as the drive id. Click Start run. A test run writes to Salesforce exactly as a real run does, so the file is renamed. The run shows what each step produced, with a confidence score for every AI answer. When the results look right, click Publish v1.

A test run on a sample passport that renames the Salesforce file

Then go back to the Workflows list and click the Copy workflow ID icon in the Run column. The Salesforce flow uses this id to start the workflow.

The Workflows list with the published Classify and Rename Files workflow

The flow needs the workflow ID from the Copy workflow ID icon. The Config API Name in the same list is used for moving configurations between orgs and is not accepted as a Workflow Id.

Start the Workflow from a Salesforce Flow

A record-triggered flow starts the workflow with the CloudFiles: Run Doc AI Workflow action whenever a file is uploaded to a Contact. It listens for two events that CloudFiles records on the CloudFiles Event object: Salesforce File Attached when a file is attached to the Contact in Salesforce Files, and File Uploaded when a file is uploaded to external storage through the CloudFiles widget. One Decision sends each event down its own path, and both paths end at the same action.

The finished record-triggered flow with a Salesforce file path and an external file path

Configure the Trigger

Create a Record-Triggered Flow on the CloudFiles Event object, triggered when A record is created and optimized for Actions and Related Records. Choose Custom Condition Logic Is Met with the logic 1 AND (2 OR 3) and these conditions: Object Type equals Contact, Type equals salesforce-file-attached, and Type equals file-uploaded. Add a Run Asynchronously path and build everything below on it, because the workflow action makes an external callout that a record-triggered flow cannot make on the path that runs immediately.

The Start element on CloudFiles Event with the Object Type and Type conditions

Get the Event Details and Check the Upload

Add the CloudFiles: Get Event Details action with the triggering CloudFiles Event record as its input. Then add a Decision named Which upload is it? with an outcome Salesforce file for {!$Record.cldfs__Type__c} equals salesforce-file-attached, and name the default outcome External file.

Create these variables for the paths below: Resource of class cldfs__Resource, slotBinding and SlotBindings of class cldfs__DocumentSlotBinding, FieldValue_Single and FieldValue_Collection of class cldfs__FieldValue, and a Text variable varContactId. Tick Allow multiple values on SlotBindings and FieldValue_Collection.

Prepare the Inputs for a Salesforce File

On the Salesforce file path, add a Get Records element named Get the uploaded file on Content Document, with the condition Content Document ID In Outputs from Get event details > SalesforceFiles > ContentDocumentIds, storing only the first record. Then add an Assignment named Prepare the Salesforce file inputs with these rows.

VariableOperatorValue
Resource > libraryEqualssalesforce
Resource > idEqualsContent Document from Get the uploaded file > Content Document ID
slotBinding > resourceEqualsResource
slotBinding > slotEqualsuploaded_file
SlotBindingsAddslotBinding
varContactIdEqualsOutputs from Get event details > SalesforceFiles > ParentId

Then add three rows for each workflow field: FieldValue_Single > name Equals the field name, FieldValue_Single > value Equals the value, and FieldValue_Collection Add FieldValue_Single.

Field nameValue
contact_idvarContactId
file_idContent Document from Get the uploaded file > Content Document ID
librarysalesforce
drive_idnone

Prepare the Inputs for an External File

On the External file path, add an Assignment named Prepare the external file inputs. Every value comes from Outputs from Get event details > FileUploaded.

VariableOperatorValue
Resource > libraryEqualsFileUploaded > Library
Resource > driveIdEqualsFileUploaded > DriveId
Resource > idEqualsFileUploaded > Id
slotBinding > resourceEqualsResource
slotBinding > slotEqualsuploaded_file
SlotBindingsAddslotBinding
varContactIdEqualsFileUploaded > RecordId

Then add the same three rows for each field as on the Salesforce path, with these values.

Field nameValue
contact_idvarContactId
file_idFileUploaded > Id
libraryFileUploaded > Library
drive_idFileUploaded > DriveId
The Prepare the external file inputs assignment

The slot name and the field names must match the names declared in the workflow exactly.

Run the Workflow

Below the two paths, add the CloudFiles: Run Doc AI Workflow action. Set Documents to {!SlotBindings}, Fields to {!FieldValue_Collection}, Record Id to {!varContactId}, and Workflow Id to the id you copied. Save the flow and activate it.

The Run Doc AI Workflow action with its four inputs

The action returns as soon as the run is accepted, and the workflow renames the file on its own a few seconds later. The flow does not receive the workflow's results.

Check the Run on the Doc AI Run Object

Every run started from a flow is recorded on the CloudFiles Doc AI Run object. Open the CloudFiles DocAI Runs tab and choose the All Runs list view to see each run with its status. A run reads Completed when the workflow finished and Failed when it could not.

Open a run to see its status, the Contact it ran for in Parent Record Id, the document it started with, what it wrote and what each step produced. For an external file, What was written to Salesforce shows the rename flow it ran.

A completed run that classified a driving license and ran the rename flow

See it in Action

Open a Contact and upload a passport or driving license from the Files or Notes & Attachments related list. Wait a few seconds and refresh the page: the file is renamed after the Contact.

A passport uploaded to a Contact's Notes & Attachments and renamed after the Contact
Notes & Attachments on the Contact showing Passport - Erika Mustermann

Now upload a document through the CloudFiles widget on the same Contact. Wait a few seconds and click the widget's refresh icon: the file in external storage carries the new name.

The CloudFiles widget showing Driving License - Erika Mustermann.jpg in the Contact's SharePoint folder

Note: Flow executions and AI runs take a few seconds, so the new name does not show immediately. Wait briefly and refresh.

Congratulations! You have built an AI-powered file classifier that renames uploads in Salesforce Files and in external storage with a CloudFiles Document AI workflow and a Salesforce flow.