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');
i got error in this line :
txt[z]= features.[code_pat].[z]
how can i fixe this?
thanks you for your helps
Solved! Go to Solution.
You're mixing up a few things...
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")
You're mixing up a few things...
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")
Thanks you Johannes,
Your last code worked perfectly, the idea to select the max just byorder, I find that very clever.
But the syntax:
did not work
any way thanks you very much for your help.