Change Log for records modified with a codeunit

philippegirodphilippegirod Member Posts: 191
edited 2015-01-14 in NAV Tips & Tricks
1/ I made a codeunit to modify a record in a table with the function Record.MODIFY(TRUE);

2/ I added a setup in Chang Log setup to track al the modification in this table

3/ When I run the codeunit, nothing is added to the change log table, but when i change manually the record from the table, a change log is added.

Any idea ?

Thanks a lot.
P.S. Navision version 4.00 SP3
My candle burns by both ends, it will not last the night,
But oh my foes and oh my friends, it gives a lovely light

Comments

  • garakgarak Member Posts: 3,263
    edited 2008-05-16
    change log only log manual changes.
    If you need to update the change log also when the record is changed by code you must create your own function based on the changelog management codeunit (see CU 1) and CU for Changelog

    REgards
    Do you make it right, it works too!
  • kinekine Member Posts: 12,562
    You need to create 2 record ref variables. One for xRec and second for Rec and pass them into this code:
      ChangLogMgt.LogModification(RecRef,xRecRef);
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • garakgarak Member Posts: 3,263
    right kine ;-)
    Do you make it right, it works too!
  • philippegirodphilippegirod Member Posts: 191
    OK, now with 4 lines added in the codeunit the changes are tracked (using Change Log Management codeunit).

    Just to inform the code looks like that:
    //before the change:
    xRecRef.GETTABLE(MyRecord);
    
    //Change it Self
    MyRecord.MyField := NewValue;
    
    //Saving of the change   
    MyRecord.MODIFY(TRUE);
    
    //Change Log Management   
    RecRef.GETTABLE(MyRecord);
    ChangLogMgt.LogModification(RecRef,xRecRef);
    
    My candle burns by both ends, it will not last the night,
    But oh my foes and oh my friends, it gives a lovely light
  • David_SingletonDavid_Singleton Member Posts: 5,479
    Oh and don't forget to buy a new server, and lots and lots of new hard disks. :mrgreen:
    David Singleton
  • garakgarak Member Posts: 3,263
    @philippegirod: But consider, that every modify on this table will be stored. so if there are many changes, theses changes will also loged. :wink:
    Do you make it right, it works too!
  • krikikriki Member, Moderator Posts: 9,094
    [Topic moved from Navision forum to Navision Tips & Tricks forum]
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • David_SingletonDavid_Singleton Member Posts: 5,479
    kriki wrote:
    [Topic moved from Navision forum to Navision Tips & Tricks forum]


    Nooooooooooooooooooooo


    Now people are going to think that this is a good idea. ](*,)
    David Singleton
  • ara3nara3n Member Posts: 9,255
    I had been asked to track who opens and releases orders. ( meaning track the status field on sales Order).
    The only way to do it is through code. And I added similar code to the release document CU.


    The makes sense in several places in Navision. Maybe it should be moved to how-to, but I don't think it's bad in every scenario.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • David_SingletonDavid_Singleton Member Posts: 5,479
    ara3n wrote:
    I had been asked to track who opens and releases orders. ( meaning track the status field on sales Order).
    The only way to do it is through code. And I added similar code to the release document CU.


    The makes sense in several places in Navision. Maybe it should be moved to how-to, but I don't think it's bad in every scenario.

    You know that, and I know that, and I am sure many other people know that. But I am sure that some people will just see this as a solution, and add it to many code units.

    What you are doing (order status) makes sense, since this is something that is directly triggered by a user. (In fact I solve that request normally by allowing the user to manually edit the field, and putting the call to the CU in the validate, but that's off topic).



    All I want to say is for developers to consider very carefully before using this Tip :mrgreen:
    David Singleton
  • krikikriki Member, Moderator Posts: 9,094
    kriki wrote:
    [Topic moved from Navision forum to Navision Tips & Tricks forum]


    Nooooooooooooooooooooo


    Now people are going to think that this is a good idea. ](*,)
    Well, it is a good tip&trick.

    Is it a good idea? Depends on a lot of things:
    -Do you really need it?
    -Is it useful?
    -How do you want to use it? ...

    But like you wrote : Consider before using it and if you decide to use it : consider HOW to use it.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • PConijnPConijn Member Posts: 31
    I agree that it's a good trick. For instance, I would like to create a push action on a web service every time a certain tables have a modify or insert action. Instead of having to edit every table's insert & modify trigger, this might be a more efficient solution.

    Thoughts?
    Kind Regards,

    Peter Conijn

    -The Learning Network-

  • krikikriki Member, Moderator Posts: 9,094
    With NAV2009R2 there are global-triggers in codeunit 1. These triggers are also triggered by programmatical insert/modify/delete/rename.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • n.topalovn.topalov Member Posts: 26
    If you
    kriki wrote:
    With NAV2009R2 there are global-triggers in codeunit 1. These triggers are also triggered by programmatical insert/modify/delete/rename.

    Hello,
    If you are talking about the new OnDatabaseInsert and the other OnDatabse... triggers, I wasn't able able to use them. I tried several tests in order to see if they are fired on every insert(true) in the code but didn't succeeded.

    I tried for example:

    NewItem."No." := 'TESTNO';
    NewItem.insert(true);

    and OnDatabaseInsert trigger in cu1 i put code to insert some dummy record in a different table but nothing happened. I also put the code ChangLogMgt.LogInsertion(RecRef); which is the same code like the OnGlobalInsert but no Change log entry was registered.

    Am I missing something or it's like that ?

    10x
  • krikikriki Member, Moderator Posts: 9,094
    Sorry it took so long, but I had been in vacation.

    Make sure you use the correct function-ID's in codeunit 1 for the triggers, otherwise they are not triggered.
    The first time, an Insert/Modify/Delete/Rename is done on a table, NAV launches function "GetDatabaseTableTriggerSetup" in Codeunit 1 that returns for which actions the function must be triggered.

    I also did a test with the rename of a card table. And for each record in other tables were the value of that record was changed, the OnModify-trigger was launched!

    PS: after you changed something in the code (or setup that is used by that code), you need to re-open the company to reset the objects and setup that was already read.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • tinoruijstinoruijs Member Posts: 1,226
    Just noticed in NAV 2013 R2 this is enough for logging modifications.
      RecordRef.GETTABLE(MyRecord);
      ChangeLogManagement.LogModification(RecordRef);
    

    The function LogModification is changed, so this is the only way.

    Tino Ruijs
    Microsoft Dynamics NAV specialist
Sign In or Register to comment.