Generate Barcode and QR Code in Salesforce

Generate Barcode and QR Code in Salesforce

Last Updated on July 31, 2023 by Rakesh Gupta

Big Idea or Enduring Question:

  • How to generate Barcodes and QR codes in Salesforce?


After reading this blog, you’ll be able to:

  • Understand the difference between Barcode and QR Code
  • Benefits of using Barcode and QR Code
  • Generate a Barcode, and display it in a Field
  • Uploading Barcode to Salesforce File and associate it with product
  • and much more

Business Use case

Jestilla Zetkin is working as a Salesforce Architect at Gurukul On Cloud (GoC). While working on Sales Cloud implementation, she got the requirement to generate barcodes. Below is the detailed requirement: 

  1. Generate a Barcode for Product Code
  2. Display the Barcode on the product detail page
  3. Auto-attach the Barcode to file related list on the product.

What are Barcode and QR Code?

A Barcode and a QR Code are both types of machine-readable codes used to store and retrieve information.

A barcode is a square or rectangular image composed of parallel black lines and white spaces. It is used in stores to track product inventory, in hospitals to track patient records, and in the tracking of airline luggage, etc.

A quick response code, also known as a QR code, is a type of barcode that includes machine-readable details about the item to which it is attached. It is used for share data such as videos, photos, web urls, contacts, etc.

The primary distinction between a Barcode and a QR code is that a barcode can only store data in one dimension, whereas a QR code is capable of storing data in two dimensions.

Benefits of Using Barcode and QR Code?

Barcode and QR Code provide several advantages in the sales process, including improved accuracy, immediate date availability, low-cost implications, and better inventory control. Benefits of Using Barcode and QR Codes:

  1. Efficient Data Entry: Barcodes and QR codes eliminate the need for manual data entry, reducing errors and increasing efficiency.
  2. Inventory Management: They help streamline inventory tracking and management by providing accurate and real-time information.
  3. Faster Transactions: Scanning barcodes and QR codes speeds up point-of-sale transactions, reducing waiting times.
  4. Easy Implementation: Barcodes and QR codes can be easily generated and printed, and their scanning can be done using standard smartphones or dedicated scanners.
  5. Enhanced Marketing Opportunities: QR codes, in particular, can be used to engage customers with interactive content, promotions, and advertisements.

How to Generate Barcode and QR Code?

To generate a barcode, you can use various online barcode generators or programming libraries that offer barcode creation functionalities.

In this article we will use bwip-js barcode API. The bwip-js online API is hosted on an Amazon AWS EC2 instance using a node.js application. You can use this API to dynamically generate barcode images from anywhere on the web. The returned image is in PNG format. There are no charges to use this API, and I could not find any usage limit.

The two required parameters are the barcode type and the value to encode. You can also add additional parameters rotate (allows rotating the image to one of the four orthogonal orientations) and includetext.

Sample URL

Read bwip-js barcode documentation to understanding about different parameters and capabilities.

Guided Practice (We-do):

Perform the steps below to generate a Barcode for Product Code:

Step 1: Create a Custom Formula field on Product to Generate and Display a Barcode

Now we will generate the barcode image using a bwip-js barcode generator and display the image in a formula field using Image tag.

  1. Click Setup.
  2. In the Object Manager, type Product.
  3. Select Fields & Relationships, then click New.
  4. Select Formula as Data Type, then click Next.
  5. Enter Field LabelBarcode (Product Code) and click the tab key, the Field Name will populate. 
    1. Formula Return Type: Text
  6. Click on the Next button.
    1. Formula URL: IMAGE(“”+ ProductCode +”&scale=2&rotate=N&includetext&backgroundcolor=ffffff”,”Barcode”)
  7. Set the Field-level Security for the profiles.  
  8. Add this field to Page Layout.
  9. Click Save.

Step 2: Associate Barcode Image to Salesforce File

