How to evaluate a value into a FieldRef?

Timo_LässerTimo_Lässer Member Posts: 481
edited 2006-07-12 in Navision Attain
I tried to evaluate a field from a record into a FieldRef.
I use 3.70 (HF12) client and get following error message:
A variable was expected. For example:

MyVar
Customer.Name
The cursor is set to
EVALUATE(|Fldref.Value,Record.Field);

Can anybody tell me how to evaluate a value into a FieldRef?
Timo Lässer
Microsoft Dynamics NAV Developer since 1997
MSDynamics.de - German Microsoft Dynamics Community - member of [clip]

Comments

  • PrebenRasmussenPrebenRasmussen Member Posts: 137
    This is how I chose to do it:
    CASE FORMAT(FR.TYPE) OF
      'Text':
        BEGIN
          EVALUATE(txtDummy,"Some Text");
          IF "booUseValidate" THEN
            FR.VALIDATE(locText)
          ELSE
            FR.VALUE(locText);
        END;
    
  • Timo_LässerTimo_Lässer Member Posts: 481
    That's seems to be the only way to do it :cry:
    Timo Lässer
    Microsoft Dynamics NAV Developer since 1997
    MSDynamics.de - German Microsoft Dynamics Community - member of [clip]
  • eromeineromein Member Posts: 589
    Why so sad?
    "Real programmers don't comment their code.
    If it was hard to write, it should be hard to understand."
  • Timo_LässerTimo_Lässer Member Posts: 481
    I have written a Codeunit which evaluates a Text into a FieldRef.
    In this Codeunit I have listet all possible data types (except for BLOB because this is not availible for a variable) in a CASE structure.

    This would be needless if the EVALUATE could handle FieldRef.
    Timo Lässer
    Microsoft Dynamics NAV Developer since 1997
    MSDynamics.de - German Microsoft Dynamics Community - member of [clip]
  • eromeineromein Member Posts: 589
    True, but not possible.

    Some information isn't know on the recref level. So you will always need to get the fldref first.
    "Real programmers don't comment their code.
    If it was hard to write, it should be hard to understand."
  • Timo_LässerTimo_Lässer Member Posts: 481
    eromein wrote:
    Some information isn't know on the recref level. So you will always need to get the fldref first.
    I didn't talk about RecRef. I know that it cannot be possible to evaluate into a RecordRef.

    I wanted to evaluate into a FieldRef:
    EVALUATE(Fldref.Value,Record.Field);
    The EVALUATE function can handle all data types, so I asked myself why it cannot handle a FieldRef.

    So I wrote this function(s):
    Global Text000 = '%1' is not a valid %2 value.
    
    
    EvaluateFieldRef(VAR ToFieldRef : FieldRef;FromText : Text[250];HideDialog : Boolean) : Boolean
    //
    // Evaluates a text into a FieldRef variable
    //
    
    CASE UPPERCASE(FORMAT(ToFieldRef.TYPE)) OF
      'INTEGER':
        IF EVALUATE(DummyInteger,FromText) THEN BEGIN
          ToFieldRef.VALUE := DummyInteger;
          EXIT(TRUE);
        END;
      'TEXT','TABLEFILTER':
        BEGIN
          ToFieldRef.VALUE := COPYSTR(FromText,1,ToFieldRef.LENGTH);
          EXIT(TRUE);
        END;
      'CODE':
        BEGIN
          ToFieldRef.VALUE := UPPERCASE(COPYSTR(FromText,1,ToFieldRef.LENGTH));
          EXIT(TRUE);
        END;
      'DECIMAL':
        IF EVALUATE(DummyDecimal,FromText) THEN BEGIN
          ToFieldRef.VALUE := DummyDecimal;
          EXIT(TRUE);
        END;
      'OPTION':
        IF STRPOS(UPPERCASE(FldRef.OPTIONCAPTION),UPPERCASE(FromText)) > 0 THEN BEGIN
          IF FldRef.OPTIONCAPTION[1] = ',' THEN
            I := 2
          ELSE
            I := 1;
          WHILE STRPOS(UPPERCASE(SELECTSTR(I,FldRef.OPTIONCAPTION)),UPPERCASE(FromText)) = 0 DO
            I += 1;
          ToFieldRef.VALUE := SELECTSTR(I,FldRef.OPTIONCAPTION);
          EXIT(TRUE);
        END;
      'BOOLEAN':
        IF EVALUATE(DummyBoolean,FromText) THEN BEGIN
          ToFieldRef.VALUE := DummyBoolean;
          EXIT(TRUE);
        END;
      'DATE':
        BEGIN
          AppMgt.MakeDateText(FromText);
          IF EVALUATE(DummyDate,FromText) THEN BEGIN
            ToFieldRef.VALUE := DummyDate;
            EXIT(TRUE);
          END;
        END;
      'TIME':
        IF EVALUATE(DummyTime,FromText) THEN BEGIN
          ToFieldRef.VALUE := DummyTime;
          EXIT(TRUE);
        END;
      'DATETIME':
        IF EVALUATE(DummyDateTime,FromText) THEN BEGIN
          ToFieldRef.VALUE := DummyDateTime;
          EXIT(TRUE);
        END;
      'BINARY':
        IF EVALUATE(DummyBinary,FromText) THEN BEGIN
          ToFieldRef.VALUE := DummyBinary;
          EXIT(TRUE);
        END;
    //  'BLOB':;  // Not allowed
      'DATEFORMULA':
        IF EVALUATE(DummyDateFormula,FromText) THEN BEGIN
          ToFieldRef.VALUE := DummyDateFormula;
          EXIT(TRUE);
        END;
      'BIGINTEGER':
        IF EVALUATE(DummyBigInteger,FromText) THEN BEGIN
          ToFieldRef.VALUE := DummyBigInteger;
          EXIT(TRUE);
        END;
      'DURATION':
        IF EVALUATE(DummyDuration,FromText) THEN BEGIN
          ToFieldRef.VALUE := DummyDuration;
          EXIT(TRUE);
        END;
      'GUID':
        IF EVALUATE(DummyGUID,FromText) THEN BEGIN
          ToFieldRef.VALUE := DummyGUID;
          EXIT(TRUE);
        END;
      'RECORDID':
        IF EVALUATE(DummyRecordID,FromText) THEN BEGIN
          ToFieldRef.VALUE := DummyRecordID;
          EXIT(TRUE);
        END;
    END;
    
    IF GUIALLOWED AND NOT HideDialog THEN
      ERROR(Text000,FromText,FORMAT(ToFieldRef.TYPE))
    ELSE BEGIN
      LastErrorText := STRSUBSTNO(Text000,FromText,FORMAT(ToFieldRef.TYPE));
      EXIT(FALSE);
    END;
    
    
    GetLastErrorText() : Text[1024]
    EXIT(LastErrorText);
    
    So you can evaluate any text into any FieldRef
    IF NOT EvaluateFieldRef(MyFieldRef,MyText,TRUE) THEN
      DoSomethingWith(GetLastErrorText);
    
    Timo Lässer
    Microsoft Dynamics NAV Developer since 1997
    MSDynamics.de - German Microsoft Dynamics Community - member of [clip]
  • eromeineromein Member Posts: 589
    I made this a long time ago. I know it is not completly correct, but it was enough for what I wanted to do.

    This code takes a value and sets it to a specific format which is defined by the user and is stored in the field "FormatString".

          FldRef := RecRef.FIELD(Veldnummer);
          IF FORMAT(FldRef.CLASS) = 'FlowField' THEN
            FldRef.CALCFIELD;
    
          IF FormatString='' THEN
            EXIT(FORMAT(FldRef.VALUE))
          ELSE BEGIN
    ->      IF EVALUATE(DecimalValue, FORMAT(FldRef.VALUE) ) THEN BEGIN
              EXIT(FORMAT(DecimalValue,0,FormatString))
            END ELSE
              EXIT(FORMAT(FldRef.VALUE))
          END;
    

    the line marked with the -> could be multiplied by the number of types you want to be able to handle and you could place it in a case.

    I think it's about the same you have.

    Could you maybe tell us what you're making here???
    "Real programmers don't comment their code.
    If it was hard to write, it should be hard to understand."
  • Timo_LässerTimo_Lässer Member Posts: 481
    Thank you for the hint. I noticed that decimals will always be displayed without decimals if they are <> 0.
    eromein wrote:
    Could you maybe tell us what you're making here???
    I try to explain :wink:
    I have a table for importing data which is a 100% Copy of an existing table (incl. Trigger code). I've named it "<Table Name> Import Worksheet".
    The User can decide which fields from the Import Worksheet should be transferred in the "real" table and what should be happen with the field values.
    (Define an "Init Value" if the Import doesn't fill the field or define a "Default Value" which will overwrite the imported value.)
    Furthermore the user can decide the condition when the field should be transferred (Always, If new value is <> empty, If old value is empty).
    All these Setups will be made in an "Import Template" on field level.

    And exactly for the "Init Value" resp. "Default Value" I need to evaluate a text into a FieldRef.
    Timo Lässer
    Microsoft Dynamics NAV Developer since 1997
    MSDynamics.de - German Microsoft Dynamics Community - member of [clip]
  • eromeineromein Member Posts: 589
    Nice!

    Is this hobby or work?
    "Real programmers don't comment their code.
    If it was hard to write, it should be hard to understand."
  • Timo_LässerTimo_Lässer Member Posts: 481
    eromein wrote:
    Nice!

    Is this hobby or work?
    No time for hobby :wink:
    It's part of an project.
    Timo Lässer
    Microsoft Dynamics NAV Developer since 1997
    MSDynamics.de - German Microsoft Dynamics Community - member of [clip]
  • eromeineromein Member Posts: 589
    To bad, would have been a great download!
    "Real programmers don't comment their code.
    If it was hard to write, it should be hard to understand."
  • dennis_decoenedennis_decoene Member Posts: 13

    EvaluateFieldRef(VAR ToFieldRef : FieldRef;FromText : Text[250];HideDialog : Boolean) : Boolean

    I guess this is for 3.70 and will not work in 3.60, since the fieldref.LENGTH is not available... For the rest it would work...
    I don't need PGP, my handwriting suffices.
  • henrikohmhenrikohm Member Posts: 30

    EvaluateFieldRef(VAR ToFieldRef : FieldRef;FromText : Text[250];HideDialog : Boolean) : Boolean

    I guess this is for 3.70 and will not work in 3.60, since the fieldref.LENGTH is not available... For the rest it would work...

    Well, use the dynamic table "Field" and find the field length.

    Best regards
    Best regards
    Henrik Ohm
  • Dmitry_ChadaevDmitry_Chadaev Member Posts: 52
    //  'BLOB':;  // Not allowed
    

    Hello guys,

    Anybody knows a workaround for this one? How come is it impossible to insert something into a BLOB field of RecRef?

    There are also complications with reading BLOBs from RecRefs - but I can come up with a workaround at least... In case of writing into a BLOB - no idea at all :(

    When I try to do something like this:
    FldRef.VALUE := MyRecord.MyBlobField;
    

    I receive an odd message:
    The expression BLOB cannot be type-converted into a BLOB value.
    

    :-k
    Best regards,
    Dmitry
  • SaroSaro Member Posts: 58
    i think using EVALUATE(FORMAT(fieldref.value), REcord.field) would work did you try that?
    Saro
  • primeapprimeap Member Posts: 37
    FldRef should be ToFieldRef?

    STRPOS(UPPERCASE(FldRef.OPTIONCAPTION),UPPERCASE(FromText)) > ..
    STRPOS(UPPERCASE(SELECTSTR(I,FldRef.OPTIONCAPTION)),UPPERCASE(F..
    ToFieldRef.VALUE := SELECTSTR(I,FldRef.OPTIONCAPTION);

  • PConijnPConijn Member Posts: 31
    I don't know if this works in every version of NAV, but recent versions, it works if you evaluate to the FieldRef instead of the FieldRef.VALUE.

    Ex.:
    if RecRef.FieldExists(ToFieldNo) then
      FldRef := RecRef.Field(ToFieldNo);
    if Evaluate(FldRef, MyNewValueAsText) then
      RecRef.Modify();
    

    I have tested this for decimal and text fields and both work just fine.
    Kind Regards,

    Peter Conijn

    -The Learning Network-

Sign In or Register to comment.