assign existing form method to form created at runtime

I am creating a runtime form and want to use a method from an existing form in the onAction event of a button. I tried:

_method = solutionModel.getForm(_formname).getMethod(_methodname)
_button = _jsForm.newButton("",_x,_y,_c_width_button,_height,_method)

I get the error: method must be a solution method, foundset method or forms own method

Is there a way to do this?

Hi sarar,

I see this is your first post. Welcome to the Servoy forum :)

I assume you want to call a method from another form from your button?
Any event on a form (be it a form event or an event on a form element) can only be linked to it’s own form method, global method or a foundset method (the foundset used on the form).
So if you try to link a method from another form to your custom button then this won’t work.

As a workaround you can create a form method on your form that triggers the other form method like so:

_method = _jsForm.newMethod("function() {forms['" + _formname + "']['" +_methodname + "'];}")
_button = _jsForm.newButton("",_x,_y,_c_width_button,_height,_method)

So as you can see the form method that is being created is calling your other form’s method by code.

Hope this helps.
Don’t hesitate to ask any questions if things are not clear.

Thanks for the welcome! took me a while to get the nerve up to post - I am a Servoy newbie.

The error is gone but the method is still not firing when I click the button. The debugger shows that the function aa is linked to onAction but the method is never called. BTW when I put the method in globals it works without issue.

_method = _jsForm.newMethod("function aa() {forms['itemadd_dtl']['showmultipick'];}")
_button = _jsForm.newButton("",_x,_y,_c_width_button,_height,_method)

suggestions?

sarar:
Thanks for the welcome! took me a while to get the nerve up to post - I am a Servoy newbie.

We were all Servoy newbies once.
Just remember there are no stupid questions, only stupid answers. So I hope I don’t give stupid answers. :)

sarar:
The error is gone but the method is still not firing when I click the button. The debugger shows that the function aa is linked to onAction but the method is never called. BTW when I put the method in globals it works without issue.

_method = _jsForm.newMethod("function aa() {forms['itemadd_dtl']['showmultipick'];}")

_button = _jsForm.newButton(“”,_x,_y,_c_width_button,_height,_method)



suggestions?

Yes, I see I forgot to add some parenthesis in my example code (apart from not giving the function a name, which you already found out I see :) ).
forms[‘itemadd_dtl’][‘showmultipick’] will only give you the function object, it doesn’t execute it. Adding () at the end will execute it.
So your code should be like this:

_method = _jsForm.newMethod("function aa() {forms['itemadd_dtl']['showmultipick']();}")
_button = _jsForm.newButton("",_x,_y,_c_width_button,_height,_method)

Hope this helps.

Thanks much. It Worked