How to Parse Invoice using Salesforce Apex using

In this step-by-step guide, we’ll briefly see steps on how to parse invoice files using Document Parser in Salesforce Apex using Initially, we’ll go through all the basic steps of Salesforce integration. Later we’ll also see the code and demo video. Let’s get started!

  1. Create Remote Site Settings
  2. Create an Apex Class in Salesforce
  3. Enter API Key
  4. Verify Code
  5. Check the Output
  6. Source Code Files
  7. Demo Video: Parse Invoice

Step 1: Create Remote Site Settings

Create one remote site settings in the Salesforce Org like below.

Note that the URL is “”.

Step 2: Create an Apex Class in Salesforce

2. Create an apex class in Salesforce like below and paste the code there.
Once you login to the Salesforce org, you will see the screen like below and click on “Developer Console”

Create an apex class. For this, click on “Files” then “New” then “Apex Class”.

Write the class name “DocumentParserOutputAsJSON” and click “Ok”. Now copy the DocumentParserOutputAsJSON code in this file.

Similarly, create a new file with the name “DocumentParserOutputAsJSONTest” and copy the code.

Step 3: Enter API Key

In the DocumentParserOutputAsJSON file, please add the API key by replacing the ‘******************************************’.

Step 4: Verify Code

To Verify the code, please open the execute Anonymous window and call the method like below.

Then Click on “Execute”.

Step 5: Check the Output

5. Now, You will see the output in the debug log.

Source Code Files:


public class DocumentParserOutputAsJSON {
    // The authentication key (API Key).
    // Get your own by registering at
    static String API_KEY = '***********************';
    // Direct URL of source PDF file.
    static string SourceFileUrl = '';
    // PDF document password. Leave empty for unprotected documents.
    static string Password = '';

    String jsonOutput;
    public void parseDocumentAsJSON()
            // Create HTTP client instance
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            // Set API Key
            request.setHeader('x-api-key', API_KEY);
            Boolean async = false;
            String inline = 'true';
            String profiles = '';
            Boolean encrypt = false;
            Boolean storeResult = false;
            // Prepare requests params as JSON
            // See documentation:
            // Create JSON payload
            JSONGenerator gen = JSON.createGenerator(true);
            gen.writeStringField('url', SourceFileUrl);
            gen.writeStringField('outputFormat', 'JSON');
            gen.writeStringField('templateId', '1');
            gen.writeBooleanField('async', async);
            gen.writeBooleanField('encrypt', encrypt);
            gen.writeStringField('inline', inline);
            gen.writeStringField('password', password);
            gen.writeStringField('profiles', profiles);
            gen.writeBooleanField('storeResult', false);
            // Convert dictionary of params to JSON
            String jsonPayload = gen.getAsString();
            // URL of 'PDF Edit' endpoint
            string url = '';
            request.setHeader('Content-Type', 'application/json;charset=UTF-8');
            // Execute request
            HttpResponse response =  http.send(request);                
            if(response.getStatusCode() == 200)
                // Parse JSON response
                Map<String, Object> json = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
                if ((Boolean)json.get('error') == false)
                    jsonOutput = response.getBody();
                    System.debug('JSON '+ jsonOutput);    
                System.debug('Error Response ' + response.getBody());
                System.Debug(' Status ' + response.getStatus());
                System.Debug(' Status Code' + response.getStatusCode());
                System.Debug(' Response String' + response.toString());
        catch (Exception ex)
            String errorBody = 'Message: ' + ex.getMessage() + ' -- Cause: ' + ex.getCause() + ' -- Stacktrace: ' + ex.getStackTraceString();


public class DocumentParserOutputAsJSONTest
    private  testmethod static void testparseDocumentAsJSON()
        Test.setMock(HttpCalloutMock.class, new DocumentParserOutputAsJSONTest.DocumentCreationMock());
        DocumentParserOutputAsJSON docParse = new DocumentParserOutputAsJSON();
        System.assert(docParse.jsonOutput.contains('John A. Doe'));

    private  testmethod static void testparseDocumentAsJSONException()
        DocumentParserOutputAsJSON docParse = new DocumentParserOutputAsJSON();
        System.assert(null == docParse.jsonOutput);
    public class DocumentCreationMock implements HttpCalloutMock {
        public HTTPResponse respond(HTTPRequest req) {
            HttpResponse res = new HttpResponse();
            String testBody = '{"hash":"John A. Doe","url":";x-amz-security-token&X-Amz-Signature=95287bf3c007fed4c2c5aeea1ce75c846cc6c68b22aaf35175ebe41a105f54e1","pageCount":1,"error":false,"status":200,"name":"newDocument","remainingCredits":9913694,"credits":3}';
            res.setHeader('Content-Type', 'application/json');
            return res;

Parse Invoice – Demo Video

Learn how you can extract data from PDF invoices using API and Salesforce integration.

Similar Pages