Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

OnBeforeModifyEvent - xRec

Posted on by 714

Hi Team,

    I would like to check with you regarding "OnBeforeModifyEvent - xRec". I can see the xRec.xxx (previous value) if we change the value from table or page. But xRec.xxx value is always same as current record modified value if we modifed by code. Does is standard behavior or bug?

NAV - 2016 CU14 (AU)

Regards,

Yukon

*This post is locked for comments

  • Tys Profile Picture
    Tys 5 on at
    RE: OnBeforeModifyEvent - xRec

    Right! Today it was my turn to spend a good hour, reading 14 websites, and then running a few code tests on this topic.

    Wondering whether we can clean up such threads with well-meant but non-working suggestions, untested code or what ever. In the end it is super simple. Microsoft confirms that it has been like this for many versions. And it still works exactly the same today in AL. So, if you want to use this in an OnBeforeModify event, be sure to call xRec.FIND('=') to obtain the current/previous/old (whatever way you look at it) value as stored in the database.

  • a.sh Profile Picture
    a.sh 5 on at
    RE: OnBeforeModifyEvent - xRec

    Hi Yukon,

    The post is not recent but I'd just like to mention that it's been like this for a long time: xRec is different from Rec inside the OnModify trigger and the relevant event triggers unless the record is modified by code. 

    Regards,

    Alex

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: OnBeforeModifyEvent - xRec

    And now try this:

    OBJECT Codeunit 50099 TEST

    {

     OBJECT-PROPERTIES

     {

       Date=16.06.17;

       Time=09:46:38;

       Modified=Yes;

       Version List=;

     }

     PROPERTIES

     {

       OnRun=VAR

               Item@1000000000 : Record 27;

             BEGIN

               Item.FINDFIRST;

               Item.VALIDATE(Description, 'foobar');

               Item.MODIFY(TRUE);

             END;

     }

     CODE

     {

       [EventSubscriber(Table,27,OnAfterModifyEvent)]

       LOCAL PROCEDURE OnAfterModifyEvent@1000000000(VAR Rec@1000000000 : Record 27;VAR xRec@1000000001 : Record 27;RunTrigger@1000000002 : Boolean);

       BEGIN

         MESSAGE(xRec.Description);

         MESSAGE(Rec.Description)

       END;

       BEGIN

       END.

     }

    }

  • Luc van Vugt Profile Picture
    Luc van Vugt on at
    RE: OnBeforeModifyEvent - xRec

    Hi guys, I have just checked it in NAV 2017 and works fine, so xRec is different from Rec. For both OnBeforeModifyEvent and OnAfterModifyEvent.

    I have tested with the following code:

    OBJECT Codeunit 88889 Update Item

    {

     OBJECT-PROPERTIES

     {

       Date=16-06-17;

       Time=09:16:42;

       Version List=OnBeforeModifyEvent - xRec;

     }

     PROPERTIES

     {

       OnRun=BEGIN

               UpdateDescriptionAssigment;

               UpdateDescriptionValidate;

             END;

     }

     CODE

     {

       VAR

         Item@1000 : Record 27;

       LOCAL PROCEDURE UpdateDescriptionAssigment@1();

       BEGIN

         WITH Item DO BEGIN

           GET('1000');

           Description := 'Bicycle UpdateDescriptionAssigment';

           VALIDATE(Description);

         END

       END;

       LOCAL PROCEDURE UpdateDescriptionValidate@2();

       BEGIN

         WITH Item DO BEGIN

           GET('1000');

           VALIDATE(Description,'Bicycle UpdateDescriptionValidate');

         END

       END;

       BEGIN

       END.

     }

    }

    OBJECT Codeunit 88888 Item - Events

    {

     OBJECT-PROPERTIES

     {

       Date=16-06-17;

       Time=08:42:12;

       Version List=OnBeforeModifyEvent - xRec;

     }

     PROPERTIES

     {

       OnRun=BEGIN

             END;

     }

     CODE

     {

       [EventSubscriber(Table,27,OnBeforeValidateEvent,Description)]

       LOCAL PROCEDURE BeforeOnValidateDescription@1(VAR Rec@1000 : Record 27;VAR xRec@1001 : Record 27;CurrFieldNo@1002 : Integer);

       BEGIN

         MESSAGE('BeforeOnValidateDescription')

       END;

       [EventSubscriber(Table,27,OnAfterValidateEvent,Description)]

       LOCAL PROCEDURE AfterOnValidateDescription@2(VAR Rec@1000 : Record 27;VAR xRec@1001 : Record 27;CurrFieldNo@1002 : Integer);

       BEGIN

         MESSAGE('AfterOnValidateDescription')

       END;

       BEGIN

       END.

     }

    }

  • Luc van Vugt Profile Picture
    Luc van Vugt on at
    RE: OnBeforeModifyEvent - xRec

    No, Nikolay, that's not strange as the record in the table has not been modified yet, so a GET on that record will give you the old (=current) version.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: OnBeforeModifyEvent - xRec

    It looks a little bit strange, but if you write in event xRec.GET before your code everything be as you expecting

  • Yukon Profile Picture
    Yukon 714 on at
    RE: OnBeforeModifyEvent - xRec

    Thanks, Luc. Please report it to MS. Nowadays, we are star using Event.

    Regards,

    Yukon

  • Luc van Vugt Profile Picture
    Luc van Vugt on at
    RE: OnBeforeModifyEvent - xRec

    Thanx, Dennis.

    Yukon, if you want I can report it to MS as MVP.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: OnBeforeModifyEvent - xRec

    It's the same on 2017.

    If I remember right, it's only if you modify by code.

  • Yukon Profile Picture
    Yukon 714 on at
    RE: OnBeforeModifyEvent - xRec

    Yes, I think same as you but haven't test on other CU and 2017. Let see other member reply.

    Regards,

    Yukon

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans