Day 14: Scan a Barcode in a Lightning Web Component (Beta) #TexeiAdventCalendar

By

2 minutes de lecture

Hello everyone, until now, we have written 13 wonderful stories. The last one is here, I hope that you enjoyed it! Today, we will talk about how we can scan a Barcode in Lightning web component (LWC) by using our mobile phone.

Now, we have the possibility to scan a QR Code or UPC Symbol by using our mobile device’s camera and mobile OS platform. When a barcode is successfully scanned, the data that was read from the barcode is returned to the Lightning web component that invoked it. And we don’t need to have a network connection.

So now, we will show you the steps to scan a BarCode in LWC, let’s go.

Firstly, you must import BarcodeScanner using the standard JavaScript import :

import { getBarcodeScanner } from 'lightning/mobileCapabilities';

after importing the BarCode Scanner API, you can now test the Availability of the API by using this function :

handleBeginScanClick(event) {
    const myScanner = getBarcodeScanner();
    if(myScanner.isAvailable()) {
// Perform scanning operations
    }
    else {
// Scanner not available
// Not running on hardware with a scanner
// Handle with message, error, beep, and so on
    }
}

So now, we have the BarCode Scanner API available. We can now begin to scan a barCode by using the function beginCapture() :

const myScanner = getBarcodeScanner();
const scanningOptions = {
    barcodeTypes: [
// Add expected barcode types here
        myScanner.barcodeTypes.QR
    ]
}
myScanner.beginCapture(scanningOptions)
    .then((result) => {
// Do something with the result of the scan
        console.log(result);
        this.scannedBarcode = result.value;
    })
    .catch((error) => {
// Handle cancellation and scanning errors here
        console.error(error);
    })
    .finally(() => {
        myScanner.endCapture();
    });

The function beginCapture() have a parametre: scanningOptions to select the type of the scan such as (QR,dataMatrix etc…), you can see all the barCodes types here .

When we scan successfully the barcode , the result is returned via a fulfilled promise. But if we have a failed result so we handle errors in the catch clause.

The last clause , is finally when we end the scan barCode. So we can call the funtion endCapture() to handle final cleanup.

barcode in Lightning web components

So now ,you can scan easily any barcode in Lightning web components, it’s amazing ;)

Hope you had a good read! Want to learn more? Check out the new article written by Mouloud HABCHI about Salesforce dynamic forms. Follow us on LinkedIn here and on Twitter here!

Read more posts

Enforce code standards with PMD

Developers working on a project usually set coding rules to have a standardized codebase. It is an important piece of the code maintainability, and it can be very easy …
March 2023
Advices
Scratch orgs

Uncovering Salesforce Settings: A Step-by-Step Guide for Scratch Orgs

Today, it’s pretty easy to build your Scratch Org definition file when you know what Settings you want to activate, as they are mapped with the same setting names …
February 2023
Advices
Business Analyst

Core qualities of a Business Analyst?

A common definition we are used to hear is that being a Business Analyst means to have a combination of both hard skills and soft skills. What does a …
June 2022
Advices
Image d'illustration d'une employée travaillant sur un ordinateur portable

Process builder and workflow make way to Flows (½)

Overview “If you can do it with a Workflow, then do it with a Process Builder, because everything a Workflow does, a Process Builder does it better”. If you …
March 2022
Advices

Day 22 : Salesforce new “Migrate To Flow tool” in Spring 22

As most of you already know, the workflow rules and process builders are planned to be retired in 2023 (no precise date defined so far). Today, I’m going to …
December 2021
Advices

Day 18 : Fake callout responses for test classes !

Hello everybody ! Today let’s talk about Apex tests classes in Salesforce. Everyone loves a good test class, and Salesforce makes it official by requiring to have a minimum …
December 2021
Advices