AfterValidate form function


I’ve always heard that VALIDATE function works as if the user has entered the value manually but I have a problem.

I’m in Order form and want to get the extended texts for an item, standard NAV gets it on the AfterValidate trigger and works good, the problem is when I put code lines in the OnLookUp trigger… there I just open the List Item form filtered with the Text var of the trigger… when I get the item (LookupOk), I call the VALIDATE(“No.”, Item.“No.”)… good !!.

At this point the item No. on Sales line is OK but code on AfterValidate trigger is not executed and obviously Extended Texts are not inserted… anyway I copy the same code lines of that trigger in the OnLookUp trigger, after the VALIDATE line but and error is displayed. I will try to explain the error…

  • I want to insert an item whose No. starts with ‘00’, then i put ‘00’ on the No. field of the sales line

  • Then OnLookup of “No” is called and there I execute Item.SetFilter(“No.”, Text + ‘*’). Text has value ‘00’, so the list, show items which “No.” starts with ‘00’. The form is displayed ok

  • Now I choose the item with No. ‘0001’, and error is displayed: ‘Item No. 00 doesn’t exist’ ¿?

This is the trigger…
Item.SETFILTER(“No.”, Text + ‘*’);
VALIDATE(“No.”, Item.“No.”);
InsertExtendedText(FALSE); //this is a default function on form 46

Please somebody help me, I’m starting to get depressed :frowning:

I hope we can help. As the code is pretty straight I would be depressed as well.

I assume that you put your code in the sales line table (T37, OnLookUp trigger for “No.”). However, please try to put your code in the “No. - OnValidate()” trigger in the Sales Order Subform (F46) and let us know the result.

Anyway, you should use GUIALLOWED before you use the FORM.RUNMODAL() command.

Did you try this?

Item.SETFILTER(“No.”, Text + ‘*’);
Text := Item.“No.”;

I think the problem doesn’t lay with the OnAfterValidate trigger not being executed, but with the “No.” field not being updated.

If the OnLookUp trigger is executed the cursor is on the control, when the cursor moves from it, the OnValidate trigger is called and the OnAfterValidate trigger will follow.

I thought of that as well. But WHERE is the difference then between Text := Item.“No.”; and VALIDATE(“No.”, Item.“No.”); ?

As I was writing before, I am not quite sure where Carlos actually put his code.

The difference is CurrForm.UPDATE.

VALIDATE(“No.”, Item.“No.”) is just redundant, since the field validation will be executed anyway. That is, that’s my idea… [8-|]

From the way he described it, I assume Carlos put the code in the control trigger. If he put the code in the table, I don’t beleive the problem would happen that way, but I didn’t try…

I think there is a simple solution to your problem…

Try to put the following code into the OnAfterInput - Trigger in the forms textbox (“No.”):

IF (Type <> Type::Item) or (Text = ‘’) THEN
Item.SETFILTER(“No.”,Text + ‘*’);
Text := Item.“No.”;

You can choose Item as a global variable. If you choose it as a local variable you can ommit the RESET (the RESET is primarily for security reasons because in case of a global variable you or somebody else can never know if this variable is not or will not be used at annother place of the object and some filters, etc. may be set just at the moment when you want to show your Item List…); The OnAfterValidate Trigger will be executed (I’ve tested it :wink: - so you don’t need the InsertExtendedText at this place.
There is normally no write to the database before the input of “No.” in Sales Line, so you get no problem with the FORM.RUNMODAL which would otherwise require a COMMIT leading to other problems eventually…
By the way, to place the code in the table is not possible, because “No.” has a table relation (item, etc.) and when the input is passed to the table the first matching code (valid item no.) is passed even if the input is only one character, furthermore the IF GUIALLOWED … construct is not necessary because the code in the form cannot be accessed by the application server.

First thing… walter, anna, ghuebner… thank you all, you are experienced developers and you are helping me a lot. And sorry for being late in response.

Second thing… I started with NAV development 9 months ago and I still have several basic doubts, I suppose it is because I’m learning myself. Anyway in this case I can’t see the real difference between:

Text := Item.“No.”;
“No.” := Item.“No.”;
VALIDATE(“No.”, Item.“No.”);
… I’m quite lost here :frowning:

Third thing… it finally works jajajaja, the only code line I had to insert was EXIT(true), as Anna said. The final code is placed on the OnLookup trigger of the FORM and the code is:

Item.SETFILTER(“No.”, Text + ‘*’);
Text := Item.“No.”;

I suppose the exit(true) assures that the “No.”.onValidate trigger of the table is called. But that is another doubt I usually ask myself, taking care I want to use this code lines in Orders, Quotes… shouldn’t I place the code on the table instead of on every form?

Thanks :wink:

This is really pleasant, congratulations… - I’m not sure, but I think putting the code into the table trigger will not work, because there is no “text”-variable as in the form trigger…

By the way, I forgot to mention, if you would place the code (which I put above) into the OnAfterInput-trigger, you would have to press the enter-button instead of the F6-button in order to get (nearly) the same functionality… - furthermore you would not change the standard lookup behavior of the field “No.”, which may lead to trouble if someone wants to input G/L Accounts or Resources or something like that … - another disadvantage is for the constellation, if there is already a valid item no. entered in the field and one presses F6 in order to change it, one gets an item list with (normally) only one item (the one already entered) filtered…

I added the code into the table at last, just receiving Text as parameter by reference, so now, I can use the same code for Quotes, Invoices…

You’re right suggesting me not to change the lookup trigger “No.”, I realised yesterday how a resource couldn’t be selected but the functionality I implemented was “ordered” by my boss jajaja, so I had to programm the trigger to lookUp on Resource list, G/L account…

Best regards :wink: