OnNew event from Method

I added a “New” button to my controller. I noticed, however, that when the button is clicked and the “New” method runs, it doesn’t trigger the onNewCmd event for forms where that’s set.

Is there a way to have the one common method on the controller add a record, but also tell it to trigger the onNewCmd event if one is specified for the current form?

What you call the onNewCmd event is not an event (or maybe you can call it an event, I don’t know). What this does is ‘capturing’ the menu choice ‘new’ or the equivalent command key that creates a new record and replaces it with your method.

To further Marcel’s post - so you should hook up both the button’s “onAction” property and the form’s “onNewRecordCmd” property to the same method.

Bob

OK, I may be out in left field on this one, but…

If I want to put a “New” button on the controller, is there any way to have it run the onNewCmd method of the current form?

I’m trying to avoid the following workarounds: Putting a “New” button on every layout, or having the “New” button method say “if contacts form, do this; if customer form, do this; if bills form, do this…”

I’d rather have the button do the exact equivalent of “Ctrl + N”, and then manage special cases at the form level. (Actually, truth be told, I’d like the users to just use the keyboard or menu, but the client wants pretty buttons. :? )

If it can’t be done, I can work around as above, and maybe beg for a feature in a later release, but I don’t want to go the long way around if the preferred way works.

Sure you can.

You can find the name of your current form with currentcontroller.getName().

You can ALWAYS access the method on the current form with . So that’s no issue.

What you can do in the method of the ‘new’ button in your custom controller is ```
forms[currentcontroller.getName()].yourNewMethod();

That sounds perfect! Thanks!

Of course, being shameless, I have one more thing to ask for. :wink:

Suppose I have a method in SOME forms called newRecord, but on others I just want to create a new record and be done with it. Is there a step I could use to find out whether the form has a ‘newRecord’ method? That way, if there’s not a specific method, I could just run the Global version.

If it’s just a matter of remembering to make a newRecord method for every form, that’s certainly acceptable. If there’s a way to check for the existence of methods on a particular form, though, this will open a whole realm of possibilities for dynamic branching by exception.

hmm, not sure but you can try this

if (forms[currentcontroller.getName()].yourNewMethod) {
  do whatever you want;
} else {
  do whatever you don't want;
}

That doesn’t seem to work. Thanks, though.

I tried this, too. Turns out, though, that the error capture won’t prevent javascript errors from showing.

Is there another way?

application.setErrorCapture(true)
forms [currentcontroller.getName () ].rowNew ();
if ( application.getLastErrorCode()!= 0 )
{
    currentcontroller.newRecord()
}
application.setErrorCapture (false);
if ( "rowNew" in forms [currentcontroller.getName () ] )
	{
		forms [currentcontroller.getName () ].rowNew ();
	}
	else
	{
		currentcontroller.newRecord();
	}

setErrorCapture is not for javascript errors.

but youre problem can be handled an other way:

if(forms[currentcontroller.getName()].newRow)
{
    forms[currentcontroller.getName()].newRow();
}

It would appear the code in my last post doesn’t work in Find mode. Oddly enough, in Find, the “In” function seems to return true no matter what method name is inserted.

Just FYI for those following this thread.