Sample Code Snippets for OnScan Event
This article covers the following use cases for the OnScan event, along with sample code snippets:
Scenario 1: Scan and Populate the Barcode Text Scanned Field, and Parse the Text into Different Field
As a mobile user, you want to scan a Barcode or QR code. The scanned text should populate the field where the scan occurred, and a portion of that text should be parsed and populated into a different field.
The following is the sample flow:
1. Scan the barcode on Sales Order Number field on an IB record.
2. Populate the barcode text 1001234567890210123ABC on Sales Order Number field.
3. Parse the text and populate 123ABC on the Serial/Lot Number field.
Sample Code Snippet:
$event.get(function (response) {
debugger;
var parsedRec = JSON.parse(response);
var SalesOrderNumber = parsedRec.barcodeMetaData && parsedRec.barcodeMetaData.value;
if (SalesOrderNumber && SalesOrderNumber !== undefined && SalesOrderNumber.length === 25) {
var serialLotNumber = SalesOrderNumber.substring(19);
$sfm_records.setFieldValue(
{
header: {
fields: [
{
name: "SVMXC__Serial_Lot_Number__c",
value: serialLotNumber,
},
],
},
},
() => {
$response({ status: "success" });
}
);
// alert('success');
}});
Scenario 2: Scan Barcode and Populate Parsed Text into Scanned and Other Fields
As a mobile user, you want to scan a Barcode or QR code. The parse text should automatically populate into the scanned field and other fields.
The following is the sample flow:
1. Scan the barcode on Asset Tag field on an IB record.
2. Pass the barcode—generated text 0345312000001109112510ABCD1234 to the code snippet to parse the text.
3. Override 03453120000011 on the Asset Tag field.
4. Populate ABCD1234 on Serial/Lot Number field.
5. Populate 091125 on Due Date field with the Date field value as November 25, 2009.
Sample Code Snippet:
$event.get(function (response) {
debugger;
var parsedRec = JSON.parse(response);
var AssetTag = parsedRec.barcodeMetaData && parsedRec.barcodeMetaData.value;
var newAssetTag = AssetTag.substring(2, 16);
var dueDateString = AssetTag.substring(18, 24);
var serialLotNumber = AssetTag.substring(26);
var year = 2000 + parseInt(dueDateString.substring(0, 2), 10);
var month = parseInt(dueDateString.substring(2, 4), 10);
var day = parseInt(dueDateString.substring(4, 6), 10);
var dueDate = new Date(year, month - 1, day);
if (AssetTag && AssetTag.length === 34) {
$sfm_records.setFieldValue(
{
header: {
fields: [
{
name: "SVMXC__Asset_Tag__c",
value: newAssetTag
},
{
name: "SVMXC__Serial_Lot_Number__c",
value: serialLotNumber
},
{
name: "Due_Date__c",
value: dueDate
},
],
},
},
() => {
$response({ status: "success" });
}
);
// alert('success');
}});
Was this helpful?