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"
}
Solved! Go to Solution.
That's the problem. The Dictionary is looking for an exact match. You can use the Trim function
var lookupValue = Trim($feature.OldMeterNumber);
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.
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.
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...
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.
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";
};
Thanks again, Ken!!
Regarding the Mid statement....where in the code (line) could I place my Mid?
Anywhere after line 1, depending on where and how you want to use it.