Select to view content in your preferred language

Custom primary key for relationships?

590
3
Jump to solution
08-22-2022 12:37 PM
DamonBreen
Regular Contributor

Hi all,

I posted a few days ago about an issue I was having with related records in an arcade expression not evaluating correctly. I came to find out that the globalID (which I always use as my primary key) was not populating until after the submission of a feature, therefore affecting my sql statement to look into the related records. To fix this, I created a new primary key field and used that for my relationships. I then added this code: 

var AllLeaks = FeatureSetByName($map,"Gas Leak")
var LeakCount = Count(AllLeaks);
var NewestLeak = First(OrderBy(AllLeaks,"LeakID DESC"));
if(LeakCount == 0){
return 1
} else {
return sum(NewestLeak.LeakID, 1);
}

... to make my own auto-incrementing key. The problem with this is, that if I have to go back into an already-submitted feature to edit something, it renumbers this LeakID, and the relationships are gone. Is there a way with Arcade that I can tell it to only evaluate this code on the creation of a new feature and not when updating it? or is there a better way of doing what I'm doing? I also tried to have it do a compare of the object ID of this feature to the newest feature to stop it from updating, but that doesn't seem to work. I would like to keep the end users from having to enter their own leak number if possible.

 

Thanks,

Damon

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Check if there is already a LeakID. If so, just return that, essentially changing nothing.

if($feature.LeakID != null) {
    return $feature.LeakID
}
var AllLeaks = FeatureSetByName($map,"Gas Leak")
var LeakCount = Count(AllLeaks);
var NewestLeak = First(OrderBy(AllLeaks,"LeakID DESC"));
if(LeakCount == 0){
    return 1
}
return sum(NewestLeak.LeakID, 1);

 


Have a great day!
Johannes

View solution in original post

0 Kudos
3 Replies
JohannesLindner
MVP Frequent Contributor

Check if there is already a LeakID. If so, just return that, essentially changing nothing.

if($feature.LeakID != null) {
    return $feature.LeakID
}
var AllLeaks = FeatureSetByName($map,"Gas Leak")
var LeakCount = Count(AllLeaks);
var NewestLeak = First(OrderBy(AllLeaks,"LeakID DESC"));
if(LeakCount == 0){
    return 1
}
return sum(NewestLeak.LeakID, 1);

 


Have a great day!
Johannes
0 Kudos
DamonBreen
Regular Contributor

Very weird, I thought I had tried this basically already with IsEmpty() and it did not work, but this in fact does work.

Thank you very much!

0 Kudos
JohannesLindner
MVP Frequent Contributor

Also, not sure if that applies to FIeldMaps, but in an Enterprise GDB, you can create a database sequence and then call NextSequenceValue in Arcade instead of loading and ordering the whole featureset.


Have a great day!
Johannes
0 Kudos