GetIdFromLabel function on Static Entities

By Hanno on 27 Apr

It would be really useful to have a default function on static entities that would allow you to get an entity record based on the label property value. This would work similar to the current default function that returns an entity record based on the Id property.

Current workaround (see image below) is to use an aggregate to match a text value to the label property. If a value is found, we return the id, otherwise we raise a ValueNotFoundException.


And... what happens when the labels are not unique?

There's NO requirement that the Labels be unique. Even worse, even if you make it a unique index, there could be a removed record in the DB with the same label, so the only way to fix the problem is to go into the DB to correct it.

On top of that, why would you want to be looking up static entities directly by their labels? This is a "worst practice". It's one thing to include the SE in a JOIN and use the label as part of the query in a search screen. It's a completely different thing (and a VERY bad one!) to be looking them up by their name as part of the code.

J.Ja

Completely agree with Justin here. The only time that labels need to be queried are when searching for them. If you already know the record you are referring to, then you should use its Id.

In your scenario (whatever that may be), you should be storing its Id as a unique and immutable reference to the record. In fact, that's the only way to do it.