Select to view content in your preferred language

attribute rules: calculate field based on max and right fonction on insert with Arcade

1061
2
Jump to solution
08-28-2022 06:34 PM
Labels (1)
solidsnake
Emerging Contributor

Hello evrybody,

i have somme problème on Arcade statement, i want to insert into a field an expression 

i have à table that containe a field "code_pat" as texte where i put à unique calculated id.

The field is composed of two section : text and number like this (PAT0001 -> PAT (text) and 0001 (number)

on insert in want to automaticly add the value with arcade as attrubute rules. so i have to use 3 functions:

1- right, to get the four digit

2- number, to convert each one into number

3- max, to calculate the maximum and add 1

after that i concatenate "PAT" + the max+1 and put il in the field.

 

My arcade :

var features = FeatureSetByName($datastore,"patrimoine",['*']);

var txt = [];

for(var z=0; z<count(features); z++) {
txt[z]= features.[code_pat].[z]

}

var nb = [];
nb = number (txt);

var num = max (nb);

return "PAT" + text(num + 1,'0000');

solidsnake_0-1661736497277.png

 

 

i got error in this line :

txt[z]= features.[code_pat].[z]

how can i fixe this?

 

thanks you for your helps

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

You're mixing up a few things...

  • txt[z]= features.[code_pat].[z]
    • wrong order of code_pat and z. z is the index of the feature in the featureset, code_pat is the attribute of the feature. 
    • you can either use the dot notation or use the bracket notation to get to the attribute, not both.
    • you have to use brackets to get the index
    • if you use brackets, you have to input a string.
    • this line should look like this:
      • txt[z]= features[z]["code_pat"]
      • txt[z]= features[z].code_pat
  • var nb = [];
    nb = number (txt);
    • Number() only converts a single value, not all elements of an array. you should call Number in the for loop
    • txt looks like this: ["PAT0001", "PAT0002", ...]  Number("PAT0001") returns NaN (not a number), because you didn't eliminate the text part before.

 

Here are better ways to do what you want:

 

If you are working in an Enterprise Geodatabase, create a new database sequence and use it to automatically increment your id field:

var id = NextSequenceValue("NameOfTheDatabaseSequence")
return "PAT" + Text(id, "0000")

 

If you work in a File Geodatabase, you don't need a for loop. You can just order the featureset by your id field (descending), grab the first feature (the one with the max id) and get the number from that:

// get the feature with the highest id
var max_feature = First(OrderBy($featureset, "code_pat DESC"))
// get the number of that id
var max_number = 0
if(max_feature != null) {  // no features in featureset? -> this block is skipped and max_number is 0
    var txt = Replace(max_feature.code_pat, "PAT", "")
    max_number = Number(txt)
}
// calculate and return the new id
return "PAT" + Text(max_number + 1, "0000")

 


Have a great day!
Johannes

View solution in original post

0 Kudos
2 Replies
JohannesLindner
MVP Frequent Contributor

You're mixing up a few things...

  • txt[z]= features.[code_pat].[z]
    • wrong order of code_pat and z. z is the index of the feature in the featureset, code_pat is the attribute of the feature. 
    • you can either use the dot notation or use the bracket notation to get to the attribute, not both.
    • you have to use brackets to get the index
    • if you use brackets, you have to input a string.
    • this line should look like this:
      • txt[z]= features[z]["code_pat"]
      • txt[z]= features[z].code_pat
  • var nb = [];
    nb = number (txt);
    • Number() only converts a single value, not all elements of an array. you should call Number in the for loop
    • txt looks like this: ["PAT0001", "PAT0002", ...]  Number("PAT0001") returns NaN (not a number), because you didn't eliminate the text part before.

 

Here are better ways to do what you want:

 

If you are working in an Enterprise Geodatabase, create a new database sequence and use it to automatically increment your id field:

var id = NextSequenceValue("NameOfTheDatabaseSequence")
return "PAT" + Text(id, "0000")

 

If you work in a File Geodatabase, you don't need a for loop. You can just order the featureset by your id field (descending), grab the first feature (the one with the max id) and get the number from that:

// get the feature with the highest id
var max_feature = First(OrderBy($featureset, "code_pat DESC"))
// get the number of that id
var max_number = 0
if(max_feature != null) {  // no features in featureset? -> this block is skipped and max_number is 0
    var txt = Replace(max_feature.code_pat, "PAT", "")
    max_number = Number(txt)
}
// calculate and return the new id
return "PAT" + Text(max_number + 1, "0000")

 


Have a great day!
Johannes
0 Kudos
solidsnake
Emerging Contributor

Thanks you Johannes,

Your last code worked perfectly, the idea to select the max just byorder, I find that very clever.

But the syntax:

  • txt[z]= features[z]["code_pat"]
  • txt[z]= features[z].code_pat

did not work

any way thanks you very much for your help.

0 Kudos