Mendix object from MendixIdentifier string

Hi All, I am using the CommunityCommons getOriginialValueAsString to retrieve the original value of a reference. The result I get is a string value for example: "[MendixIdentifier:: id=25332747903959156 objectType=CountriesStates.State entityID=90]" My question is how can I retrieve the original State object using the string above, either by microflow or java action. I have extracted the "id" from the string with substring but if I do a retrieve action I get an Incompatible types State,Integer error. In java to do a Core.RetrieveById i need the MendixIdentifier to be an object. I can create the MendixIdentifier from string but I need a guid. Any ideas? Thanks and much appreciated.
3 answers

There is no function I know of that will return the object based on the ID directly. But, if you extract the id from the identifier object that you get as a string, the id can be used in an Core.retrieveXpathQuery like the simple version below:

List<IMendixObject> mxObjects = Core.retrieveXPathQuery(getContext(), "//<ModuleName>.<EntityName>[id="+<ID as string>+"]");
    IMendixObject mxObj =mxObjects.get(0);
    return mxObj;

In this java action the input parameter is the ID as string from the identifier object and the return value is the entity that you are searching for. The result of the query will be a list of IMendixObjects, but as there will be only 1 object returned, I just take the first object from the list and then this will return the object based on the input id value. The example is meant just as an example to demonstrate the function, a check for empty list could be useful and the retrieve could use a limit etc. But you can use this a a reference for the implementation of the real function.


There is a direct function which returns a list of mendixObject or a single object

List<IMendixObject> objectlist = Core.retrieveIdList(getContext(), ids);
IMendixObject myobject = Core.retrieveId(getContext(), id);

Both solutions suggested by Erwin and Chris are good solution to do this. One addition for your specific case, from your question you are saying you are trying to retrieve the original value, it is probably better to create your own Java action to eliminate the string mutation activities. That way if the platform notation ever changes your action will keep working.

The code below is capable of returning the Original value, exactly the same way as the function for community commons. However you must make sure that the input memberName is the name of an association. The action will then return null if it was empty or the actual object that was associated.

    IMendixIdentifier myId = (IMendixIdentifier) this.MyInputObject.getMember(this.getContext(), this.memberName).getOriginalValue(this.getContext());
    if( myId == null )
        return null;

        return Core.retrieveId(this.getContext(), myId);