Select to view content in your preferred language

Arcade Expression for Barcode Scanning

4239
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
43 Replies
JustinReynolds
Regular Contributor

@JD1016 Nice; so now you know your logic works with the test data. 

Now what happens when you comment out the test lines (lines 8 and 9) and uncomment out the production lines (lines 4 and 5)? 

Notice that when you test you have a "Results" tab and a "Messages" Tab.  Check those messages too. That is where the console statements are printed.  

Also, an "Unknown" result here is not bad... it might even be expected based on the data you are passing as a lookup value and the lookup values that you have mapped in your dictionary.

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";
};

 

- Justin Reynolds, PE
0 Kudos
JD1016
by
Occasional Contributor III

Okay.  I've attached two screen captures; one for Results and another for Messages.

You are correct that the Unknown appeared in the Results.

Please see attached.

JustinReynolds
Regular Contributor

@JD1016 Perfect.  So this result was anticipated.  Your scanned barcode is null, which is not a key in your dictionary.  Your expression works. Now it is just up to you to scan a barcode that has a lookup value in your dictionary.

Note, that the reason your barcode field (feature.OldMeterNumber) is null is because when you test in the expression editor it is pulling test data from the first feature in your data.  The OldMeterNumber field might not have a value in that feature.  Or maybe you don't have any data collected yet.

So either edit or create feature 1 in your data so that OldMeterNumber has a value that is in your lookup table.  Then when you test here, you won't get an unknown, you will get an address.

- Justin Reynolds, PE
0 Kudos
JD1016
by
Occasional Contributor III

I completely understand your logic on this and it "should" work but when I use the ArcGIS Field Maps app, scan the meter (which is in my office), it populates the barcode field with the accurate code that is in the expression but the Address line remains Unknown.  It does not bring in the corresponding address.

So sorry...I wish I had better news since you are working so hard on this.

0 Kudos
JustinReynolds
Regular Contributor

@JD1016 Just checking that you reloaded the map (or killed Field Maps App and relaunched it) after making your changes to the form to bring in your changes to the mobile map.

- Justin Reynolds, PE
0 Kudos
JD1016
by
Occasional Contributor III

Yes.  I sure did.  I went back to the Maps window, accessed Reload map, and began my scan.  I actually did both...killed and relaunched it.

0 Kudos
JustinReynolds
Regular Contributor

Bummer.  Do you want to post a picture of that barcode.  I'll see what I can come up with on my end.

- Justin Reynolds, PE
0 Kudos
JD1016
by
Occasional Contributor III

Absolutely.  I've attached a screenshot.  Thank you again, Justin.

KenBuja
MVP Esteemed Contributor

Did you check whether the OldMeterNumber field has what you're expecting? Instead of returning the address, just return that field to test it out, skipping over the dictionary and HasKey section.

var lookupValue = $feature.OldMeterNumber;
return lookupValue;

 

JD1016
by
Occasional Contributor III

I did not.  So...just so I understand...swap out all the code that is currently in the expression...and use only the two lines that you've included?  Is that right?

0 Kudos