Select to view content in your preferred language

Arcade Expression for Barcode Scanning Using Mid

1200
10
Jump to solution
06-01-2022 10:54 AM
JD1016
by
Occasional Contributor III

Hello,

This post is a continuation of an already solved posting.  The earlier post was expertly solved with the tag team combination of Justin Reynolds and Ken Buja.  Although this question is correlated to another posting, I asked the question after the initial one was already solved.  It just didn't seem right to carry on with the original post and not give everyone a fresh look at the new inquiry.  On with the question...

The attached code below is working off of a barcode scan event within ArcGIS Field Maps.  The barcode number is the following 1NF314421199 and I am attempting to use "Mid" to strip away the "1NF" leaving the rest of the barcode intact.  Upon scanning, this number would be recognized in the dictionary and my corresponding Address field (form question) populated with 69944 SUNNYFIELD RD.  Unfortunately, after the scan, the original barcode still comes up instead of the desired truncated version of 314421199 I am attempting to obtain from Mid.

Ken Buja graciously ran it through the Arcade Playground and it worked as expected.  However, for some reason, unknown to me, it will not behave when using the field app.

Thank you all for any assistance offered!

Jeff

 

var lookupValue = Trim($feature.OldMeterNumber);
var meter = Mid(lookupValue, 3, 9);

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

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

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

OK. Now, for the address question, use your old code:

var lookupValue = Trim($feature.OldMeterNumber);
var meter = Mid(lookupValue, 3, 9);

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

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

 

For the Old meter number question, use the short code:

var lookupValue = Trim($feature.OldMeterNumber);
var meter = Mid(lookupValue, 3, 9);
return meter

 

Or, if you can't add code to that question, create a new question ("Old Meter Short Code" or something like that).


Have a great day!
Johannes

View solution in original post

10 Replies
JohannesLindner
MVP Frequent Contributor

So you want the truncated barcode to show up in your table? $feature.OldMeterNumber should be 314421199 after you run the rule?

Then you have to return it:

 

var lookupValue = Trim($feature.OldMeterNumber);
var meter = Mid(lookupValue, 3, 9);

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

var address = "Unknown";

if (hasKey(d, meter)) {
    console("      >>> " + meter + " was found! " + "Address: " + d[meter]);
    address = d[meter];
} else {
    console("      >>> The meter was NOT found!");
};

// you can return multiple field values with a dictionary
// if this doesn't work in Field Maps, create a second rule 
// on the field OldMeterNumber where you just return meter
return {
    "result": {
        "attributes": {
            "Address": address,
            "OldMeterNumber": meter
        }
    }
}

 


Have a great day!
Johannes
0 Kudos
JD1016
by
Occasional Contributor III

Hello Johannes,

Thank you so much for responding to my posting.  I really appreciate your time.

That is correct...I would like the truncated version of the barcode...3124421199 to result after the barcode scan event versus the addition of the first three characters "1NF".

I implemented the code you included above, used the Field Maps app to scan my meter barcode, and the result was the following...

The Old Meter Number field (form question) was populated with the entire barcode of 1NF314421199

And the Address field (form question) was populated with this information... {"result":{"attributes":{"Address":"69944 SUNNYFIELD RD","OldMeterNumber":"314421199"}}}

I am by far a complete novice when it comes to Arcade and Calculated Expressions.  Can you please take a look at the code and offer some additional insight on how it can be further modified to populate the Old Meter Number with 314421199 and the Address of 69944 SUNNYFIELD RD?

Thank you again so much for assisting.

Jeff

0 Kudos
JohannesLindner
MVP Frequent Contributor

I'm unfamiliar with FIeld Maps. Do you implement the code as Attribute Rule on a feature class (that's what I'm assuming)?

Try creating a new Attribute Rule, leaving the field empty (forgot that in my first answer).

JohannesLindner_0-1654177221541.png

 

If that fails, do it in 2 rules:

In the first rule (field "Address"), use your old code.

In the second rule (field "OldMeterNumber"), copy the first two lines and then return meter.


Have a great day!
Johannes
0 Kudos
JD1016
by
Occasional Contributor III

No.  The code, or Calculated Expression, is being entered and applied within the ArcGIS Field Maps web app form itself (please see attached screen capture).  Once applied within that setting, you then go to your Field Maps app, reload your map, and attempt the scan in that environment.  The meter I am testing is actually in my office and once this returns the expected result can be implemented with our other meters.

0 Kudos
JohannesLindner
MVP Frequent Contributor

Ah, ok. As I said, completely unfamiliar with Field Maps, so I'm probably not going to be a huge help here. But still:

var lookupValue = Trim($feature.OldMeterNumber);
var meter = Mid(lookupValue, 3, 9);
return meter

 

Try that as expression on OldMeterNumber (if it will let you do that). If that doesn't work, try creating a new field (that isn't populated by any other means) and use the expression on the new field. If that still doesn't work, I'm out.


Have a great day!
Johannes
0 Kudos
JD1016
by
Occasional Contributor III

Don't sell yourself short Johannes.  Any assistance you are attempting to provide is incredibly appreciated and goes far and above my current capabilities working in Arcade.

So just to make sure I get this right...I entire in the above code as such...

var lookupValue = Trim($feature.OldMeterNumber);
var meter = Mid(lookupValue, 3, 9);
return meter;

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

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

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

Just the first 3 lines.


Have a great day!
Johannes
0 Kudos
JD1016
by
Occasional Contributor III

Okay...I modified the code so it only contains those three lines as shown below.  The result is the Mid function worked and the address question was populated with the truncated barcode value of 314421199.  Now to get them all to play nice together...

var lookupValue = Trim($feature.OldMeterNumber);
var meter = Mid(lookupValue, 3, 9);
return meter

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

OK. Now, for the address question, use your old code:

var lookupValue = Trim($feature.OldMeterNumber);
var meter = Mid(lookupValue, 3, 9);

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

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

 

For the Old meter number question, use the short code:

var lookupValue = Trim($feature.OldMeterNumber);
var meter = Mid(lookupValue, 3, 9);
return meter

 

Or, if you can't add code to that question, create a new question ("Old Meter Short Code" or something like that).


Have a great day!
Johannes