How to transfer User options from codeunit to the global function of a table via c/al code

Hi all,
I have a table called custom table with 7 fields:
- Custom Code datatype: Code;
- Custom Picture datatype: BLOB;
- Expiry Date of type: Date;
- Toxity of type option the options are: , Caution, Danger, and Hazardous;
- Custom Number of datatype: integer;
- Custom Description of type: text and
- Commission of type decimal.
I need to write a codeunit that will be called in the C/AL of the custom table that will transfer user options to the C/AL code.
From the option list the code works perfectly on the onValidate() function:

Comments

  • AntonyNgorora7AntonyNgorora7 Member Posts: 18
    This code works on the OnValidate() of the Toxity field
    IF "Custom Toxity" = 1  THEN
      "Custom Description" := 'Caution';
    IF "Custom Toxity" = 2 THEN
      "Custom Number" := 6;
    IF "Custom Toxity" = 3 THEN
      Commission := 99.0;
    

    but when implemented on the codeunit it does not work.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    edited 2018-09-25
    I need to write a codeunit that will be called in the C/AL of the custom table that will transfer user options to the C/AL code.
    From the option list the code works perfectly on the onValidate() function:
    Could you you please be more specific about your requirements ? What is your goal, what do you want to achieve, in business terms? (Writing a codeunit is not a goal, transferring some data from one place to another might be)
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • AntonyNgorora7AntonyNgorora7 Member Posts: 18
    Hi Slawek_Guzek, my goal is to transfer data from the user option field "Toxity" and change the values of the other fields in the table via the Codeunit global function. I want to call the Cdeunit function in the Toxity - OnValidate(), so that it runs as soon as the user makes an option from the option list.

    For example, the codeunit should:
    1. When the user chooses the option 'Caution' on the option list the text Caution should be added to the Custom Description field.
    2. When the user chooses the option 'Danger' on the option list the Custom Number should be changed to 6.
    3. When the user chooses the option 'Hazardous' on the option list the commission field should be changed to 99.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Still not sure if I understand correctly.

    Anyway - the codeunit can have a table as a parameter. You can pass your rec (your custom table) to the codeunit call in Toxity - OnValidate, like this
    Toxity - OnValidate
    CODEUNIT.RUN(CodeunitID,Rec)
    
    Then inside the codeunit's OnRun trigger you can have your code changing values in your custom table (the one passed in CODEUNIT.RUN call:
    OnRun(Rec : your custom table)
    WITH Rec DO BEGIN
      IF "Custom Toxity" = 1  THEN
        "Custom Description" := 'Caution';
      IF "Custom Toxity" = 2 THEN
        "Custom Number" := 6;
     IF "Custom Toxity" = 3 THEN
        Commission := 99.0;
    END;
    

    I hope this answers your question. Not sure why do you need to have this in a separate codeunit, and why having the code in the Toxity - OnValidate trigger is not good enough. The only purpose I can think of is add flexilbility and enable different classification by specifying different codeunit ID to be run from Toxity - OnValidate in some setup, and that those codeunits will be added to your solution at later stages.
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • AntonyNgorora7AntonyNgorora7 Member Posts: 18
    Thank you very much @Slawek_Guzek It definitely did help, and answered my question.
    I had initially put the validation rules in the Toxity - OnValidate trigger and it worked very well.
    But I was informed that they prefered for the OnValidate trigger to have less code and to populate the code and validation rules on a Codeunit instead.
    I am really grateful.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Unless the ID of the codeunit in the CODEUNIT.RUN(CodeunitID,Rec) line is supposed to be configurable somehow (you specify the id of the codeunit to be run in a variable which is populated eariler in the code from some sort of setup) the Toxity - OnValidate trigger is the best place to put this sort of validation code and it makes a very little sense to move the code to a separate object outside the Toxity - OnValidate trigger

    My 0.01£



    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • AntonyNgorora7AntonyNgorora7 Member Posts: 18
    That is very true, I agree, and I have taken note of this. Thank you very much for the valuable information.
  • dansdans Member Posts: 148
    Why not add a function in codeunit and call that function from onvalidate ? You can pass any variables that you want .
    Instead of CODEUNIT.RUN, just call that NewCodeUnit.DoThisFunction.

    Putting it in a separate codeunit, makes it easier to maintain and update in the future.
    Microsoft Certified IT Professional for Microsoft Dynamics NAV

    Just a happy frood who knows where his towel is
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    dans wrote: »
    Putting it in a separate codeunit, makes it easier to maintain and update in the future.
    Interesting.

    Why spreading given functionality between two objects, rather than having it all in one place, would make it easier to maintain and update in the future?
    How about user access management, does managing user permission for two separate objects also make it easier than having to manage the permissions for the table alone?
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • dansdans Member Posts: 148
    Having two instead of one objects would not make a big different managing the permissions. Normally once we setup the permission properly, we hardly change it in the future anyway.
    It also depends on the requirement and personal preferences. Sometimes it makes sense to put simple codes in one place. However, I found out that normally we can always reuse the functionality elsewhere, so it is easier to maintain it if we have a separate codeunit (instead of putting it on the OnValidate).
    Also when working with multiple developer, it is easier to spot and merge objects.
    Microsoft Certified IT Professional for Microsoft Dynamics NAV

    Just a happy frood who knows where his towel is
  • robbonickrobbonick Member Posts: 40
    I have to say I prefer to put a lot of functionality into codeunits. It just makes things much easier to deploy, for instance I know I can fix an urgent bug by deploying a codeunit in the middle of the day, with no service restarts at all.

    Where as if this was in a table, I would have to restart all the services, and users would get those error messages "The Definition of table blah blah has changed".
Sign In or Register to comment.