RunModal with temp table

grouicgrouic Member Posts: 34
edited 2015-12-14 in NAV Three Tier
Hello,

I'm trying this code on a Onlookup trigger :

Table R_TempDimensionValue is temporary.
The temp table is correctly populated and displayed but the value I choose is not copyied into the code field on the main page.
Code - OnLookup(VAR Text : Text) : Boolean
IF Type = Type::BU THEN BEGIN
  CLEAR(R_DimensionValue);
  R_TempDimensionValue.DELETEALL;
  R_DimensionValue.SETRANGE("Dimension Code",'BU');
  R_DimensionValue.SETRANGE(Blocked,FALSE);
  IF R_DimensionValue.FINDSET THEN REPEAT
    IF NOT GET(Type::BU,R_DimensionValue.Code) THEN BEGIN
      R_TempDimensionValue := R_DimensionValue;
      R_TempDimensionValue.INSERT;
    END;
  UNTIL R_DimensionValue.NEXT = 0;

  IF PAGE.RUNMODAL(0,R_TempDimensionValue) = ACTION::LookupOK THEN
    Code := R_TempDimensionValue.Code;
END;


NB : I tried not using a temp table by using MARKs instead on the R_DimensionValues (that is not temp). It doesn't work either.

Any suggestion ?

Thanks !

Answers

  • vaprogvaprog Member Posts: 1,140
    Hi grouic,

    you should assign R_TempDimensionValue.Code to Text (the trigger's VAR parameter), not to Code.

    And the flaw that broke your code: you need to return TRUE on successful lookup.


    And finally, please, when posting code in this forum, use [code]...[/code] tags, so indentation is preserved and your code is much more easily readable. (See the Paragraph symbol above the edit box.)
  • grouicgrouic Member Posts: 34
    Hello,

    Thx for your answer. I tried to assign the Text variable, but no change.

    I finally fixed the problem by creating a record variable instead using the default rec record. I replaced the

    IF NOT GET(Type::BU,R_DimensionValue.Code) THEN BEGIN ...
    
    by
    
    IF NOT R_myPageRec.GET(Type::BU,R_DimensionValue.Code) THEN BEGIN ...
    

    and everything is OK now.

  • vaprogvaprog Member Posts: 1,140
    edited 2015-12-17
    Hi grouic

    The code change, of which you claim fixed your issue has nothing at all to do with returning the value chosen in the lookup, but only with the records shown in that lookup page.

    You claimed, however:
    grouic wrote: »
    The temp table is correctly populated and displayed but the value I choose is not copyied into the code field on the main page.

    Even if this solved your issue, you still should implement the changes I gave you. But they must go together. The value of Text is only filled into the control if also you return TRUE.

    The difference is not widely understood, apparently not even by Microsoft, looking at most of their standard code:

    If the lookup is not coded, but solely results from the TableRelation trigger, and when you use the coded approach assigning to Text and returning TRUE, the value you select is not yet validated but simply filled into the control. The user has now a chance to press Esc and the original value reappears, just as if he did not lookup the value at all, but typed it. If he leaves the field any other way, normal validation occurs, just as if typed in.

    If you assign the value directly to SourceExpression, and do not return TRUE, the system is in an entirely different state. Pressing Esc now will (try to) close the page, writing the change to the database. Moreover, the no control or field validation is called. You do not have any chance of calling the OnValidate trigger of the page in this situation, but may explicitly call the fields OnValidate by using VALIDATE(SourceExpression) in your Lookup code. Take care, though: Microsoft says (in Best Practices) about field OnLookup, that, if the validation may cause an error, you should do the validation on a copy of Rec. I don't know if this does apply to control OnLookup triggers as well.

    Unfortunately, with field OnLookup triggers, you do not have any chance of doing the same as a TableRelation lookup does. The parameters needed for this are simply missing.
Sign In or Register to comment.