Tutorials
Document AI Tutorials
Mass property extraction from a Medical Form
overview in this tutorial, we will see how cloudfiles can be used with salesforce flow automations to extract multiple properties from documents using custom metadata each time a file is uploaded into salesforce or external storage, the following actions will occur the file is processed using cloudfiles document ai natural language query is executed through custom meadata to extract information from the document the extracted data is processed using salesforce apex 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 set up to process the uploaded files the first flow is triggered when the document is uploaded into salesforce 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 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 get the custom metadata in this tutorial, we use a custom metadata type called cloudfiles prompts to store fields and their corresponding prompts this approach keeps the flow consistent, regardless of how many fields are added each entry includes the field’s api name, type, and prompt use a get records element to fetch all created cloudfiles prompts records custom metadata is efficient, consumes minimal data, supports org to org transfers, and works in the background since custom metadata behaves like a standard object, you can retrieve records from it using get records action steps to add a new record to custom metadata go to setup (click the gear icon → setup ) in the quick find box, type "custom metadata types" and click it click on your custom metadata type (e g , cloudfiles prompts ) under the custom metadata details, click "manage records" click the "new" button to add a new record fill in the required fields label field api name c field type c (e g , date, string, integer) prompt c (or any other custom field) click save create a prompt collection once the cloudfiles prompts records have been successfully retrieved, we add a loop element to iterate through the output cloudfiles prompts record collection for each record in the loop,we create an apex defined variable var prompt to hold structured data such that {!var prompt id} < {!for every prompt field api name c} {!var prompt query} < {!for every prompt prompt c} then the populated var prompt is added into the apex defined collection variable varcoll prompt excecute queries in batch the previous steps consolidates all queries into a single collection, preparing them for batch execution using query document (batch) docid\ ok1o1lpb07zvaclnrbgvu in this step, we will actually use this action along with the collection variable from previous step to execute these queries this action takes in 2 inputs processed document id select triggering cldfs cloudfilesevent c {!get triggering event details documentprocessed processeddocumentid} prompts select varcoll prompt (the previously created query collection) the final action with the inputs is shown below process ai output using apex action once the ai output is received from the query (batch) action, an apex action is used to handle the data this apex class maps the ai generated prompt results to fields on medical form records using the custom metadata type cloudfiles prompts it loops through each record and its prompts, checks the field type, sets the field values, and updates the records in salesforce this is the apex code used to process the ai output public class cldfsdynamicfieldmapper { @invocablemethod( label='cloudfiles dynamic fields mapper' description='maps fieldvalues from the queryresult to the corresponding medical form record fields' ) public static void mapfieldvalues( list\<fieldmapperflowinput> flowinputlist ) { if (flowinputlist isempty()) return; list\<sobject> recordslist = new list\<sobject>(); list\<cloudfiles prompts mdt > cloudfilessettings = \[select field api name c , field type c , prompt c from cloudfiles prompts mdt ]; if (cloudfilessettings size() == 0) return; map\<string, cloudfiles prompts mdt> settingsmap = new map\<string, cloudfiles prompts mdt>(); for (cloudfiles prompts mdt setting cloudfilessettings) { settingsmap put(setting field api name c, setting); } for (fieldmapperflowinput flowinput \ flowinputlist) { list\<cldfs querydocumentprompt> prompts = flowinput queryprompts; id recordid = flowinput recordid; sobject record = recordid getsobjecttype() newsobject(); record put('id', recordid); for (cldfs querydocumentprompt prompt prompts) { cloudfiles prompts mdt setting = settingsmap get(prompt id); if (setting == null) continue; string outputstring = prompt result; if (outputstring == '0') continue; string datatype = (setting field type c) touppercase(); if (datatype == 'date') { record put(prompt id, date valueof(outputstring)); } else { record put(prompt id, outputstring); } } recordslist add(record); } update recordslist; } public class fieldmapperflowinput { @invocablevariable(required=true label='pass the output that was returned from the query document (batch) apex action') public list\<cldfs querydocumentprompt> queryprompts; @invocablevariable(required=true label='record id to update the field(s) of that particular record') public string recordid; } } to ensure that the above apex code can handle more data types like boolean, number, date , time, currency etc, the original conditional block shown below can be expanded with additional conditions for each supported type if (datatype == 'date') { record put(prompt id, date valueof(outputstring)); } else { record put(prompt id, outputstring); } see it in action now that everything is set up, you can test your flows when a medical form is uploaded as a salesforce file on the record, the properties are extracted and updated in the record 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