Skip to main content

Salesforce

Sending PDF Document via REST API in Salesforce: A Beginner’s Guide

A Male Programmer Shows A Female Colleague A Coding Technique. The Codes Are Visible On The Laptop Screen

Imagine this: You’re working in a sales team, and you need to send a professionally designed PDF quote to your client. But instead of attaching it manually, your system does it automatically at the click of a button. Seamless, isn’t it? This magic happens when Salesforce and REST API join hands to send PDF documents. If you’re new to this concept, don’t worry. I’ll walk you through the story behind it, how it works, and why it’s so cool, with simple examples and answers to common questions.

Setting the Stage: What is REST API in Salesforce?

Before jumping to PDFs, let’s break down REST API. In Salesforce, REST API is like a bridge that lets different systems talk to each other. For example, if a customer portal needs to fetch or send data to Salesforce, REST API enables that communication.

REST (Representational State Transfer) is lightweight and easy to use, which makes it beginner-friendly. It’s like sending a letter: You include the address (endpoint), the content (data), and the type of communication (GET, POST, etc.).

Now, let’s add the PDF element to this picture.

Sending Pdf Document Via Rest Api In Salesforce A Beginner's Guide Visual Selection (1)

Why Send PDFs via REST API?

Think about scenarios where businesses need to:

  • Send order invoices or quotes to clients.
  • Share contracts with vendors.
  • Automatically email reports generated in Salesforce.

In all these cases, PDFs come into play because they’re professional, uneditable, and easy to share. By combining REST API with Salesforce, you can generate and send these documents on the fly, saving time and effort.

The Process in a Nutshell

Sending Pdf Document Via Rest Api In Salesforce A Beginner's Guide Visual Selection

Here’s how the story unfolds:

  1. Generate the PDF in Salesforce: Salesforce provides features like Visualforce or Lightning Components to create PDFs dynamically.
  2. Encode the PDF: Since REST API transmits data as text, the PDF is converted into Base64 format.
  3. Prepare the API Request: Include the encoded PDF in the body of a POST request.
  4. Send the Request: Use Salesforce’s HTTP methods to send the request to an external system or API endpoint.
  5. Handle the Response: Process the response from the external system, like confirming whether the PDF was received.

Step-by-Step Breakdown

Let’s dive into each step with an easy-to-understand example.

1. Generate the PDF

Imagine you have a Visualforce page to create a quote PDF. You can use the PageReference class in Salesforce to render the Visualforce page as a PDF. Here’s how:

PageReference pdfPage = Page.QuotePDF; // Name of your Visualforce page
Blob pdfBlob = pdfPage.getContentAsPDF();

2. Encode the PDF

To send the PDF via REST API, it must be converted into a format suitable for transmission—Base64 encoding. It’s like turning your PDF into a text-based package.

String base64EncodedPDF = EncodingUtil.base64Encode(pdfBlob);

3. Prepare the API Request

Now, you’ll structure the request. Think of it as preparing an envelope with the recipient’s address and the content. Here’s a sample:

HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.example.com/send');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');

// Add the PDF to the request body
String requestBody = '{"file":"' + base64EncodedPDF + '", "filename":"Quote.pdf"}';
request.setBody(requestBody);

4. Send the Request

Use the Http class in Salesforce to send the request.

Http http = new Http();
HttpResponse response = http.send(request);

5. Handle the Response

After sending, you’ll receive a response. If the external system says “success,” your job is done. If not, you’ll need to handle the error.

if (response.getStatusCode() == 200) {
    System.debug('PDF sent successfully!');
} else {
    System.debug('Failed to send PDF: ' + response.getBody());
}

Common Questions Answered

1. What if the PDF is too large?

  • Check the size limit of your external system’s API. If your PDF exceeds the limit, consider compressing it or splitting the data into chunks.

2. Is Base64 encoding mandatory?

  • Yes, if the external API requires it. Some APIs might allow you to upload the Blob directly, but Base64 is a safe bet.

3. Can I test this locally?

  • Yes! Use tools like Postman to simulate the API endpoint and check if your request is structured correctly.

The Bigger Picture

Sending PDFs via REST API in Salesforce is more than a technical task; it’s a game-changer for automating business processes. Imagine eliminating manual tasks like attaching PDFs to emails. With a few lines of code, you’re creating a seamless experience for both your team and your clients.

Key Takeaways

  • REST API is the bridge that connects Salesforce to other systems.
  • Generating and encoding PDFs in Salesforce makes data sharing efficient and professional.
  • Use tools like Visualforce, Base64 encoding, and the Http class to implement this process.
  • Always test your API calls and handle responses for a smooth workflow.

Now that you’ve learned the basics, why not try implementing it? Start small, experiment, and see how this approach transforms your workflows. Happy coding!

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Darshan Kukde

Darshan Kukde is an experienced Salesforce professional with around 4 years of experience in the Salesforce domain. He has worked across Salesforce Sales, Service, Experience, Marketing Cloud, and CPQ. With multiple certifications and a commitment to continuous learning, Darshan excels in crafting innovative solutions that enhance business processes. His passion for technology is reflected in his expertise across various platforms, ensuring he remains at the forefront of industry advancements.

More from this Author

Categories
Follow Us