Setting a default list value in a table

CannikinCannikin Member Posts: 72
We want to set our "Shipping Method Code" to a default value each time a customer is created. Shipping Method Code pulls from another table so it appears as a dropdown/drilldown on the Customer card.

Is there a way to set one of the values in there as the default? Can I just set the "InitValue" property to whatever I want it to be and it'll match up automatically?

Thanks for any help! :)

Comments

  • SbhatSbhat Member Posts: 301
    You can put a code onNewRecord of the form where you say

    "Shipping method code" := 'ABC'

    also you should put this code at the place where the default value for Shipping method code is being pulled by Default values.

    SB.
  • DenSterDenSter Member Posts: 8,307
    No I would never write data manipulation code like that on the form. The best solution would be to add a "Default Shipping Method" field in the Sales and Receivables setup table (don't forget to add it to the form as well), with a table relation into the shipping method code table.

    Then, in the OnInsert trigger of the Customer table, you add something like this:
    IF "Shipping Method Code" = '' THEN BEGIN
      SalesSetup.GET;
      VALIDATE("Shipping Method Code",SalesSetup."Default Shipping Method");
    END;
    

    That way, your users can enter the default code to use in a setup table, and they would not have to get you involved to update the C/AL code every time they feel like changing the default value. The validation of the field value would be taken care of the DBMS because of the table relation of the field.

    It is generally not a good idea to hard code any values, either by setting an initvalue in a property or by writing lines of code. If you make it a habit of programming forms to manipulate data, you will at some point spend most of yuor time searching where the hell this code is that keeps messing up your data. Form events should only be used to program the appearance and behaviour of the form.
  • SavatageSavatage Member Posts: 7,142
    Cannikin wrote:
    Can I just set the "InitValue" property to whatever I want it to be and it'll match up automatically?

    Thanks for any help! :)

    you can do this - we have our InitValue set as "UPS" for shipping agent code
  • CannikinCannikin Member Posts: 72
    Thanks guys! I just set it as the "InitValue" in the table.
  • kinekine Member Posts: 12,562
    Not good solution... :-( I saw many of that hardcoded codes, and it is a pain to correct it (or make possible to use in another country or changing the codes etc.)...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • DenSterDenSter Member Posts: 8,307
    As long as the code value UPS is an existing value in the table, that is a perfectly acceptable solution, and with minimal development, so cheap for the customer.
  • kinekine Member Posts: 12,562
    DenSter wrote:
    As long as the code value UPS is an existing value in the table, that is a perfectly acceptable solution, and with minimal development, so cheap for the customer.

    Yes, as you wrote: As long as the code value UPS is an existing value... but after that, it is problem... but it depend what you want to do with your application in the future. If you want to use it in another country or something... :-) Cheap now, costly after...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • KowaKowa Member Posts: 925
    I agree with kine. Changing init values can be a double-edged sword.
    Sometime it will work out ( it probably will in this case) , but if you, for instance, change the init value of "Type" in Sales Line from Type::" " to Type::Item, you may encounter all sorts of problems with a coded SalesLine.INIT and subsequent processing, because the whole code was built on the base of Type::" ". Creating additional default values in the setup tables also has the advantage of informing other developers at a glance that something has been changed.
    Kai Kowalewski
  • jreynoldsjreynolds Member Posts: 175
    Also keep in mind that the OnValidate trigger for the field does not fire when the InitValue is set. Not a problem for now with Shipment Method Code on the Customer table since there is no code in the OnValidate trigger of this field, but perhaps in a future relase there may be.
  • SavatageSavatage Member Posts: 7,142
    wow! this turned into quite a discussion.

    We use the InitValue in a few fields for a NEW customer that will ALWAYS be the same for our company (many other companies may differ)

    Posting Groups, Costing Method, Shipping Agent, etc

    It was necessary due to the fact that sometimes the person entering the new customer would occasionally leave them blank and cause all kinds of problems with E-ship & fast packing. So it was better to have them automatically fill in. Fields that can vary use the mandatory field solution discussed several times in this forum.
  • nelsonnelson Member Posts: 107
    Even for fields where the values will always be the same, it is definitely better to take these default values from a Setup table.
    Navision HQ themselves did it with Customer Templates, for example.
    They can't be wrong, can they?... :D
    Nelson Alberto
Sign In or Register to comment.