Lookup - Best Way and The Difference

AngeloAngelo Member Posts: 180
I tried to created 4 methods to lookup table by using C/AL in the Control. All are have the same result (lookup the table, get the value, and put it to field or variable). What is the best way? are they any difference among this 4 methods?
"Test Lookup" is new field in Customer table.

In the form, pull "Test Lookup" and overwrite standard lookup :

"Test Lookup" - Onlookup (VAR Text : Text[1024];) : Boolean

//Method 1
CLEAR(CustForm);
CustTable.SETRANGE("No.",'10000','50000');
CustForm.SETTABLEVIEW(CustTable);
CustForm.SETRECORD(CustTable);
CustForm.LOOKUPMODE(TRUE);
IF CustForm.RUNMODAL = ACTION::LookupOK THEN BEGIN
CustForm.GETRECORD(CustTable);
"Test Lookup" := CustTable."No.";
END;

//Method 2
CLEAR(CustForm);
CustTable.SETRANGE("No.",'10000','50000');
CustForm.SETTABLEVIEW(CustTable);
//CustForm.SETRECORD(CustTable); //REMOVE SETRECORD
CustForm.LOOKUPMODE(TRUE);
IF CustForm.RUNMODAL = ACTION::LookupOK THEN BEGIN
CustForm.GETRECORD(CustTable);
"Test Lookup" := CustTable."No.";
END;

//Method 3
CustTable.SETRANGE("No.",'10000','50000');
IF FORM.RUNMODAL(0,CustTable) = ACTION::LookupOK THEN
"Test Lookup" := CustTable."No.";

//Method 4
CustTable.SETRANGE("No.",'10000','50000');
IF FORM.RUNMODAL(0,CustTable) = ACTION::LookupOK THEN BEGIN
Text := CustTable."No.";
EXIT(TRUE);
END;

Comments

  • tinoruijstinoruijs Member Posts: 1,226
    I use method 2 because on a custom lookup I usually want to decide which form is used.
    And SETRECORD is not necessary.

    Tino Ruijs
    Microsoft Dynamics NAV specialist
  • vaprogvaprog Member Posts: 1,140
    I recommend to use the following
    //Method 5
    CustTable."No." := Text;
    CustTable.SETRANGE("No.",'10000','50000');
    IF FORM.RUNMODAL(0,CustTable) = ACTION::LookupOK THEN BEGIN
      Text := CustTable."No.";
      EXIT(TRUE);
    END;
    
    If you need a lookup form other than the default one you can either just replace the 0 with FORM::"My Lookup Form" or even declare a Form type variable and set it up as needed. Use SETTABLEVIEW as well as SETRECORD in that case.

    Setting the primary key of the lookup table (using an assignment as above or using SETRECORD) sets focus to a record close to the one specified already (using FIND('=<>')). This enables you to start typing the code, then lookup, and chances are, you can just press enter to complete the code. Using SETRECORD without setting up the record first only overrides the SourceTablePlacement / SourceTableRecord properties.

    Returning the value through Text rather then assigning directly to "Test Lookup" enables you to see the value you selected before confirming the field by leaving it and also to cancel the field entry after lookup before confirming the field.
  • AngeloAngelo Member Posts: 180
    Hi Vaprog,

    "Setting the primary key of the lookup table (using an assignment as above or using SETRECORD) sets focus to a record close to the one specified already (using FIND('=<>')). This enables you to start typing the code, then lookup, and chances are, you can just press enter to complete the code. Using SETRECORD without setting up the record first only overrides the SourceTablePlacement / SourceTableRecord properties."

    --> When I test your above statement, I can't prove it. I tried to type customer code 30 (my intention to get customer code 30000), then I lookup or press enter, it will now return "30000".


    "Returning the value through Text rather then assigning directly to "Test Lookup" enables you to see the value you selected before confirming the field by leaving it and also to cancel the field entry after lookup before confirming the field."

    --> I'm a bit confused on this. when I test to put message("Test Lookup") onValidate, it will show "Test Lookup" value even though I assign to "Text" when lookup using your method.
  • vaprogvaprog Member Posts: 1,140
    Hi Angelo
    Angelo wrote:
    --> When I test your above statement, I can't prove it. I tried to type customer code 30 (my intention to get customer code 30000), then I lookup or press enter, it will now return "30000".
    Yes, I was wrong. It's actually the same record the system locates when you use Find (Ctrl+F) with option Beginning of Field selected and enter the contents of the control there.
    Angelo wrote:
    --> I'm a bit confused on this. when I test to put message("Test Lookup") onValidate, it will show "Test Lookup" value even though I assign to "Text" when lookup using your method.
    Text is the string shown in the control (A binary Integer or decimal cannot be shown, after all). It is, what get's formatted in the OnFormat trigger and what you get for inspection or manipulation in the OnAfterInput trigger. When you confirm the control by leaving it after you started editing it, the system basically executes EVALUATE(SourceExpression,Text) to get the value you entered back into your source variable or field in it's binary form. If you don't modify Text in the OnLookup trigger, no EVALUATE is executed.
Sign In or Register to comment.