Generate Barcode and QR Code in Salesforce

Final Up to date on Might 11, 2023 by Rakesh Gupta

Huge Concept or Enduring Query:

  • The way to generate Barcode and QR Code in Salesforce?


After studying this weblog, you’ll be capable of:

  • Perceive the distinction between Barcode and QR Code
  • Advantages of utilizing Barcode and QR Code
  • Generate a Barcode, and show it in a Subject
  • Importing Barcode to Salesforce File and affiliate it with product
  • and far more

Jestilla Zetkin is working as a Salesforce Architect at Gurukul On Cloud (GoC). Whereas engaged on Gross sales Cloud implementation, she acquired the requirement to generate barcodes. Beneath is the detailed requirement: 

  1. Generate a Barcode for Product Code
  2. Show the Barcode on the product element web page
  3. Auto-attach the Barcode to file associated record on the product.

What are Barcode and QR Code?

A Barcode and a QR Code are each kinds of machine-readable codes used to retailer and retrieve data.

A barcode is a sq. or rectangular picture composed of parallel black traces and white areas. It’s utilized in shops to trace product stock, in hospitals to trace affected person data, and within the monitoring of airline baggage, and so on.

A fast response code, often known as a QR code, is a sort of barcode that features machine-readable particulars in regards to the merchandise to which it’s connected. It’s used for share information resembling movies, images, net urls, contacts, and so on.

The first distinction between a Barcode and a QR code is {that a} barcode can solely retailer information in a single dimension, whereas a QR code is able to storing information in two dimensions.

Advantages of Utilizing Barcode and QR Code?

Barcode and QR Code present a number of benefits within the gross sales course of, together with improved accuracy, speedy date availability, low-cost implications, and higher stock management. Advantages of Utilizing Barcode and QR Codes:

  1. Environment friendly Information Entry: Barcodes and QR codes get rid of the necessity for handbook information entry, decreasing errors and rising effectivity.
  2. Stock Administration: They assist streamline stock monitoring and administration by offering correct and real-time data.
  3. Quicker Transactions: Scanning barcodes and QR codes quickens point-of-sale transactions, decreasing ready instances.
  4. Simple Implementation: Barcodes and QR codes will be simply generated and printed, and their scanning will be achieved utilizing normal smartphones or devoted scanners.
  5. Enhanced Advertising and marketing Opportunities: QR codes, specifically, can be utilized to interact prospects with interactive content material, promotions, and ads.

The way to Generate Barcode and QR Code?

To generate a barcode, you should utilize numerous on-line barcode mills or programming libraries that provide barcode creation functionalities.

On this article we’ll use bwip-js barcode API. The bwip-js on-line API is hosted on an Amazon AWS EC2 occasion utilizing a node.js software. You need to use this API to dynamically generate barcode photographs from wherever on the internet. The returned picture is in PNG format. There are not any fees to make use of this API, and I couldn’t discover any utilization restrict.

The 2 required parameters are the barcode sort and the worth to encode. You may as well add extra parameters rotate (permits rotating the picture to one of many 4 orthogonal orientations) and includetext.

Guided Observe (We-do):

Carry out the steps beneath to generate a Barcode for Product Code:

Step 1: Create a Customized Method subject on Product to Generate and Show a Barcode

Now we’ll generate the barcode picture utilizing a bwip-js barcode generator and show the picture in a formulation subject utilizing Picture tag.

  1. Click on Setup.
  2. Within the Object Supervisor, sort Product.
  3. Choose Fields & Relationships, then click on New.
  4. Choose Method as Information Sort, then click on Subsequent.
  5. Enter Subject LabelBarcode (Product Code) and click on the tab key, the Subject Identify will populate. 
    1. Method Return Sort: Textual content
  6. Click on on the Subsequent button.
    1. Method URL: IMAGE(“”+ ProductCode +”&scale=2&rotate=N&includetext&backgroundcolor=ffffff”,”Barcode”)
  7. Set the Subject-level Safety for the profiles.  
  8. Add this subject to Web page Structure.
  9. Click on Save.

