FieldRef and a TESTFIELD?

jversusjjversusj Member Posts: 489
Hi,
i did search the forum regarding FieldRef as well as read the online help, but i still don't quite understand if what I want to do is possible.

I am working on an integration project. Part of the integration is data requirements on the sales order prior to interfacing them to our warehouse management system. We have a routine that we use prior to order release that checks for required fields using TESTFIELD, etc.

now, i have to potentially send 3 addresses down to the WMS. different countries require different data (is zip code required, is city, etc.). I have written repetitive code that basically says (pseudo code follows).
IF condition then begin
TESTFIELD("sell-to City");
TESTFIELD("sell-to country Code");
..
..
more logic
..
..
IF Condition2 Then begin
TESTFIELD("ship-to city")
TESTFIELD("ship-to country code");
.. more processing..

as you can imagine, i have a lot of very similar code. I check city, state, post code, and country code for sell-to address, ship-to address, and a third-party billing address. it is a lot of code doing the same thing.

i would like to create a function to handle this, but i can't get my head around how to handle this. i wondered if i could create a function that says something like
//pseudocode
If condtion 1 Then begin
   TESTFIELD(fieldRef)
   TESTFIELD(fieldRef2)
...
IF condition 2 then begin
   TESTFIELD(fieldRef)
   TESTFIELD(fieldRef3)
...

is this a valid use of fieldref?

here is an actual code snippet showing just one instance of this check. i know it isn't pretty, hence why i'm trying to make it better.
  IF (recCountry."Suppress State in Rating") AND NOT (recCountry."Suppress Postal Code in Rating") THEN BEGIN
     TESTFIELD("Ship-to City");
     TESTFIELD("Ship-to Post Code");
     TESTFIELD("Ship-to Country Code");
  END;
  IF NOT (recCountry."Suppress State in Rating") AND (recCountry."Suppress Postal Code in Rating") THEN BEGIN
     TESTFIELD("Ship-to City");
     TESTFIELD("Ship-to County");
     TESTFIELD("Ship-to Country Code");
  END;
  IF (recCountry."Suppress State in Rating") AND (recCountry."Suppress Postal Code in Rating") THEN BEGIN
     TESTFIELD("Ship-to City");
     TESTFIELD("Ship-to Country Code");
  END;
  IF NOT (recCountry."Suppress State in Rating") AND NOT (recCountry."Suppress Postal Code in Rating") THEN BEGIN
     TESTFIELD("Ship-to City");
     TESTFIELD("Ship-to Post Code");
     TESTFIELD("Ship-to County");
     TESTFIELD("Ship-to Country Code");
  END;
kind of fell into this...

Comments

  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Hi,

    First: something about FieldRef and TESTFIELD from NAV help:

    TESTFIELD (FieldRef)
    Use this function to see if the contents of a field match a given value. If the contents differ from the given value, the system displays an error message.
    FieldRef.TESTFIELD([Value])


    I would create a function which could be called with list of fields formatted as as a filter, and then do all TESTFIELDs in that function:
    PROCEDURE TestFields@1000000000(VAR RecToTest@1000000000 : RecordRef;FieldList@1000000001 : Text[1024]);
        VAR
          FieldEnumerator@1000000002 : Record 2000000026;
          fRef@1000000003 : FieldRef;
        BEGIN
          IF FieldList = '' THEN
            EXIT; // OR ERROR - List cannnot be empty
    
          FieldEnumerator.SETFILTER(Number,FieldList);
          IF FieldEnumerator.FINDSET THEN
            REPEAT
              fRef := RecToTest.FIELD(FieldEnumerator.Number);
              fRef.TESTFIELD; // test if field not empty
            UNTIL FieldEnumerator.NEXT = 0;
    END;
    

    Having this TestFields function you can then make your tests more generic, for example:
    SalesHeader.GET(SalesHeader.type::invoice, SalesInvoiceNo);
    RecRef.gettable(SalesHeader);
    IF SomeCondition THEN
      TestFields(RecRef, '17|91|93')
    ELSE
      TestFields(RecRef, '17|91')
    

    The above tests if "Ship-to City" (field 17), "Ship-to Post Code" (field 91) and "Ship-to Country" (field 93) are not empty in tested Sales Header record if SomeCondition is true, otherwise it tests only "Ship-to City" and "Ship-to Post Code"

    Just an example how to approach the subject. Is that what you're trying to do ?

    Regards
    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • jversusjjversusj Member Posts: 489
    yes, thank you again!
    kind of fell into this...
  • jversusjjversusj Member Posts: 489
    also, my apologies for not checking that there were two entries in the online help.

    TESTFIELD(FieldRef) and TESTFIELD (Record).

    i am familiar with the latter and overlooked that there was another entry.
    :oops:
    kind of fell into this...
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Your welcome :)

    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
Sign In or Register to comment.