Asynchronous microflow call with known user

1
When a microflow is executed asynchronously (using Core.executeAsync in CommunityCommons) it seems that there is no current user: $currentUser/Name returns 'null' and $currentUser has no roles. Instead, I would like to have it executed with a known user (and a role), for example the user that started the call. How to achieve this?
asked
2 answers
0

Looking at the community commons in my project, I can't find the specified method. The Java action in my Community Commons is called executeMicroflowInBackground. This Java action does more than simply run a microflow asynchronously: e.g. it also ensures they are placed in a queue if there is more than one. The documentation also specifically states this limitation (that the currentUser is unavailable). However, creating your own Java action using Core.executeAsync should be possible, there is nothing to indicate that this will fail.

To extend the current code, you could go two ways:

  • A comment by, presumably Michiel Weststrate, indicates the construction only works with a system context. This may be a leftover from a 3.0 or 4.0 implementation, so you could try to substitute the system context with the correct context.
  • Duplicate the method, but now instead of just sending a single paramter, add the current user to the hash map.
answered
4

The solution in Misc.java is:

        public static Boolean runMicroflowInBackground(final IContext context, final String microflowName,
            final IMendixObject paramObject)
    {
        final ISession session = context.getSession();

        if (paramObject != null)
            session.retain(paramObject);

        MFSerialExecutor.instance().execute(new Runnable() {

            @Override
            public void run()
            {
                try
                {
                    // Disabled (Axel Brink 16-10-2014)
/*                  IContext c = Core.createSystemContext();
                    if (paramObject != null) {
                            Core.executeAsync(c, microflowName, true, paramObject).get(); //MWE: somehow, it only works with system context... well thats OK for now.                       
                    }
                    else
                        Core.executeAsync(c, microflowName, true, new HashMap<String,Object>()).get(); //MWE: somehow, it only works with system context... well thats OK for now.
*/
                    // Modified version below (Axel Brink 16-10-2014)

                    if (paramObject != null) {
                            Core.executeAsync(context, microflowName, true, paramObject).get();                     
                    }
                    else {
                        Core.executeAsync(context, microflowName, true, new HashMap<String,Object>()).get();
                    }

                }
                catch (Exception e)
                {
                    throw new RuntimeException("Failed to run Async: "+ microflowName + ": " + e.getMessage(), e);
                }

                finally {
                    if (paramObject != null)
                        session.release(paramObject.getId());
                }
            }

        });
        return true;
    }
answered