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;
0
Comments
And SETRECORD is not necessary.
Tino Ruijs
Microsoft Dynamics NAV specialist
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.
"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.
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.