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 cloudfiles document ai installation docid\ rfxciqrbejmpsmbczexam and configuration docid\ amnhmxwixyngnaxerbkwd are complete you have an active subscription or trial of cloudfiles document ai the cloudfiles event mode setting is set to custom object 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 docid\ sxn79rxu7gw7bb6ogpuix 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 read the article on ai flows initial setup guide docid\ sxn79rxu7gw7bb6ogpuix 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 docid\ dm3eh gzaocyoqd5y0r8d 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" 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 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 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 for salesforce files simply update the content document title using update records action 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 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 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 var resource > driveid = get triggering event details > document processed > file > driveid var resource > id = get triggering event details > document processed > file > id var resource > library = get triggering event details > document processed > file > library var resource > type = get triggering event details > document processed > file > type 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 when updating a resource's 'name' column/property with set resource property docid 0zoopbzobe0czqmm6spsf to update the 'name' column of a sharepoint resource (file or folder), you need to follow a special configuration 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 updating 'name' and other columns if you need to update both the 'name' column and other columns simultaneously, you must u se 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 docid 31ujx1ligtkwfkjuanbzt action, a document processed docid\ xrr pnbpocwwcmyg6qgk9 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 docid\ dm3eh gzaocyoqd5y0r8d action, you can check the results and modify the queries if required