Using GUIDs as Entity Identifiers in OutSystems
A common need in OutSystems is to use a GUID as an Entity Identifier, but unfortunately the platform does not natively support this (there is an Idea on the Ideas board for this). Luckily, it is not too hard to do this yourself, especially when using the CRUD wrapper patterns to save data.
Why would you want to do this in the first place? Using a GUID for an Identifier has a number of advantages, and a disadvantage or two as well.
- When the Id is used in a URL that could be public (such as a link in an email), no one can look at it and guess the value for another record that they should not have access to*.
- Absolutely must-do in mobile applications with offline capabilities, so that the mobile app can generate the ID for new records, sync to the server, and have the same ID post-sync as it had pre-sync.
- Provides many more possible values than Integer or Long Integer can.
- Records can be transported between environments with no reason to remap other records that refer to them, this makes a “lift and shift” of data very simple.
* However, if this is the primary concern, it may be better to have a separate “PublicId” field instead (or a separate entity to map 1:N between Id and Public Id in case you might want to control access very granularly), this way if there is some sort of security concern, these can be changed and the only damage is breaking saved/old links and bookmarks and such, rather than needing to update all foreign keys as well.
- Record lookup in very large tables can be slower. This is not a concern except when we are thinking about very large tables (millions and millions of records).
- Any time you have to type this Id out by hand, or write it down on a piece of paper, you won’t be happy (this is a joke of course, don’t make a decision based on this, but it’s true).
First, we want to make an Entity with an Identifier ready to have a GUID used for it. The OutSystems GenerateGuid() Action (make the reference from the System module) will put hyphen in the GUID (which I generally prefer for readability), so we will change our Id Attribute from “Long Integer” to be of type Text with a length of 36. If you do not want the hyphens, change it to 32. You absolutely must make these changes before you publish the Module with the new Entity in it, the system will not allow you to change the type of the Identifier Attribute once it has been published to the environment.
Next, build out your CRUD wrappers. The GetCanRemove and the Remove CRUD wrappers will be the same. Remember, anywhere that you are checking to see if the Id is null, you will want to use NullTextIdentifier(), not NullIdentifier(). The biggest change will be in the Upsert; here, in the Assign before we call the Create Entity Action, we will need to set the Id of the record to the result from GenerateGuid(). If you do not want the hyphens, use the Replace function to remove them, so it says: TextToIdentifier(Replace(GenerateGuid(), “-”, “”))
Using a GUID for an Identifier has a number of strong advantages, but it is not right for every situation. Now you have the knowledge to know when and how to use it.