In this blog, you will learn how you can use the newly launched Barcode Scanner API for lightning web component in salesforce.
Salesforce released a new Barcode Scanner API which now supports Scanning barcodes from a Lightning web component for mobile devices.
salesforceScanner.html :-
<!-- barcodeScannerExample.html -->
<template>
<div class="slds-text-align_center">
<span class="slds-text-heading_large">
<lightning-icon icon-name="utility:scan" alternative-text="XML file" title="XML">
</lightning-icon>Salesforce Wallah</span>
</div>
<template if:true={scannedBarcode}>
<div class="slds-var-m-vertical_large slds-var-p-vertical_medium
slds-text-align_center slds-border_top slds-border_bottom">
Scanned barcode value is:
<span class="slds-text-heading_small">{scannedBarcode}</span>
</div>
</template>
<div class="slds-align_absolute-center slds-m-vertical_large">
<lightning-button
variant="brand"
class="slds-var-m-left_x-small"
disabled={scanButtonDisabled}
icon-name="utility:scan"
label="Scan Barcode"
title="Open a camera view and look for a barcode to scan"
onclick={handleBeginScanClick}>
</lightning-button>
</div>
</template>
salesforceScanner.js :-
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getBarcodeScanner } from 'lightning/mobileCapabilities';
export default class SalesforceScanner extends LightningElement {
myScanner;
scanButtonDisabled = false;
scannedBarcode = '';
connectedCallback() {
this.myScanner = getBarcodeScanner();
if (this.myScanner == null || !this.myScanner.isAvailable()) {
this.scanButtonDisabled = true;
}
}
handleBeginScanClick(event) {
this.scannedBarcode = '';
if (this.myScanner != null && this.myScanner.isAvailable()) {
const scanningOptions = {
barcodeTypes: [this.myScanner.barcodeTypes.QR],
instructionText: 'Scan a QR Code',
successText: 'Scanning complete.'
};
this.myScanner
.beginCapture(scanningOptions)
.then((result) => {
this.scannedBarcode = result.value;
this.dispatchEvent(
new ShowToastEvent({
title: 'Successful Scan',
message: 'Barcode scanned successfully.',
variant: 'success'
})
);
})
.catch((error) => {
// Handle cancellation and unexpected errors here
console.error(error);
if (error.code == 'userDismissedScanner') {
// User clicked Cancel
this.dispatchEvent(
new ShowToastEvent({
title: 'Scanning Cancelled',
message:
'You cancelled the scanning session.',
mode: 'sticky'
})
);
}
else {
// Inform the user we ran into something unexpected
this.dispatchEvent(
new ShowToastEvent({
title: 'Barcode Scanner Error',
message:
'There was a problem scanning the barcode: ' +
error.message,
variant: 'error',
mode: 'sticky'
})
);
}
})
.finally(() => {
this.myScanner.endCapture();
});
} else {
alert(
'Scan Barcode button should be disabled and unclickable.'
);
console.log('Somehow it got clicked: ');
console.log(event);
// Let user know they need to use a mobile phone with a camera
this.dispatchEvent(
new ShowToastEvent({
title: 'Barcode Scanner Is Not Available',
message:
'Try again from the Salesforce app on a mobile device.',
variant: 'error'
})
);
}
}
}
salesforceScanner.js-meta.xml :-
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>55.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
<target>lightningCommunity__Default</target>
<target>lightningCommunity__Page</target>
</targets>
</LightningComponentBundle>
Resources :-
- Developer Guide Click Here
Note :-
This Code only work on Mobile salesforce, on desktop it will not work.
Comments
Post a Comment
Please Write your comment here.