Arcade Expression for Barcode Scanning

3465
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
KenBuja
MVP Esteemed Contributor

That's the problem. The Dictionary is looking for an exact match. You can use the Trim function

var lookupValue = Trim($feature.OldMeterNumber);

 

0 Kudos
JD1016
by
Occasional Contributor III

I modified the first line of the code as you indicated (see attached to verify) and the address is still populated with "Unknown".

0 Kudos
KenBuja
MVP Esteemed Contributor

So what does this return? Make sure you use the backtick (`) character for Template Literals

 

var lookupValue = Trim($feature.OldMeterNumber);
return `'${lookupValue}'`;

 

If it doesn't return  '1NF314421199' then something else is going on.

0 Kudos
JD1016
by
Occasional Contributor III

I modified the code (see new attachment).  It returns the barcode number in single quotes in the address field.

0 Kudos
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.

JD1016
by
Occasional Contributor III

Okay.  That worked!  I've attached what the final code was that successfully applied the address with the corresponding barcode.  Thank you so much for sticking with me on this Ken.  I really appreciate your time and patience.  This will make our system so much better.

I do have another corresponding question concerning the same code if I may...

If I wanted to strip away the "1NF" from the barcode so it only displays "314421199"...can that be done?

I seem to recall a Mid function...

0 Kudos
KenBuja
MVP Esteemed Contributor

It's much easier to read your code when you insert it into your message as you did in your initial post rather than sending a screen shot. And that allows for copying code. That's why I didn't see the problem at first. You can remove a bunch of lines (3-17, 21, and 22) since they're either duplicative or for code testing.

Yes, you can use the Mid statement to do that.

0 Kudos
JD1016
by
Occasional Contributor III

That's right.  I forgot about that.  Let me remedy. 

var lookupValue = Trim($feature.OldMeterNumber);

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

if (hasKey(d, lookupValue)) {
    console("      >>> " + lookupValue + " was found! " + "Address: " + d[lookupValue]);
    return d[lookupValue];
} else {
    console("      >>> The lookupValue was NOT found!");
    return "Unknown";
};
0 Kudos
JD1016
by
Occasional Contributor III

Thanks again, Ken!!

Regarding the Mid statement....where in the code (line) could I place my Mid?

0 Kudos
KenBuja
MVP Esteemed Contributor

Anywhere after line 1, depending on where and how you want to use it.

0 Kudos