Select to view content in your preferred language

Calculated Expression - return OBJECTID

1604
7
03-28-2022 12:40 PM
RonParis
Occasional Contributor

I understand calculated expressions are a fresh (and brilliant addition) to Field Maps, but should it be possible to automatically retrieve the objectid of the feature and use it to populate a field?

My use case is that I want to assign a unique reference to each newly created feature with a concat of text and the objectid. I'm testing at the moment and I am only able to calculate the text part. Interestingly, when I submit the form, I can see that output of what I am after appears briefly during the submission of the form(suggesting to me that objectid has been retrieved), but it doesn't actually calculate the field with the full concat.

TIA

7 Replies
DamonBreen
New Contributor III

I just tried it myself and it seems like it works. It would be something like:

var NewField = concatenate(['Text ',feature.ObjectID]);

return NewField

Hope this helps.

0 Kudos
RonParis
Occasional Contributor

Thanks Damon, but this still doesn't work unfortunately. I have tried the following:

var NewField = concatenate(['Text',$feature.OBJECTID]);

return NewField

 and 

"Text" + $feature.OBJECTID

In both instances, I can still see the correct output showing briefly when the form is submitting.

0 Kudos
DamonBreen
New Contributor III

I saw that it worked in the expression builder and went with it. My assumption is that upon creation of a new feature, the ObjectID may not be created until the form is submitted. If that is the case, and what is causing the problem, you could reference the LAST ObjectID and add 1 to it. This may be problematic if you delete features from the map however. 

0 Kudos
RonParis
Occasional Contributor

That was my thoughts also regarding the allocation of the objectid, but I notice that when I press submit and the submission is taking place, the concat of the text and the objectid does appear for a split second in it's correct form. Not sure if it's a bug or the functionality just isn't there

0 Kudos
DamonBreen
New Contributor III

You're probably right in thinking it's a bug. I saw Thursday where they have actually pulled the full functionality of the calculated expressions because of  a "Show Stopper" error. It may just be that something is broken, because this is the third time now where I have made a calculated expression, and the test button in the expression builder shows it is working, but when I real world test it, it does not work. I was able to get the most recent idea I explained to work, but when I sort the globalID's of the previous records, I can only get it to pull the first record, and not the last. Not sure what kind of command I need to use to reverse it for it to do what you're trying to do, but this is what I came up with. Maybe you can finish it off.

var AllRecords = FeatureSetByName($map, "Odorizer");// pull all record of the 'Odorizer' feature
var LastObjectID = Top(OrderBy(AllRecords, "ObjectID"),1);// sort all pulled records by ObjectID


if (IsEmpty(AllRecords)) { // if there are no previous records...
return '1'; // then this must be the first, so the ObjectID should be 1
} else {
for (var Rec in LastObjectID){ // loops through to the only record
var LastRecord = Rec.ObjectID //gets the objectID of the only record
var CurrentObjectID = LastRecord +1 // adds 1 to the ObjectID of the last record

concatenate("Text ", CurrentObjectID); //concatenates text with what should be the current GlobalID
return CurrentObjectID; // return value


}
}

MarkBennett
Occasional Contributor

I know this is an old question, but I thought I'd provide an answer in case anyone else needs it. The following code worked for me to make a facility id field using objectid:

 

var fs_data = FeatureSetByName($datastore, "Example_Data", ["OBJECTID"], false)
var id = max(fs_data, "OBJECTID")
id = id + 1
var string_id = text(id)
var fid = concatenate("F-", string_id)
return fid

 

I hope this works for anyone else who needs it!

0 Kudos
NickShannon2
New Contributor III

This post is useful — thanks. 

 

I've read that it's not a good idea to use Object ID as a bases to generate unquie IDs. 

This is a cardinal rule! Never use any internal IDs from a geodatabase or any other database for your unique IDs unless you are 100% certain that it will not be automatically recalculated by the application in the future. An example of this is the OBJECTID attribute in a geodatabase. Because the OBJECTID attribute is internally used by the geodatabase, on occasion in may be recalculated by ArcMap.

Thoughts/opinions on this? 

 

0 Kudos