Step 2: Affiliate Barcode Picture to Salesforce File

Now you understand how to generate the barcode picture with none code. The following step is to add the barcode picture to the Salesforce file for PDF era or print.

  1. bwip-js barcode API returned picture is in PNG format. We want apex code to retrieves the physique of this request as a Blob.
    HttpRequest request = new HttpRequest();
    Http binding = new Http();
    HttpResponse response = binding.ship(request);
    Blob picture = response.getBodyAsBlob();

  2.  The following step is to insert ContentVersion to create a file and ContentDocumentLink to affiliate picture file to product.
    // Creates the contentversion
    ContentVersion cv = new ContentVersion();
    cv.PathOnClient="fileName" + '.jpg';
    cv.VersionData = picture;
    Insert cv;
    // Get the content material doc id from contentversion
    Id contentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id].ContentDocumentId;
    //Create ContentDocumentLink and  Hyperlink the content material doc to a document
    ContentDocumentLink cdl = New ContentDocumentLink();
    cdl.LinkedEntityId = 'Present SF Product Id';
    cdl.ContentDocumentId = contentDocumentId;
    Insert cdl;

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

Step 3: Create a Document-Triggered Stream to Name Invocable Apex on Product Creation

Earlier than discussing the answer, let me present you a diagram of the method at a excessive stage. Please spend a couple of minutes going by way of the next Stream diagram to know it.

First step is to authorize the endpoint url ( in Setup| Safety | Distant Web site Settings.

Step 3.1: Outline Stream Properties

  1. Click on Setup.
  2. Within the Fast Discover field, sort Flows.
  3. Choose Flows, then click on on the New Stream.
  4. Choose the Document-Triggered Stream choice, click on on Create 
    1. Object: Product2
    2. Set off the Stream When: A document is created
    3. Set Entry Situations: None
    4. Optimize the Stream For Motion and Associated Data
    5. Select the Choice to Embody a Run Asynchronously path to entry an exterior system after the unique transaction for the triggering document is efficiently dedicated.
  5. Click on Achieved.

Step 3.2: Method to Assemble the Barcode URL

  1. Below Toolbox, choose Supervisor, then click on New Useful resource to assemble the barcode code by including Product Identify utilizing concatenate formulation. 
  2. Enter the next data:
    1. Useful resource Sort: Method
    2. API Identify: forT_BarcodeURL
    3. Information Sort: Textual content
    4. Method:”+{!$Document.ProductCode}+”&scale=2&rotate=N&includetext&backgroundcolor=ffffff”
  3. Click on Achieved.

Step 3.3: Add Motion – URL to Picture Invocable Apex

The following step is so as to add apex motion to generate picture type barcode url. We are going to use the Motion factor.

  1. Below Run Asynchronously node, choose Motion.
    1. Choose the Convert an Picture URL to Base64 motion.
  2. Label the brand new motion Generate and Affiliate Barcode.
  3. Set Enter Values
    1. Picture File Identify: {!$Document.ProductCode}
    2. Picture URL: {!forT_BarcodeURL}
    3. Mum or dad Document Id: {!$Document.ExternalDataSourceId}
  4. Click on Achieved.

In the long run, Jestilla’s Stream will appear to be the next screenshot:

As soon as all the things appears to be like good, carry out the steps beneath: 

  1. Click on Save.
  2. Enter Stream Label the API Identify will auto-populate.
  3. Click on Present Superior.
  4. API Model for Working the Stream: 58
  5. Interview Label: Auto Generate barcode {!$Stream.CurrentDateTime}
  6. Click on Save.

Nearly there! As soon as all the things appears to be like good, click on the Activate button.  

Proof of Idea

Now onwards, when a consumer inserts a product, the movement will fireplace and create barcode(1) and affiliate(2) the barcode file to the product document.

Formative Evaluation:

I need to hear from you!

What’s one factor you discovered from this publish? How do you envision making use of this new information in the true world? Be happy to share within the feedback beneath.

Source link

Thanks for Reading

Enjoyed this post? Share it with your networks.

Leave a Feedback!