Select to view content in your preferred language

Assistance with arcade expression for attribute rule

327
7
Jump to solution
07-29-2025 05:30 AM
HannahLegacy1
Emerging Contributor

Hello, 

My attribute rule stopped working when my company upgraded from Enterprise 10.9 to 11.3 (not sure if that is relevant). This code was not written by me, but my predecessor. My knowledge of arcade is limited, but it looks like it should work and I didn't change anything?

The goal of this code is to create a simple ID number for a feature at a specific site. When a feature is created, this code should assign it a number sequentially (if there are 5 features, the next feature created should have a feature ID of 6).

var sid = $feature.site_id

var sql = "site_id = '" + sid + "'"

var sitefilter = Filter($featureset, sql);

var idseq = Max(sitefilter, "ft_id");

//Console(idseq + 1)

    return idseq + 1;

0 Kudos
2 Solutions

Accepted Solutions
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Try the code below and if it works, great, otherwise more information about the data will be needed to better troubleshoot.

var sid = $feature.site_id
/*
Not sure if the sid is a text field but if it is then the code below
should work
*/
//if( !IsEmpty( sid ) ){ sid = Concatenate(' ',sid,' ') }
var sitefilter = Filter($featureset, "site_id = @Sid")
var idseq = Max(sitefilter, "ft_id")
//Console(idseq + 1)
return idseq + 1

The solution above is not the solution to the posters issue but it is a solution in terms of code structure for anyone who has similar issues or is looking for similar behavior.

View solution in original post

HannahLegacy1
Emerging Contributor

Hey there,

I wound up getting in touch with esri support and we got the code to work. You were definitely right about @ to reference the value--just needed to rearrange the syntax a little to make it work. Thank you for all your help!

Solution as follows:

var sid = $feature.dacf_site_id
var sql = "dacf_site_id = " + "@sid";
var sitefilter = Filter($featureset, sql);
var idseq = Max(sitefilter, "dacf_ft_id");
//Console(idseq + 1)
    return idseq + 1;

 

View solution in original post

0 Kudos
7 Replies
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Try the code below and if it works, great, otherwise more information about the data will be needed to better troubleshoot.

var sid = $feature.site_id
/*
Not sure if the sid is a text field but if it is then the code below
should work
*/
//if( !IsEmpty( sid ) ){ sid = Concatenate(' ',sid,' ') }
var sitefilter = Filter($featureset, "site_id = @Sid")
var idseq = Max(sitefilter, "ft_id")
//Console(idseq + 1)
return idseq + 1

The solution above is not the solution to the posters issue but it is a solution in terms of code structure for anyone who has similar issues or is looking for similar behavior.

HannahLegacy1
Emerging Contributor

Hi! Thank you so much. The sid field is actually numerical. My coworker and I reworked the code to this:

 

var sid = $feature.dacf_site_id
var sql = "dacf_site_id = " + sid;
var sitefilter = Filter($featureset, sql);
var idseq = Max(sitefilter, "dacf_ft_id");
//Console(idseq + 1)
    return idseq + 1;

 which, according to Expression Builder, is a valid expression. However, when we try to save it, we get the same error (002717, Invalid arcade expression, arcade error: Invalid where clause) and it does not allow us to save the attribute rule. 

Not sure why it would confirm it is valid on one screen and deny it on the other.

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Try using the @ like the example below.

"dacf_site_id = @Sid"

@ references the value so if it is text then it will make it text, otherwise it will be any other value.

The only other thing is potentially the field name itself. Try getting a list of field names to see if the field is called that or if you are trying to use an alias instead.

0 Kudos
HannahLegacy1
Emerging Contributor

Hi there,

Thank you--I just tried it and it does allow me to save the attribute rule when I install the @ symbol. Unfortunately though, the code did not populate the feature id field with anything, just a null value.

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Then it may require you to configure your code so that if the sid is not null then filter the records, otherwise return, like so.

var sid = $feature.site_id
if( !IsEmpty( sid ) ){ 
    var sitefilter = Filter($featureset, "site_id = @Sid")
    sid = Max(sitefilter, "ft_id")+1
}
else{ Console('Enter a default value') }
return sid

Another option is to setup a NextSequenceValue which will automatically update to next value stored in the table.

var sid = $feature.dacf_site_id
if( IsEmpty( sid  ) ){ sid = NextSequenceValue('dacf_site_id') }
else{ return }
0 Kudos
HannahLegacy1
Emerging Contributor

Hey there,

I wound up getting in touch with esri support and we got the code to work. You were definitely right about @ to reference the value--just needed to rearrange the syntax a little to make it work. Thank you for all your help!

Solution as follows:

var sid = $feature.dacf_site_id
var sql = "dacf_site_id = " + "@sid";
var sitefilter = Filter($featureset, sql);
var idseq = Max(sitefilter, "dacf_ft_id");
//Console(idseq + 1)
    return idseq + 1;

 

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

I am glad you got it working. In addition to your solution I am going to mark the one that I previously posted as well since both answers are technically solutions in terms of formatting but not in terms of the exact use case.

I will make note to anyone else who has similar issues and comes across this post.