Now you know how to generate the barcode image without any code. The next step is to upload the barcode image to the Salesforce file for PDF generation or print.

  1. bwip-js barcode API returned image is in PNG format. We need apex code to retrieves the body of this request as a Blob.

    HttpRequest request = new HttpRequest();
    Http binding = new Http();
    HttpResponse response = binding.send(request);
    Blob image = response.getBodyAsBlob();

  2.  The next step is to insert ContentVersion to create a file and ContentDocumentLink to associate image file to product.

    // Creates the contentversion
    ContentVersion cv = new ContentVersion();
    cv.Title = 'fileName';
    cv.PathOnClient = 'fileName' + '.jpg';
    cv.VersionData = image;
    Insert cv;
    // Get the content document id from contentversion
    Id contentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id].ContentDocumentId;
    //Create ContentDocumentLink and  Link the content document to a record
    ContentDocumentLink cdl = New ContentDocumentLink();
    cdl.LinkedEntityId = 'Provide SF Product Id';
    cdl.ContentDocumentId = contentDocumentId;
    cdl.shareType = 'V';
    Insert cdl;

  3. Modify the above code as needed. I’ve created an InvocableMethod that performs callouts. You can find it here.

Step 3: Adding Remote Site Settings

Before any Apex callout can call an external site, that site must be registered in the Remote Site Settings page, or the callout fails. Salesforce prevents calls to unauthorized network addresses.  To add a remote site setting:

  1. Click Setup.
  2. In the Quick Find box, enter Remote Site Settings, and then select Remote Site Settings.
  3. Click New Remote Site.
    1. Enter a descriptive term for the Remote Site Name.
    2. Enter the URL ( for the remote site.
    3. Optionally, enter a description of the site.
  4. Click Save.

Step 4: Create a Record-Triggered Flow to Call Invocable Apex on Product Creation

Before discussing the solution, let me show you a diagram of the process at a high level. Please spend a few minutes going through the following Flow diagram to understand it.

Step 4.1: Define Flow Properties

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows, then click on the New Flow.
  4. Select the Record-Triggered Flow option, click on Create 
    1. Object: Product2
    2. Trigger the Flow When: A record is created
    3. Set Entry Conditions: None
    4. Optimize the Flow For Action and Related Records
    5. Choose the Option to Include a Run Asynchronously path to access an external system after the original transaction for the triggering record is successfully committed.
  5. Click Done.

Step 4.2: Formula to Construct the Barcode URL

  1. Under Toolbox, select Manager, then click New Resource to construct the barcode code by adding Product Name using concatenate formula. 
  2. Input the following information:
    1. Resource Type: Formula
    2. API Name: forT_BarcodeURL
    3. Data Type: Text
    4. Formula:”+{!$Record.ProductCode}+”&scale=2&rotate=N&includetext&backgroundcolor=ffffff”
  3. Click Done.

Step 4.3: Add Action – URL to Image Invocable Apex

The next step is to add apex action to generate image form barcode url. We will use the Action element.

  1. Under Run Asynchronously node, select Action.
    1. Select the Convert an Image URL to Base64 action.
  2. Label the new action Generate and Associate Barcode.
  3. Set Input Values
    1. Image File Name: {!$Record.ProductCode}
    2. Image URL: {!forT_BarcodeURL}
    3. Parent Record Id: {!$Record.Id}
  4. Click Done.

In the end, Jestilla’s Flow will look like the following screenshot:

Once everything looks good, perform the steps below: 

  1. Click Save.
  2. Enter Flow Label the API Name will auto-populate.
  3. Click Show Advanced.
  4. API Version for Running the Flow: 58
  5. Interview Label: Auto Generate barcode {!$Flow.CurrentDateTime}
  6. Click Save.

Almost there! Once everything looks good, click the Activate button.  

👉 Check out the video for step-by-step instructions.

Proof of Concept

Now onwards, when a user inserts a product, the flow will fire and create barcode(1) and associate(2) the barcode file to the product record.

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

Preferred Timing(required)

11 thoughts on “Generate Barcode and QR Code in Salesforce

  1. Hi Rakesh,
    This is a fairly neat process to generate a barcode. We barcode all products as they are received to send in the field for technicians to scan with FSL. Currently I am exporting all products monthly from data loader then uploading to an address book in Dymo to print barcodes.
    Do you have any thoughts on also printing the barcode with a defined quantity from the record directly to a printer?

Leave a Reply

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