Calling MicroFlow from Widget

2
I'm trying to get the result of a Microflow (a string) into my widget. So in my XML I declare a property with type microflow named "fromMicroflow" including returnType type="String" I make a simple microflow that only has a constant String as return value and connect this microflow in the Settings-box of my widget. Now in my js-file I have to following code, but the only value that gets stored in processedOutput is the name of the microflow, not the resulting string. The documentation isn't quite clear about the inputargs, so perhaps that's where I make the mistake, or do I need to call the getMicroFlow function in some different way? Any help appreciated. dojo.provide( "custommodule.MyWidget" ); dojo.require("dijit.dijit"); dojo.require("dijit.Calendar"); dojo.require("dojo.parser"); mendix.widget.declare("custommodule.MyWidget", { addons : [ dijit.Calendar ], inputargs : { minDays : '', maxDays : '', fromMicroflow : {} }, templateString: dojo.cache("dijit", "templates/Calendar.html"), isDisabledDate: function(date, locale){ ... }, getMicroFlowInput : function(){ mx.processor.xasAction({ error : function() { logger.error(this.id + "error: XAS error executing microflow"); }, actionname : this.fromMicroflow, guid : [dataobject.getGUID], callback : dojo.hitch(this, this.processOutput) }); }, processOutput : function(output) { logger.debug(this.id + ".processOutput"); processedOutput = dojo.fromJson(output.xhr.responseText); }, postCreate : function(){ this.actRendered(); }, uninitialize : function(){ } }); function myHandler(id,newValue){ console.debug("onChange for id = " + id + ", value: " + newValue); }
asked
5 answers
10

There are a number of ways to invoke a microflow, and get the result passed to the callback handler. The preferred and easiest way is using mx.processor.get:

mx.processor.get({
    microflow : "Module.Microflow",
    guids     : [ obj.getGUID() ],
    callback  : function(result) {
        // result.actionResult will contain the return value
    }
});

mx.ui.action is an alias of mx.processor.action, but in addiction it provides the possibility to show a progress indicator while the microflow is executed. With mx.ui.action calling the same microflow would look like this:

mx.ui.action("Module.Microflow", {
    params : {
        applyto : "selection",
        guids   : [ obj.getGUID() ],
    },
    callback : function(result) {
        // result.actionResult will contain the return value
    }
});

With mx.processor.action this would do exactly the same as the above calls:

mx.processor.action({
    actionname : "Module.Microflow",
    applyto    : "selection",
    guids      : [ obj.getGUID() ],
    callback   : function(ioArgs, result) {
        // result.actionResult contains the return value
    }
});

It is not recommended to parse the json result yourself, since the response may contain Mendix objects. These objects should be instantiated, before they can be used or manipulated.

answered
6

I believe that you need to set your processedOuput to

dojo.fromJson(dojo.isIE ? response.xhr.responseText : response.xhr.response).actionResult;

As in IE the xhr response (I recall only checking IE 8) is a property called "responseText", where other browsers (checked in chrome and firefox) it is a property called "response".

answered
4

Here are some small fixes which should get you on long way.

  • getGUID is a function, use getGUID()
  • Use either 'guids' with an array or 'guid' with a single integer
  • Your 'myHandler' function seems to be out of your widget's scope. This looks like a bad idea, as it will be placed in the widgets.js main scope, in between the widget constructors. I doubt that is the desired location for such a function.
  • You declare fromMicroflow as an object in your inputargs. Objects aren't allowed in the inputargs, as nothing the widget receives can or will be an object. This means the string will be set to an object variable. I have no idea what this would result in (as the inputargs are copied into this.params for the actual values etc.), but I doubt it'll work as expected. Always set your inputargs as strings, integers or booleans.
  • For 3.0+ we use startup instead of postCreate and update(obj,callback) instead of applyContext(context, callback). Last but not least, actRendered has been renamed to actLoaded. The old versions will be available and work, but note that they are deprecated.
answered
2

Thank you all for your answers; seems like the tutorial is a bit outdated, but this forum is working just fine :-) I'll incorporate these changes this weekend and tell you the result; thanks already.

answered
0

I believe that you need to set your processedOuput to

dojo.fromJson(output.xhr.responseText).actionResult
answered