Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Need help accessing barcode data in a mobile app

gjz
Mega Sage

I am creating a custom mobile app that allows the user to scan multiple asset barcodes before submitting.  The function that submits the data is a scripted Action Item that uses a WriteBackAction to run a function in a script include.

 

My problem is the barcode data, it's not sending the type of data I thought it would be.  I watched the Mobile App Academy video for QR Code scanning several times, and in it the presenter says the data is just a string, but that's not my experience.

 

Once I scan multiple barcodes and submit, the script log shows it is "Ljava.lang.string:@xxxx".  How do I pass the scanned data to a script include?

 

My mobile Action item WriteBackAction:

gjz_2-1790117249247.png

 

The messages take the "else" path, I do have a sys_id and scanned assets data.

 

Currently my script include function is simple: 

updateAssetScanData: function(assets, recordID) {
        var scanID = recordID;
        var assetArray = assets;
        var assetObject = assets;

        gs.log('@@ record id: ' + scanID);
        gs.log('@@ assetArray: ' + assetArray);
        gs.log('@@ assetObject: ' + assetObject);

    },
 
What I see in the script log is:
gjz_0-1790116989685.png

 

Where did I go wrong?

1 ACCEPTED SOLUTION

gjz
Mega Sage

In case anyone wonders, Google finally found the answer for me.  Here is what I ended up with in the script include.

 

    updateAssetScanData: function(assets, recordID) {
        var scanID = recordID;
        // Convert the Java array into a native JavaScript Array
        var assetArray = Array.from(assets);
        // Safely join the array into a comma-separated string
        var assetListString = assetArray.join(',');
        // Convert the clean JS array to a JSON string
        var assetString = JSON.stringify(assetArray);

        gs.log('@@ record id: ' + scanID);
        gs.log('@@ assetString: ' + assetString);

    },
 
And the results are what I expected, the asset tag is in the string:
gjz_0-1790176277158.png

 

View solution in original post

2 REPLIES 2

abirakundu23
Giga Sage

Hi @gjz ,

Please follow the below suggestion :

Convert the Java array to a standard JavaScript string using String(assets) or "" + assets, then split it into an array:



In your script include : change the below line

updateAssetScanData: function(assets, recordID) {

    var scanID = recordID;

   

    // Convert Java object to a JavaScript string and split into an array

    var assetList = assets ? String(assets).split(',') : [];

 

    gs.log('@@ record id: ' + scanID);

    gs.log('@@ Scanned Assets: ' + assetList.join(', '));

}

Check: verify your syslog after scanning. It may display readable barcode strings .

Please mark helpful & correct answer if it's worthy for you.

 

gjz
Mega Sage

In case anyone wonders, Google finally found the answer for me.  Here is what I ended up with in the script include.

 

    updateAssetScanData: function(assets, recordID) {
        var scanID = recordID;
        // Convert the Java array into a native JavaScript Array
        var assetArray = Array.from(assets);
        // Safely join the array into a comma-separated string
        var assetListString = assetArray.join(',');
        // Convert the clean JS array to a JSON string
        var assetString = JSON.stringify(assetArray);

        gs.log('@@ record id: ' + scanID);
        gs.log('@@ assetString: ' + assetString);

    },
 
And the results are what I expected, the asset tag is in the string:
gjz_0-1790176277158.png