How to Read Csv File From Document in Apex
In this pace-by-step guide, we'll observe steps on how to parse a PDF invoice document and go output in CSV format with Salesforce Noon and PDF.co. In the beginning, we'll go through all the bones steps on salesforce integration. Afterwards we'll also encounter the code and demo video. Let's get started!
- Create Remote Site Settings
- Enter API Key
- Create an Apex Grade in Salesforce
- Verify Code
- See the Consequence
- Source Code Files
- Demo Video: Document Parser with CSV Output
1. Create Remote Site Settings
Create remote site settings in the Salesforce Org like below.
Please note that the URL is "https://api.pdf.co".
2. Enter API Key
2. You should have an API class in your org with the key.
3. Create an Apex Class in Salesforce
Create an apex class in Salesforce like below and paste the code there.
One time yous login to the Salesforce org, yous volition see the screen like below and click on "Developer Console"
Create an noon class. For this, click on "Files" then "New" so "Noon Course".
Write the class name "DocumentParserCSV" and click "Ok". Now copy the DocumentParserCSV code in this file.
Similarly, create a new file with the name "DocumentParserCSVTest" and copy the code.
4. Verify Code
To Verify the lawmaking, please open the execute Anonymous window and call the method below.
Then Click on "Execute".
5. See the Consequence
Now, You tin can see the result in the Debug Logs.
Source Code Files:
API.cls
public class API { public static String API_KEY = '********************************'; //Get Phone call public static HttpResponse getCall(String endPointURL) { Http http = new Http(); HttpRequest request = new HttpRequest(); //Set the central in header. request.setHeader('x-api-cardinal', API_KEY); //Prepare the URL to invoke request.setEndpoint(endPointURL); //Sets the type of method to be used for the HTTP request. request.setMethod('GET'); /* The timeout is the maximum fourth dimension to wait for establishing the HTTP connexion. The same timeout is used for waiting for the asking to start. When the request is executing, such as retrieving or posting data, the connectedness is kept live until the request finishes. */ asking.setTimeout(60000); HttpResponse response = http.send(asking); return response; } //Mail Phone call public static HttpResponse postCall(String endPointURL, String torso, String contentType) { Http http = new Http(); HttpRequest request = new HttpRequest(); //Set the key in header. request.setHeader('x-api-primal', API_KEY); //Ready the URL to invoke request.setEndpoint(endPointURL); request.setHeader('Content-Type', contentType); //Sets the blazon of method to be used for the HTTP request. request.setMethod('POST'); //Sets the contents of the body for this asking. request.setBody(trunk); /* The timeout is the maximum time to wait for establishing the HTTP connexion. The same timeout is used for waiting for the request to start. When the request is executing, such equally retrieving or posting data, the connection is kept alive until the request finishes. */ request.setTimeout(60000); HttpResponse response = http.ship(asking); render response; } //PUT Telephone call for Hulk Body public static HttpResponse putCall(String endPointURL, Blob body, String contentType) { Http http = new Http(); HttpRequest asking = new HttpRequest(); //Fix the cardinal in header. request.setHeader('ten-api-key', API_KEY); //Gear up the URL to invoke request.setEndpoint(endPointURL); asking.setHeader('Content-Type', contentType); //Sets the blazon of method to be used for the HTTP request. request.setMethod('PUT'); //Sets the contents of the trunk for this request using a Blob. asking.setBodyAsBlob(body); /* The timeout is the maximum time to await for establishing the HTTP connexion. The aforementioned timeout is used for waiting for the request to outset. When the request is executing, such as retrieving or posting data, the connectedness is kept alive until the request finishes. */ request.setTimeout(60000); HttpResponse response = http.transport(asking); return response; } }
APITest.cls
@isTest public class APITest { private testmethod static void getCallTest() { Test.setMock(HttpCalloutMock.class, new APITest.APIMock()); Test.StartTest(); HttpResponse resp = API.getCall('https://world wide web.google.com/'); Test.stopTest(); Organization.assertNotEquals(cipher, resp); } private testmethod static void postCallTest() { Test.setMock(HttpCalloutMock.class, new APITest.APIMock()); Exam.StartTest(); HTTPResponse res = API.postCall('https://world wide web.google.com/', 'jsonPayload', 'application/json'); Examination.stopTest(); System.assertNotEquals(null, res); } individual testmethod static void putCallTest() { Test.setMock(HttpCalloutMock.class, new APITest.APIMock()); Examination.StartTest(); HTTPResponse res = API.putCall('https://www.google.com/', Blob.valueOf('sourceFile'),'application/octet-stream'); Test.stopTest(); Arrangement.assertNotEquals(aught, res); } public course APIMock implements HttpCalloutMock { public HTTPResponse answer(HTTPRequest req) { HttpResponse res = new HttpResponse(); String testBody = '{"presignedUrl":"https://pdf-temp-files.s3-the states-due west-2.amazonaws.com/0c72bf56341142ba83c8f98b47f14d62/test.pdf?X-Amz-Expires=900&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIZJDPLX6D7EHVCKA/20200302/us-w-2/s3/aws4_request&10-Amz-Date=20200302T143951Z&X-Amz-SignedHeaders=host&X-Amz-Signature=8650913644b6425ba8d52b78634698e5fc8970157d971a96f0279a64f4ba87fc","url":"https://pdf-temp-files.s3-usa-west-2.amazonaws.com/0c72bf56341142ba83c8f98b47f14d62/examination.pdf?X-Amz-Expires=3600&ten-amz-security-token=FwoGZXIvYXdzEGgaDA9KaTOXRjkCdCqSTCKBAW9tReCLk1fVTZBH9exl9VIbP8Gfp1pE9hg6et94IBpNamOaBJ6%2B9Vsa5zxfiddlgA%2BxQ4tpd9gprFAxMzjN7UtjU%2B2gf%2FKbUKc2lfV18D2wXKd1FEhC6kkGJVL5UaoFONG%2Fw2jXfLxe3nCfquMEDo12XzcqIQtNFWXjKPWBkQEvmii4tfTyBTIot4Na%2BAUqkLshH0R7HVKlEBV8btqa0ctBjwzwpWkoU%2BF%2BCtnm8Lm4Eg%3D%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIA4NRRSZPHEGHTOA4W/20200302/u.s.-west-2/s3/aws4_request&10-Amz-Appointment=20200302T143951Z&X-Amz-SignedHeaders=host;x-amz-security-token&Ten-Amz-Signature=243419ac4a9a315eebc2db72df0817de6a261a684482bbc897f0e7bb5d202bb9","error":simulated,"status":200,"proper name":"exam.pdf","remainingCredits":98145}'; res.setHeader('Content-Type', 'application/json'); res.setBody(testBody); res.setStatusCode(200); return res; } } }
DocumentParserCSV.cls
public class DocumentParserCSV { public void startProcessing() { endeavor { JSONGenerator gen = JSON.createGenerator(true); gen.writeStartObject(); gen.writeStringField('url', 'https://bytescout-com.s3-united states-west-2.amazonaws.com/files/demo-files/cloud-api/document-parser/sample-invoice.pdf'); gen.writeStringField('templateId', '1'); gen.writeStringField('outputFormat', 'CSV'); gen.writeStringField('generateCsvHeaders', 'true'); gen.writeBooleanField('async', false); gen.writeStringField('encrypt', 'fake'); gen.writeStringField('inline', 'true'); gen.writeStringField('password', ''); gen.writeBooleanField('storeResult', false); gen.writeEndObject(); // Catechumen dictionary of params to JSON String jsonPayload = gen.getAsString(); String url = 'https://api.pdf.co/v1/pdf/documentparser'; HttpResponse response = API.postCall(url, jsonPayload, 'application/json'); Map<String, Object> json = (Map<String, Object>)JSON.deserializeUntyped(response.getBody()); if(response.getStatusCode() == 200) { if ((Boolean)json.get('error') == simulated) { // Become URL of generated PDF file String jsonBody =(String)json.get('body'); SYstem.Debug(jsonBody); } } else { System.debug('Error Response ' + response.getBody()); System.Debug(' Status ' + response.getStatus()); System.Debug(' Condition Lawmaking' + response.getStatusCode()); Arrangement.Debug(' Response Cord' + response.toString()); } } catch (Exception ex) { String errorBody = 'Message: ' + ex.getMessage() + ' -- Crusade: ' + ex.getCause() + ' -- Stacktrace: ' + ex.getStackTraceString(); System.Debug(errorBody); } } }
DocumentParserCSVTest.cls
@isTest individual class DocumentParserCSVTest { private testmethod static void testDocumentParserCSV() { Test.setMock(HttpCalloutMock.class, new DocumentParserCSVTest.DocumentCreationMock()); DocumentParserCSV dc = new DocumentParserCSV(); Test.startTest(); dc.startProcessing(); Test.stopTest(); } private testmethod static void testDocumentParserCSVError() { DocumentParserCSV dc = new DocumentParserCSV(); Test.startTest(); dc.startProcessing(); Test.stopTest(); } public class DocumentCreationMock implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); Cord testBody = '{"presignedUrl":"https://pdf-temp-files.s3-us-west-2.amazonaws.com/0c72bf56341142ba83c8f98b47f14d62/exam.pdf?X-Amz-Expires=900&X-Amz-Algorithm=AWS4-HMAC-SHA256&10-Amz-Credential=AKIAIZJDPLX6D7EHVCKA/20200302/united states-west-two/s3/aws4_request&Ten-Amz-Date=20200302T143951Z&X-Amz-SignedHeaders=host&X-Amz-Signature=8650913644b6425ba8d52b78634698e5fc8970157d971a96f0279a64f4ba87fc","url":"https://pdf-temp-files.s3-united states-west-2.amazonaws.com/0c72bf56341142ba83c8f98b47f14d62/test.pdf?X-Amz-Expires=3600&ten-amz-security-token=FwoGZXIvYXdzEGgaDA9KaTOXRjkCdCqSTCKBAW9tReCLk1fVTZBH9exl9VIbP8Gfp1pE9hg6et94IBpNamOaBJ6%2B9Vsa5zxfiddlgA%2BxQ4tpd9gprFAxMzjN7UtjU%2B2gf%2FKbUKc2lfV18D2wXKd1FEhC6kkGJVL5UaoFONG%2Fw2jXfLxe3nCfquMEDo12XzcqIQtNFWXjKPWBkQEvmii4tfTyBTIot4Na%2BAUqkLshH0R7HVKlEBV8btqa0ctBjwzwpWkoU%2BF%2BCtnm8Lm4Eg%3D%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIA4NRRSZPHEGHTOA4W/20200302/us-west-2/s3/aws4_request&10-Amz-Date=20200302T143951Z&Ten-Amz-SignedHeaders=host;x-amz-security-token&Ten-Amz-Signature=243419ac4a9a315eebc2db72df0817de6a261a684482bbc897f0e7bb5d202bb9","fault":false,"status":200,"name":"test.pdf","remainingCredits":98145}'; res.setHeader('Content-Type', 'awarding/json'); res.setBody(testBody); res.setStatusCode(200); render res; } } }
Document Parser with CSV Output – Demo Video
Source: https://pdf.co/parse-document-to-csv-format-with-salesforce-apex-and-pdfco
0 Response to "How to Read Csv File From Document in Apex"
Enregistrer un commentaire