OnLookup, EXIT, SourceExpr questions

kytohkytoh Member Posts: 11
Hi all, I just started learning and working with Navision few weeks ago. I have some .NET background.
For below case, I am using Navision 5.0.
I have some questions regarding the behavior of OnLookup trigger in a textbox of a report request form.
Basically, this textbox will open up a modal dialog of Work Schedules, user will choose the work schedule and the work schedule code will be populated back to the textbox.
Below are some methods that I tried to play around with.

1. Assigning selected code to Text, followed by EXIT.
This is the method that I found out works best, thanks to the Internet. This is possibly the most proper/standard method, correct me if I am wrong.
Employee - OnLookup(VAR Text : Text[1024];) : Boolean
IF FORM.RUNMODAL(FORM::"Work Schedules", WorkSchedules) = ACTION::LookupOK THEN BEGIN
  Text := WorkSchedules.Code;
  EXIT(TRUE);
END;

2. Assigning selected code to the SourceExpr variable i.e. "WorkScheduleCode", followed by EXIT.
Problem: The text in the textbox is empty after choosing a value from the lookup form.
Employee - OnLookup(VAR Text : Text[1024];) : Boolean
IF FORM.RUNMODAL(FORM::"Work Schedules", WorkSchedules) = ACTION::LookupOK THEN BEGIN
  WorkScheduleCode := WorkSchedules.Code;
  EXIT(TRUE);
END;

3. Assigning selected code to the SourceExpr variable i.e. "WorkScheduleCode", and no EXIT statement.
The text in the textbox is filled out properly based on chosen value from the lookup form.
However, based on some readings, I believe due to the missing "EXIT" statement, form level's OnValidate and several other built-in functions will not be executed.
There is a very minor issue with this method on specific scenario, but I will leave it for now as it is solved by using method 1 and it might be too long to explain here.
Employee - OnLookup(VAR Text : Text[1024];) : Boolean
IF FORM.RUNMODAL(FORM::"Work Schedules", WorkSchedules) = ACTION::LookupOK THEN BEGIN
  WorkScheduleCode := WorkSchedules.Code;
END;

My questions are:
a. What is the difference in terms of backend process in assigning the value to the "Text" in OnLookup (method 1) as compare to assigning the value to the "SourceExpr" variable directly (method 2 & 3)?
b. The textbox is filled out properly when there is no EXIT statement on method 3. But adding the EXIT statement will make the textbox empty as shown in Method 2. How does the EXIT statement affect this?


I will use method 1 for future coding, but I just want to understand more on the process because the previous programmer was using method 3, and it has a very minor issue which is only noticed by the user now.
Hope someone can provide some explanation, thank you!

-KY

Answers

  • IsitarIsitar Member Posts: 29
    Hello and welcome to mibuso

    Interesting fact you found here :)

    I did testing about this by myself.
    What I found out is that if you have your first method and you open the form, choose a record and click OK and then in the starting form you press ESC, it works. Actually I have no Idea why,
    I can only guess: Navision saves your correct value in the field and then overwrites it with your new value but doesn't leave the field. You can test this by yourself if you add the field twice.

    Another interesting thing that I found out is, if you exit with false (EXIT(FALSE)) it works perfectly fine.

    So here comes my these:
    If you exit the OnLookup trigger with true, Navision thinks you're done with your work and has nothing to do, and sets your current value (in your case '') to the field.
    If you exit with false, Navision reassignes the value to the field value.
    a. What is the difference in terms of backend process in assigning the value to the "Text" in OnLookup (method 1) as compare to assigning the value to the "SourceExpr" variable directly (method 2 & 3)?
    In assigning nothing. You can use your code in a codeunit and it works. Only in Forms it doesn't work this way.
    b. The textbox is filled out properly when there is no EXIT statement on method 3.
    But adding the EXIT statement will make the textbox empty as shown in Method 2.
    How does the EXIT statement affect this?
    see above

    Cheers
    Greetings from Switzerland
  • vaprogvaprog Member Posts: 1,141
    I think you need to distinguish the value in the control from the value in the SourceExpression.

    This is what I suppose happens:
    • The control loads it's value from the SourceExpression
      1. You run the lookup and TRUE is returned (method 1 with LookupOk):
        The value in the Text parameter is loaded into the control, the underlying SourceExpression remains unchanged. If you press Esc, the value of the SourceExpression is reloaded. If you do anything to confirm the value, the control stores it to the SourceExpression and continues to validate it.
      2. You run the lookup and FALSE is returned (method 1 with LookupOk=FALSE, method 3):
        Nothing happens to the SourceExpression at this point, nothing happens to the value in the control, nor to the state of the control. Just to be sure, the control reloads it's value from the SourceExpression. If you changed the value of the SourceExpression in the lookup, the modified value is shown, but the control is not in an edit state and when you leave that field, nothing gets validated.

    The effect of Method 2 follows from this model.
    If you selected a value in method 2 (TRUE is returned), the text in the textbox is not empty, but is unchanged from when you run the lookup. The state of the control changed to edit mode, though. Just as if you had selected the value that was in the control from the lookup table with a properly working lookup as in Method 1 or configured through properties on the table/field. But, after all, that's exactly what you tell the system. By returning TRUE you tell the system: The value in the Text parameter is the value I selected in the lookup.
  • DakkonDakkon Member Posts: 192
    You should always use method 1 for handling custom textbox lookup behavior if feasible. This conforms to the expected behavior of Navision and allows certain property settings to function as they are supposed to (such as the "ClearOnLookup" of the textbox). If you set the previously mentioned property to No on your textbox, the other methods would break that functionality, whereas method 1 allows it to work as intended.
    Thad Ryker
    I traded my sanity for a railgun :mrgreen:
  • kytohkytoh Member Posts: 11
    Thank you for the explanations.
    It all makes sense to me now. :D
Sign In or Register to comment.