Arcade Expression for Barcode Scanning

3035
43
Jump to solution
05-25-2022 06:47 AM
JD1016
by
Occasional Contributor III

Hello Everyone,

I need some assistance with code creation whereby after I scan my barcode it will auto-populate another question on the form pertaining to "address".  The calculated expressions are being applied in ArcGIS Field Maps within the Form.  Please see code attached at the bottom of this posting.

I was first able to obtain some helpful code examples via github but I am definitely missing something.  The examples on github center around extracting certain components from the barcode and converting them to descriptions versus the numeric code.  For my situation, the entire barcode is equivalent to the address whereby each barcode is uniquely assigned to this address information.

My workflow for this is as such...I scan the barcode, it populates the "OldMeterNumber" field with 1NF314421199 and in turn populates my "Address" field with 69944 SUNNYFIELD RD.

At this point, it keeps coming up "Unknown".

Thank you for taking the time and assisting.

Jeff

var BARCODE = $feature.OldMeterNumber
var address = BARCODE

var d = Dictionary(
	"1NF314421199", "69944 SUNNYFIELD RD")

if (hasKey(d, address)) {
    return d[address]
} else {
    return "Unknown"
}

   

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

I think I see what happened. You added the trim to Line 1, but then you read the value in without the trim in Line 11. Remove Line 11.

View solution in original post

43 Replies
KenBuja
MVP Esteemed Contributor

If I test it in the Playground, it works properly. Are you verifying the OldMeterNumber is returning the expected value?

playground.png

 

0 Kudos
JD1016
by
Occasional Contributor III

Hi Ken,

Thank you so much for responding to my inquiry!

Yes.  After the scan the barcode question populates with the exact information but "Unknown" keeps coming up.  I've attached a screenshot of what I am seeing on the testing side for me which maintains the "Unknown" issue.

Jeff

0 Kudos
KenBuja
MVP Esteemed Contributor

Make sure the test is giving you the correct Barcode with the Console function. It only reads the first record of your data.

playground.png

  

 

JD1016
by
Occasional Contributor III

I added the same entry for Console as you did and nothing appeared in Messages.

I don't know if this would affect anything on my end or not but...I have only applied this calculated expression inside the ArcGIS Field Maps Form area...not within the corresponding web map pop-ups area.  This is for field crews scanning meter barcodes using the ArcGIS Field Maps app.  I assumed I could go straight to ArcGIS Field Maps (web) and add calculated expressions right from there...harder than I thought it was going to be given the expression is...what I thought anyway...fairly simple.  Much to my surprise. 

0 Kudos
JD1016
by
Occasional Contributor III

Ken,

I've also attached a screenshot of my ArcGIS Field Maps screen and where the calculated expression is being applied...if that helps to understand the entire picture.

Thanks.

Jeff

0 Kudos
JustinReynolds
Occasional Contributor III

Hey @JD1016.  The fact the the console message returned null is the clue that you need.  As Kenbuja mentioned, when you are testing in the expression builder for Field Maps it pulls test data from the first feature in your feature service.  So if the address attribute for your first feature is null or you don't have any feature collected yet it will return null and execute your else statement.

It can be useful to provide test data when developing your arcade expressions

var BARCODE = $feature.OldMeterNumber
// Use in Production
// var address = BARCODE

// Use for Testing
var address = '1NF314421199'

var d = Dictionary(
	"1NF314421199", "69944 SUNNYFIELD RD")

if (hasKey(d, address)) {
    return d[address]
} else {
    return "Unknown"
}
- Justin Reynolds, PE
0 Kudos
JD1016
by
Occasional Contributor III

Hey Justin,

I thought I was on the right track with the github arcade examples for barcodes but I'm not quite sure they apply to my situation.

The code I supplied is looking for 1NF314421199 under $feature.OldMeterNumber to already be present in the database...is that correct?  And...not finding it...delivers the "Unknown"?

If so, how do you get around this issue?  In S123 you can use the pull data function to extract information to populate your form.  I was hoping by using "Dictionary" I could in essence create my own pseudo lookup table that could, in theory, perform the same function as pull data in S123.  That doesn't seem possible because it appears you need data already in the hosted feature layer to create a connection.

Thanks.

Jeff

0 Kudos
JustinReynolds
Occasional Contributor III

@JD1016 If you are going down the path of providing your own lookup values rather than encoding/decoding the barcode itself then I would suggest storing the lookup values in a table and accessing them by creating a featureSet rather than hard coding the dictionary in your arcade expression.

Can you provide a screenshot of what this arcade expression returns?

console("Use Barcode as a lookup value, return the address")

// The real barcode scan for production
// var lookupValue = $feature.OldMeterNumber;
// console(">>> My Scanned Barcode: " + lookupValue);

// Use for Testing, Simulate Barcode Scan, remove in production
var lookupValue = '1NF314421199';
console(">>> My test lookup value: " + lookupValue);

// My Hard Coded Lookup Values
var d = Dictionary(
	"1NF314421199", "69944 SUNNYFIELD RD");

console(">>> My Lookup Values: " + d);
console("   >>> Looking for " + lookupValue + " in " + d);

if (hasKey(d, lookupValue)) {
    console("      >>> " + lookupValue + " was found! " + "Address: " + d[lookupValue]);
    return d[lookupValue];
} else {
    console("      >>> The lookupValue was NOT found!");
    return "Unknown";
};

JustinReynolds_0-1653577132815.png

 

- Justin Reynolds, PE
JD1016
by
Occasional Contributor III

Thank you so much for supplying the code.  That was so unexpected and nice!

I've attached what was returned.

I opened the ArcGIS Field Maps app and the address is auto-filled already in the Address field without even scanning the barcode.  Is that supposed to happen?

Unfortunately, the actual barcode segments do not contain any information specific to the address, account number, route or sequence.  It's rather the "entire barcode number" itself is equivalent to this address, and this account, and this route, and this sequence.  That's why the github examples, in the end, were not what I was looking for since they were extracting specific information from segments of the barcode equivalent to material, pipe size, etc...