Java Action to set all Booleans of an object to true/false

0
Hi there, I’m looking for a shortcut to set all booleans of an object to a certain value. I know colleagues of mine were working with Java Actions to sum up all decimal/integer values of an entitiy.   Does someone know if such a solution is already existing?  Thanks in Advance!
asked
1 answers
2

I don’t know of any existing solution, but it requires just a few lines of java.

You could create a java action with an object (with entity set to TypeParameter, like for example CommunityCommons.getGUID does) and desired boolean value as input parameters named “myObject” and “wantedValue”. Then in the java action retrieve the attributes for this object and if they’re boolean attributes, set them to the wanted value.

[...]

import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.meta.IMetaObject;
import com.mendix.systemwideinterfaces.core.meta.IMetaPrimitive;
import com.mendix.webui.CustomJavaAction;
import java.util.Collection;

[...]

// BEGIN USER CODE
if (myObject == null)
    return null;           
IMetaObject metaObject = myObject.getMetaObject();
Collection<? extends IMetaPrimitive> metaPrimitives = metaObject.getMetaPrimitives();
for (IMetaPrimitive metaPrimitive : metaPrimitives) {
    if (metaPrimitive.getType() == IMetaPrimitive.PrimitiveType.Boolean) {
        myObject.setValue(this.context(), metaPrimitive.getName(), wantedValue);
    }
}
if (myObject.isChanged())
    Core.commit(this.context(), myObject);
return null;
// END USER CODE

[...]

Now you can call this action in a microflow, with a random object as input and all its boolean attributes will be set to the boolean value you want. Be warned that I mocked this up just now, so it may be bug ridden or violating all kinds of best practices, which the helpful community no doubt will be eager (and in fact is most welcome) to point out, but it seems to work.

answered