If you use a backend workflow to create (or update) the thing, then you can loop it, and therefore check for duplication.
There are quite a few ways ways you can achieve the same thing (depending on your use case), but as an example:
Create a backend workflow with 2 actions: 1 that will create and set a new random code for the ‘thing’ in question, and then another that will schedule itself to run again. The entire workflow should have a conditional to run only when the random code in question is a duplicate (i.e. search for the ‘code’: count >1).
Create another workflow (either in the browser or on the backend) to initially create the thing and set the unique code, and then trigger the backend workflow created above (that will check if the code already exist and, if it does, create a new one, then check it again, and so on).
That way, you will create your thing and assign a random unique code to it, then check that the code didn’t already exist and, if it did then immediately replace it with another one.