How to Identify Object Type by ID

0
I have a TaskQueue object from which I extract parameter object IDs using $ProcessedQueueTask/Arguments. I then retrieve these objects using GetObjectWithId, specifying the type to which the object should be cast. However, even when the specified entity type doesn't match the actual type, I still receive an object. Is there a way in Mendix to determine the type (entity) of an object using its ID, perhaps through model reflection? This way, I could perform a check before retrieving and casting the object.   This is how I retrieve the object:   package onvz_incidenten.actions; import com.mendix.core.Core;import com.mendix.systemwideinterfaces.core.IContext;import com.mendix.systemwideinterfaces.core.IMendixIdentifier;import com.mendix.webui.CustomJavaAction;import com.mendix.systemwideinterfaces.core.IMendixObject; public class JA_GetObjectById_WorkflowContext extends CustomJavaAction<IMendixObject>{    private java.lang.Long ObjectId;     public JA_GetObjectById_WorkflowContext(IContext context, java.lang.Long ObjectId)    {        super(context);        this.ObjectId = ObjectId;    }     @java.lang.Override    public IMendixObject executeAction() throws Exception    {        // BEGIN USER CODE        try{            IMendixIdentifier ident = Core.createMendixIdentifier(this.ObjectId);            IMendixObject receivedObject = Core.retrieveId(getContext(), ident);            return receivedObject;        }catch(Exception e){            return null;        }        // END USER CODE    }     /**     * Returns a string representation of this action     * @return a string representation of this action     */    @java.lang.Override    public java.lang.String toString()    {        return "JA_GetObjectById_WorkflowContext";    }     // BEGIN EXTRA CODE    // END EXTRA CODE}
asked
2 answers
1

The first 8 digits of the id-attribute hardly ever change and represent the entity. If you are certain that the number of objects will never be above 10 million (9digits) you can use the first 8 digits of the id-attribute and compare it to the value of the entity.

This is not considered "clean" programming, but very likely it works and you will never have to change it.

answered
0

If I recall correctly each entity in your modeler has a model guid which is the same for each runtime instance. In the database that guid is used to associate the entity to a short_id, which is different per database (so local/accp/prod). This short_id is used to determine the record id for that specific entity in that database.

In other words, for each entity in your application there is a short id which is used in the generation of the object id.

If you know the object id you can figure out the table by reversing this process and determining the short_id, however its been a long time ago (8 years or so that i looked into this). I believe it had to do with 16 bits for entity and the rest of the bits for uniqueness and a bit shift left operation.

 

I know its not a clear cut answer and solution to your problem (Tim's solution might be good enough), but if you are looking for "technically correct" this might point you in the right direction.

answered