Create a progress value with filled attributes of an entity

1
Hello I'm trying to create a progress % when a new entry is created in an entity. This entry is composed by a long list of attributes (below screenshot). Is there a quick way of counting the filled in attributes other than doing a microflow to count them one by one? Ideally I would count these values and then divide by the total number to provide a given % completion (but can accept other solutions). Thanks!
asked
3 answers
2

Nice question. You can create a java action (input any object and result Decimal) for that purpose and replace the code  between //BEGIN USER CODE and // END USER CODE with

 

		// BEGIN USER CODE
		Map<String, ? extends IMendixObjectMember<?>> members = anyobject.getMembers(getContext());
		int totalcount = 0;
		int filledcount = 0;
		for(String key : members.keySet()) { 
			IMendixObjectMember<?> m = members.get(key);
			if (!m.isVirtual()) { 
				totalcount++;
				if (m instanceof MendixString) {
					String s = (String) m.getValue(getContext());
					if (s != null && !s.isEmpty()) {
						filledcount++;
					}
				} else {
					if (m.getValue(getContext()) != null) {
						filledcount++;
					}
				}
			}
		}
		if (totalcount > 0) {
			return new BigDecimal (Math.round(1000.0 * filledcount / totalcount)/ 1000.0);
		}
		return new BigDecimal(0);		
		// END USER CODE

and add this to import (on top)

import java.math.BigDecimal;
import java.util.Map;
import com.mendix.core.objectmanagement.member.MendixString;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.systemwideinterfaces.core.IMendixObjectMember;

 

 

You still have to program the exclusion of the 'progress' attribute yourself.

 

answered
0

As far as I know there is no out of the box way of doing such in Mendix.

But it can be achieved using a custom JAVA action.

This post might help you to get started: https://stackoverflow.com/questions/36912914/count-non-null-fields-in-an-object

answered
0

Hi Chris

Thank you for the incredibly detailed explanation. I'm still failing to deploy your solution (first custom java action in Mendix ever, and I'm no java developer).

I have tried following the tutorial: https://docs.mendix.com/howto/logic-business-rules/extending-your-application-with-custom-java, but I'm getting errors in both, Eclipse and of course Mendix when trying to run the action in a Microflow.

These are my current settings (surely I'm missing something obvious somewhere):

Java Action

 

Eclipse:

 

Mendix Microflow (just for testing the output:

answered