Retrieve object using GUID in XPath - Mendix Forum

Retrieve object using GUID in XPath

9

Idea

The retrieve operation from database XPath constraint should have the ability to retrieve by the object’s GUID.

Example:

 

Problem

There are a few times where we need to use the Mendix generated GUID to uniquely retrieve the object.

 

Workaround

Currently, we create an additional attribute on the entity named ObjectId and use an After Create microflow + Java action to retrieve the GUID and set it on the object. Then we can use a retrieve by DB on the ObjectId attribute. It would be nice if we did not need to make a new attribute and could just retrieve by the GUID.

asked
5 answers

Runtime API has method to retrieve one object by id:

IMendixObject object = Core.retrieveId(getContext(), Core.createMendixIdentifier(ObjectID));

return object;

Created

Using Tim's comment, this worked for me:

 

// BEGIN USER CODE
		String xpathQuery = "//" + ObjectEntity + "[id='" + ObjectId + "']";
		
		List<IMendixObject> mxObjects = Core.createXPathQuery(xpathQuery)
		        .setAmount(1)
		        .setOffset(0)
		        .setDepth(1)
		        .execute(getContext());
		if(mxObjects == null || mxObjects.isEmpty())
		{
			return null;
		}
		else {
			IMendixObject mxObj =mxObjects.get(0);
			return mxObj;					
		}
		// END USER CODE

 

Created

Mind you: “We have deprecated the Core.retrieveXPathQuery methods in the Java API. Please use Core.createXPathQuery instead. “

 

Created

Thanks Stefan, I was looking for this. I did however had to remove the getType() method from the ObjectType as in my case the ObjectType is just a String. 

Created

We use the following java action in the cases we need this. 

// BEGIN USER CODE
List<IMendixObject> mxObjects = Core.retrieveXPathQuery(getContext(), "//" + ObjectType.getType() + "[id=" + ObjectId + "]");
if(mxObjects == null || mxObjects.isEmpty())
{
	return null;
}
else {
	IMendixObject mxObj =mxObjects.get(0);
	return mxObj;					
}
// END USER CODE

Created