I just did something like this yesterday -- while you cannot set the globalContext directly (afaik), you can send the value back from the ui_template node. Wire the output to a change node which copies the msg.payload to global et voila! Make sure to uncheck the "Pass through messages from input" option, unless you want to validate the contents of every msg coming out of the ui_template node.
Your code is very close to what I did -- however, i was also passing a value to the function. In your case I don't think you need to dynamically assign the value that goes into the global context, so you can just put it directly into the msg to be returned, like so: