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?
Targets:
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:
- Generate a Barcode for Product Code
- Show the Barcode on the product element web page
- 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:
- Environment friendly Information Entry: Barcodes and QR codes get rid of the necessity for handbook information entry, decreasing errors and rising effectivity.
- Stock Administration: They assist streamline stock monitoring and administration by offering correct and real-time data.
- Quicker Transactions: Scanning barcodes and QR codes quickens point-of-sale transactions, decreasing ready instances.
- Simple Implementation: Barcodes and QR codes will be simply generated and printed, and their scanning will be achieved utilizing normal smartphones or devoted scanners.
- 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.
- Click on Setup.
- Within the Object Supervisor, sort Product.
- Choose Fields & Relationships, then click on New.
- Choose Method as Information Sort, then click on Subsequent.
- Enter Subject Label – Barcode (Product Code) and click on the tab key, the Subject Identify will populate.
- Method Return Sort: Textual content
- Click on on the Subsequent button.
- Method URL: IMAGE(“https://bwipjs-api.metafloor.com/?bcid=code39&text=”+ ProductCode +”&scale=2&rotate=N&includetext&backgroundcolor=ffffff”,”Barcode”)
- Set the Subject-level Safety for the profiles.
- Add this subject to Web page Structure.
- 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.
- 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(); request.setEndpoint(r.imageURL); request.setMethod('GET'); Http binding = new Http(); HttpResponse response = binding.ship(request); Blob picture = response.getBodyAsBlob();
- 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.Title="fileName"; 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; cdl.shareType="V"; Insert cdl;
- 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 (https://bwipjs-api.metafloor.com) in Setup| Safety | Distant Web site Settings.
Step 3.1: Outline Stream Properties
- Click on Setup.
- Within the Fast Discover field, sort Flows.
- Choose Flows, then click on on the New Stream.
- Choose the Document-Triggered Stream choice, click on on Create
- Object: Product2
- Set off the Stream When: A document is created
- Set Entry Situations: None
- Optimize the Stream For Motion and Associated Data
- 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.
- Click on Achieved.
Step 3.2: Method to Assemble the Barcode URL
- Below Toolbox, choose Supervisor, then click on New Useful resource to assemble the barcode code by including Product Identify utilizing concatenate formulation.
- Enter the next data:
- Useful resource Sort: Method
- API Identify: forT_BarcodeURL
- Information Sort: Textual content
- Method: “https://bwipjs-api.metafloor.com/?bcid=code39&text=”+{!$Document.ProductCode}+”&scale=2&rotate=N&includetext&backgroundcolor=ffffff”
- 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.
- Below Run Asynchronously node, choose Motion.
- Choose the Convert an Picture URL to Base64 motion.
- Label the brand new motion Generate and Affiliate Barcode.
- Set Enter Values
- Picture File Identify: {!$Document.ProductCode}
- Picture URL: {!forT_BarcodeURL}
- Mum or dad Document Id: {!$Document.ExternalDataSourceId}
- 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:
- Click on Save.
- Enter Stream Label the API Identify will auto-populate.
- Click on Present Superior.
- API Model for Working the Stream: 58
- Interview Label: Auto Generate barcode {!$Stream.CurrentDateTime}
- 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.