You are here:
Sample Flow to Extract and Store Text in Records
Create a flow in Flow Builder with the Intelligent Form Reader Flow actions to extract text from uploaded documents or based on one or more content document IDs, and to get the active OCR template details. Then use Apex to create records for storing the extracted text.
Required Editions
| Available in: Lightning Experience |
| Available in: Financial Services Cloud, Health Cloud, and Public Sector Solutions |
| Intelligent Form Reader is available for an additional cost with the Intelligent Form Reader add-on license. |
| User Permissions Needed | |
|---|---|
| To open, edit, or create a flow in Flow Builder: | Manage Flow |
- From Setup, in the Quick Find box, enter Process Automation, and then select Flows.
- Click New Flow.
-
Select Screen Flow, and then click
Create.
Note You can select a flow type that suits your requirement. -
Create the required resources.
-
To store the content document ID, create a variable of Text type with the label as
Content Document ID.
If one or more content document IDs are entered, this variable stores a content document ID. If one or more documents are uploaded, this variable stores the ID of the content document currently in a loop that’s added to repeat the operation for each content document ID.
- To store the OCR template ID currently in a loop that’s added after the Fetch Active OCR Templates action, create a variable of Text type with the label as OCR template ID.
-
To store the content document ID, create a variable of Text type with the label as
Content Document ID.
-
For users to upload documents or specify content document IDs, add a Screen element,
and enter the label and API name for the element.
- For users to upload documents, drag the File Upload component to the screen canvas, and enter the label and API name for the component.
- For users to specify content document IDs, drag the Text component to the screen canvas, and enter the label and API name for the component.
-
To determine whether documents were uploaded or content document IDs were specified,
add a Decision element.
- Enter the label and API name for the element.
- Add the condition as content document ID equals true.
If the condition is met, content document IDs were specified. Else, documents were uploaded. -
If content document IDs were specified, add an Assignment element.
- Enter the label and API name for the element.
- Set the Content Document ID variable’s value from the specified content document IDs.
-
If documents were uploaded, repeat the operation for the content document ID associated
with each uploaded document.
- Add a Loop element, enter the label and API name for the element, and select the variable that stores the content document IDs of the uploaded files.
- Add an Assignment element, enter the label and API name for the element, and set the Content Document ID variable’s value as the ID of the current item from the loop.
-
To initiate text extraction for the uploaded documents, add an Action element.
- From the Category section, select Intelligent Document Processing.
- Find and select the Initiate Text Extraction action.
- Enter a label, an API name, and a description for the action.
- Specify the text variable that stores the content document IDs.
- If necessary, specify the range of the pages of the document that you want to extract information for.
- If necessary, specify the name of the OCR service that extracts text from the document.
- Click Done.
-
To show the details of the documents that text extraction has been initiated for, add a
Screen element, and enter the label of the API name for the element.
- To show the extracted information, drag the Text component to the screen canvas, and enter the label of the API name for the component.
-
In the Text component, insert the resource that stores the output of the Initiate
Text Extraction action.
The output contains the OCR document scan result ID and page number for each page for the specified content document ID or IDs that the text extraction is initiated for.
-
To get the text extracted from the uploaded documents, add an Action element.
- From the Category section, select Intelligent Document Processing.
- Find and select the Fetch Extracted Text action.
- Enter a label, an API name, and a description for the action.
- Specify the text variable that stores the content document ID.
- If necessary, specify the range of the pages of the document that you want to fetch the extracted information for.
- Click Done.
-
To show the details of the text extracted from documents, add a Screen element, and
enter the label of the API name for the element.
- To show the extracted information, drag the Text component to the screen canvas, and enter the label of the API name for the component.
-
In the Text component, insert the resource that stores the output of the Fetch
Extracted Text action.
The output contains the extracted text, bounding box coordinates, and the extraction confidence.
-
To get the details of the active OCR templates in the Salesforce org, add an Action
element.
- From the Category section, select Intelligent Document Processing.
- Find and select the Fetch Active OCR Templates action.
- Enter a label, an API name, and a description for the action.
- Click Done.
-
Repeat the operation for each OCR template ID that’s fetched by the Fetch Active OCR
Templates action.
- Add a Loop element, enter the label and API name for the element, and select the output of the Fetch Active OCR Templates as the variable.
- Add an Assignment element, enter the label and API name for the element, and set the OCR Template ID variable’s value as the ID of the current item from the loop.
-
To show the details of all the active OCR templates, add a Screen element, and enter
the label of the API name for the element.
- To show the active OCR templates, drag the Text component to the screen canvas, and enter the label of the API name for the component.
-
In the Text component, insert the resource that stores the output of the Fetch
Active OCR Templates action.
The output contains the details of all the active OCR templates.
- Save your changes, and then activate the flow.
- After you get the details of the extracted text and the active OCR templates, use Apex to store the extracted text in the target object fields that are mapped in the corresponding templates.
Example
Let’s assume that these are the OCR document scan result details.
{
"keyValuePair" : [ {
"key" : {
"confidence" : 85.27661895751953,
"text" : "a Employee's social security number",
"label" : null,
"language" : null,
"polygon" : [ {
"x" : 0.30966568,
"y" : 0.15110396
}, {
"x" : 0.47195905,
"y" : 0.15096205
}, {
"x" : 0.4719769,
"y" : 0.16660306
}, {
"x" : 0.30968243,
"y" : 0.16674507
} ]
},
"value" : {
"confidence" : 85.27661895751953,
"text" : "123-45-6789",
"label" : null,
"language" : null,
"polygon" : [ {
"x" : 0.3385959,
"y" : 0.16764826
}, {
"x" : 0.43139917,
"y" : 0.16756704
}, {
"x" : 0.43141937,
"y" : 0.18554746
}, {
"x" : 0.33861536,
"y" : 0.18562874
} ]
}
}],
"sdkresponse" : {
"statusCode" : 200,
"successful" : true
},
"version" : "1.0"
}
Use Apex to store the extracted text in the target object and its record type fields that are mapped in the corresponding OCR templates.
Here’s a sample of the way in which you can invoke the Apex code to create an Account record for storing the extracted text details.
Map<String, String> requiredFields = new Map<String, String>();
requiredFields.put('Name', 'demoAccountRecord');
TransformDocument.transform('48tRM0000004CreYAE', 'a Employee\'s social security number',
'Account', requiredFields);Here’s the code.
public class TransformDocument {
private static final String KEY_VALUE_PAIR = 'keyValuePair';
private static final String TEXT = 'text';
private static final String KEY = 'key';
private static final String VALUE = 'value';
public static void transform(String templateId, String key, String targetObjectApiName, Map<String, String> requiredFields) {
// Get the OcrDocumentScanResult (ODSR) response for document uploaded in the template
String odsrResponse = getOdsrResponseFromTemplateId(templateId);
// Pass key for which value should be fetched
String value = getValue(odsrResponse, key);
// save the fetched value in target object
saveExtractedValuesInTargetObject(value, targetObjectApiName, requiredFields);
}
/**
* The following method gets the OcrDocumentScanResult (ODSR) response for the document uploaded in the template
*
* It queries the objects in the following sequence:
* Ocr Template -> Ocr Sample Document -> Content Asset -> Content Document -> Ocr Document ScanResult (ODSR)
*/
private static String getOdsrResponseFromTemplateId(String templateId) {
List<OcrTemplateSampleDocument> ocrTemplateSampleDocs = [SELECT OcrSampleDocumentId, OcrTemplateId FROM OcrTemplateSampleDocument WHERE OcrTemplateId =: templateId];
String sampleDocumentId = ocrTemplateSampleDocs.get(0).OcrSampleDocumentId ;
List<OcrSampleDocument> ocrSampleDocs = [SELECT ContentAssetId FROM OcrSampleDocument WHERE Id =: sampleDocumentId];
String contentAssetId = ocrSampleDocs.get(0).ContentAssetId;
List<ContentAsset> contentAssets = [SELECT ContentDocumentId FROM ContentAsset WHERE Id =: contentAssetId];
String contentDocumentId = contentAssets.get(0).ContentDocumentId;
List<OcrDocumentScanResult> ocrScanResults = [SELECT ExtractedValues FROM OcrDocumentScanResult WHERE ContentDocumentId =: contentDocumentId];
return ocrScanResults.get(0).ExtractedValues;
}
/**
* This method returns the value corresponding to the key in the response
*/
private static String getValue(String response, String key) {
Map<String, Object> jsonMap = (Map<String, Object>) JSON.deserializeUntyped(response);
List<Object> keyValuePairList = (List<Object>) jsonMap.get(KEY_VALUE_PAIR);
List<Map<String, Object>> keyValuePairMapList = new List<Map<String, Object>>();
for (Object keyValuePair : keyValuePairList) {
Map<String, Object> keyValuePairMap = (Map<String, Object>) keyValuePair;
keyValuePairMapList.add(keyValuePairMap);
}
String value = '';
for (Map<String, Object> keyValuePair : keyValuePairMapList) {
Map<String, Object> keyMap = (Map<String, Object>) keyValuePair.get(KEY);
if (keyMap.get(TEXT).equals(key)) {
Map<String, Object> valueMap = (Map<String, Object>) keyValuePair.get(VALUE);
value = (String) valueMap.get(TEXT);
break;
}
}
return value;
}
/**
* This method creates a new record of objectType target object and saves values in one of
* the fields of the target object.
* Since some target objects require mandatory fields for a record to be created,
* we use requiredFields map that has keys as mandatory fields' API names
* and values as per the usecase requirement.
*/
private static void saveExtractedValuesInTargetObject(String value, String objectType, Map<String, String> requiredFields) {
SObject newRecord = (SObject) Type.forName(objectType).newInstance();
// Required field for record-creation
for (String key : requiredFields.keySet()) {
String val = requiredFields.get(key);
newRecord.put(key, val);
}
newRecord.put('Description', value);
insert newRecord;
}
}Did this article solve your issue?
Let us know so we can improve!

