Apex classes and instructions for building Document AI demos in Salesforce.
This repository contains the necessary Apex classes, configuration, and instructions to build a demonstration of Salesforce Document AI integrated with Data 360.
This pattern uses a pre-configured Document AI schema and a set of Apex classes to perform the callout and parse the complex JSON response, making the data simple to use in a Screen Flow.
- src/classes/ConfiguredSchemaExtractor.cls: A reusable, invocable Apex action. It calls the Data Cloud
extract-dataAPI using a Named Credential and the API name of a pre-configured schema. - src/classes/InsuranceCardJsonParser.cls: An invocable Apex action that parses an insurance card and outputs variables that can be used in a Flow (i.e.
insuredMember, 'insuranceCompany'). Can be used as a reference or template to configure other JSON parser classes specific to your configured schemas (Invoices,Loan Applications, etc).
Follow these steps to build the demo in your own org.
Confirm you have a Connected App that can call Data 360 APIs. Use the Connected App to create an Auth. Provider and a Named Credential to use in the Apex Class. If this is not available in your org, follow the instructions below.
- Create and download a self-signed certificate:
- Setup > Certificate and Key Management > click Create Self-Signed Certificate > label it as
DataCloudCert> key size: 2048 > Save > Download the cert (should have a .crt extension)
- Setup > Certificate and Key Management > click Create Self-Signed Certificate > label it as
- Create a Connected App
- Setup > Search
External Client Appsand click Settings under that > if it's not already enabled, enableAllow creation of connected apps&Allow access to External Client App consumer secrets via REST API> ClickNew Connected App- Name it
DataCloudAPI - Enter your email for contact email
- Check the box:
Enable OAuth Settings - Callback URL:
https://login.salesforce.com(we'll change this later, for now make sure it starts withhttps://) - Check the box:
Use digital signaturesand upload the cert from Step 1 - For
Selected OAuth Scopes- select the following:- Access the identity URL service (id, profile, email, address, phone)
- Manage user data via APIs (api)
- Manage user data via Web browsers (web)
- Full access (full)
- Perform requests at any time (refresh_token, offline_access)
- Access unique user identifiers (openid)
- Access Lightning applications (lightning)
- Access content resources (content)
- Manage Data Cloud Ingestion API data (cdp_ingest_api)
- Manage Data Cloud profile data (cdp_profile_api)
- Perform ANSI SQL queries on Data Cloud data (cdp_query_api)
- Access chatbot services (chatbot_api)
- Perform segmentation on Data Cloud data (cdp_segment_api)
- Manage Data Cloud Identity Resolution (cdp_identityresolution_api)
- Manage Data Cloud Calculated Insight data (cdp_calculated_insight_api)
- Access the Salesforce API Platform (sfap_api)
- Access all Data Cloud API resources (cdp_api)
- Access Einstein GPT services (einstein_gpt_api)
- Make sure
Require Secret for Web Server Flowis checked, leave the rest of the defaults on. - Save and continue
- Find and copy the
Consumer KeyandConsumer Secret, save them in your notes to use later. - Click
Manageon the connected app you just created. - Under
OAuth Policies, setPermitted UserstoAdmin approved users are pre-authorized> Save - Scroll down to the Profiles > add System Admin (assuming you're logged in as the Admin setting this up)
- Name it
- Setup > Search
- Create
Auth. Provider- Setup > Auth. Provider > click New > Name it
Data_Cloud_Auth_Provider> leave the URL suffix as default > Provider: Salesforce > copy over theConsumer KeyandConsumer Secretfrom the Connected App setup > Default scopes:cdp_api refresh_token api> Click Save - After you hit save, at the bottom of the screen you should see the "Salesforce Configuration" section. find and copy the
Callback URL. Go back to the connected app you created and step 2 (Setup > App Manager > DataCloudAPI > click on drop down menu and click Edit) > update the Callback URL from https://login.salesforce.com to the callback URL you copied from the Auth. Provider (Data_Cloud_Auth_Provider)
- Setup > Auth. Provider > click New > Name it
- Create the
Named Credential- Go to Setup >
My Domain> copy the link next to "Current My Domain URL", save in your notes to use later. - Setup >
Named Credential> In the New drop down, clickNew Legacy> label: Data Cloud API > leave name as default, make sure it's Data_Cloud_API as we'll reference this later in the Flow. > URL: paste yourMy Domain URLand make sure it starts withhttps://> Identity Type:Named Principal> Auth Protocol:OAuth 2.0> Auth Provider:Data_Cloud_Auth_Provider> Scope:cdp_api refresh_token api> Make sure this box is checked:Start Authentication Flowon Save > Click Save.- You will be redirected to a Salesforce login screen. Log in as the admin user you pre-authorized in the Connected App. This performs the one-time OAuth handshake to generate the initial tokens. After authenticating, you will see the Authentication Status is Authenticated.
- Go to Setup >
Now, API setup is complete. Nice work!
- Go to the Data Cloud app.
- Click the Process Content tab.
- Click Document AI in the left navigation.
- Click New. For this demo, you can choose
Without a Source Object - Create your schema (e.g.,
Commercial_Loan_Extraction_Schema). You can define the fields manually (likeLenderName,PrincipalAmount,PrepaymentPenalty) to match the fields in the.- See example schema with field names, field types, and prompt instructions:
Insurance Card Schema
- See example schema with field names, field types, and prompt instructions:
- Feel free to test your schema with sample documents to confirm results. Then Activate your schema.
Use the src/classes/ConfiguredSchemaExtractor.cls Apex Class provided in this repo. This is configured to call Document AI with the following inputs: Pre-configured Document AI Schema, File Id, and a Named Credential.
You'll need another Apex Class to parse the extracted JSON so that it can be used in your Flow (which can then create or update records, trigger automation, ground Prompt Templates, etc.). Your JSON parser class should mirror the schema you created in Document AI.
Watch out for fields with data types other than Text or Numbers so you can update them accordingly in the Apex Class. For example, applicationDate will be defined as a String in your Document AI schema. In your JSON parser Apex Class, you'll need to define it as a Date or DateTime field type so your Flow can process it as a date field or use it to assign values to other date fields.
For reference, see the example JSON parser below: src/classes/InsuranceCardJsonParser.cls
Create other components you'll want to use in the demo flow, such as Prompt Templates to leverage AI alongside the extracted data.
Example:
- Create a new Prompt Template
*Flex
*Configure a Text Input, name it something like
extractedJson-- this will infuse the Prompt Template with the extracted JSON from Document AI - Engineer your prompt to review the extracted JSON, analyze it, and provide your desired input.
See example below that summarizes a commercial loan agreement: src/prompts/LoanExecutiveSummary.txt
Use the following elements to create a Screen Flow:
-
Screen (Upload):
- Add a File Upload component.
- Set API Name to
file_uploader. - Set Accepted Formats to
.pdf,.jpg,.jpeg,.png - Set Allow Multiple Files to
False - Set Related Record ID to
{!recordId}(you can create the variable by clickingNew Resource> Resource Type:Variable> Data Type:Text> check the box forAvailable for Input).
-
Loop:
- Loop through the Output from the
file_uploadercollection. This is necessary to get the ID of the single uploaded file. Note: even if you've set Allow Multiple Files toFalse, Flow will save the Id of your uploaded file in a collection variable. This loop will help extract the file's Id via the next step.
- Loop through the Output from the
-
Assignment (inside Loop):
- Create a new Text variable called
vContentDocumentId. - Assign
{!Loop_Element.CurrentItem}to{!vContentDocumentId}. - (This assignment ensures that after the loop finishes, the variable holds the ID of the last (and only) file.)
- Create a new Text variable called
-
Action (Extract):
- Add an Action element.
- Search for and select
Extract Data with Pre-configured Schema(ConfiguredSchemaExtractor). - Label:
Extract Data from File - Set Inputs:
configurationName: Use the API name of the schema you created in Step 2 of this setup guide.contentDocumentId:{!vContentDocumentId}namedCredentialName:Data_Cloud_API(theNamed Credentialyou created in Step 1 of this setup guide)
-
Action (Parse):
- Add an Action element.
- Search for and select the JSON parser Apex Class you deployed in Step 3 of this setup guide.
- Label:
Prepare data for review - Set Inputs:
jsonString:{!Extract_Data_from_File.extractedJson}
- You can use the Output Resources or manually assign variables.
-
Incorporate other Demo Components:
- Add in any other elements (Create, Get, or Update Records) or Prompt Templates you want to use with the extracted data.
-
Screen (Review):
- Add a Screen element.
- Add Text Input, Number Input, or Display Text components to review outputs.
- Set the Default Value for each component using the variables from the previous step (e.g., Default Value for Borrower Name text box is
{!vBorrowerName}) or usepromptResponsein Display Text components.
-
Create Records (optional):
- Add a Create Records element.
- Choose your relevant object.
- Map the fields from the Outputs of the Parse extracted JSON Action to the related object fields.
This setup guide is for instructions and guidelines to create a Document AI demo. The sample code is delivered "as-is" to the user and was designed for demonstration purposes. Salesforce bears no responsibility to support the user or implementation of this software.