How can I get association object in JavaAction?

0
I have got IMendixObject (o1) by XPathQuery. Now, I want to get the association object(o2) of o1. How can I do it ?
asked
2 answers
1

Hi,


There are 2 common cases:

1) o1 has a reference (1–1) to o2

Use Core.retrieveByPath(...) with the association name (path).


import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixObject;

import java.util.List;

public IMendixObject getReferencedObject(IContext context, IMendixObject o1) throws Exception {
    // Replace with your module + association name
    String assoc = "MyModule.Entity1_Entity2"; // association from Entity1 -> Entity2

    List<IMendixObject> results = Core.retrieveByPath(context, o1, assoc);

    // For reference (1-1), you typically get 0 or 1 object back
    return results.isEmpty() ? null : results.get(0);
}

2) o1 has a reference set (1–*) to many o2 objects

Same call, but you keep the list:


import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixObject;

import java.util.List;

public List<IMendixObject> getReferencedSet(IContext context, IMendixObject o1) throws Exception {
    String assoc = "MyModule.Entity1_Entity2"; // association from Entity1 -> Entity2
    return Core.retrieveByPath(context, o1, assoc);
}


answered
0

Thank you, Mohamed Shahi. It's working now.

answered