Method on the fly

Hello!

I have a problem… i try to create a new method with newMethod() and it doesn’t work…

_method = globals._dynamicForm.newMethod('function _nextStep_' + globals.getUUID() + '() { application.output("Method _nextStep"); }')
_button = globals._dynamicForm.newButton('Continua >',0+_marginLeftDTL,globals._hRowDTL*_currentRowDTL,globals._wColumnLabelDTL+globals._wColumnDataDTL,globals._hRowDTL*2,_method)
_button.onAction = _method;
_button.name = "_nextStep_" + globals.getUUID()
_button.enabled = true

thanks

Hi,

You are using a UUID in the method name. This UUID has hyphens (-) in there and those are not supported in a function name so you should strip them out.
In fact hyphens are not really supported in any object name.

Try the following code instead:

_method = globals._dynamicForm.newMethod('function _nextStep_' + utils.stringReplace(globals.getUUID(), "-","") + '() { application.output("Method _nextStep"); }')
_button = globals._dynamicForm.newButton('Continua >',0+_marginLeftDTL,globals._hRowDTL*_currentRowDTL,globals._wColumnLabelDTL+globals._wColumnDataDTL,globals._hRowDTL*2,_method)
_button.onAction = _method;
_button.name = "_nextStep_" + utils.stringReplace(globals.getUUID(), "-","")
_button.enabled = true

Hope this helps.

Hi, thanks for your answer…but it doesn’t work…

i try also without the getUUID but nothing…

Did you call controller.recreateUI() on your changed form?

Yes but nothing…

_method = globals._dynamicForm.newMethod('function aMethod(event){application.output("Hello world!");}');
_button = globals._dynamicForm.newButton('Continua >',0+_marginLeftDTL,globals._hRowDTL*_currentRowDTL,globals._wColumnLabelDTL+globals._wColumnDataDTL,globals._hRowDTL*2,_method)
_button.onAction = _method;
_button.name = "_nextStep_" + utils.stringReplace(globals.getUUID(), "-","")
_button.enabled = true
controller.recreateUI() 
return false

Hi,

What exactly doesn’t work ?
The button doesn’t show up ? Or clicking the button doesn’t seem to trigger the method, i.e. you don’t see the application.output ?

Hi,

I simplified your example and was able to reproduce your problem when the global form var did not contain a valid object. Like this it works:

	if (!globals._dynamicForm) {
		globals._dynamicForm = solutionModel.getForm('frmWhatEver');
	}
	var method = globals._dynamicForm.newMethod('function myMethod(event) { plugins.dialogs.showInfoDialog("Test","Hello World");}');
	servoyDeveloper.save(globals._dynamicForm);
	var button = globals._dynamicForm.newButton('Hello World',250,250,80,20,method);
	button.name = 'myNewButton';
	currentcontroller.recreateUI();

Perhaps the assumption that globals.dynamicForm is filled is mistaken?