Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

OnBeforeModifyEvent - xRec

(0) ShareShare
ReportReport
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
    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
    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
    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
    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
    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
    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
    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
    on at
    RE: OnBeforeModifyEvent - xRec

    Thanx, Dennis.

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

  • Community Member Profile Picture
    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
    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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics NAV (Archived)

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 2

#1
mmv Profile Picture

mmv 2

#1
Amol Salvi Profile Picture

Amol Salvi 2

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans