xRec in OnBefore Modify Event

  • xRec in OnBefore Modify Event

    Posted by genns@technicalprospects.com on February 20, 2018 at 5:46 pm
    • Greg Enns

      Member

      February 20, 2018 at 5:46 PM

      I want to execute specific code anytime Field1 is modified on a record we’ll call Rec.

      So I created an event subscriber to the OnBeforeModify event with a simple if statement:
      IFĀ  xRec.Field1 <> Rec.Field1 THEN …

      I created a test codeunit that did this:
      Rec.GET;
      IF Rec.Field1 = 0 THEN BEGIN
      Ā  Ā  Ā  Rec.Field1 := 5;
      Ā  Ā  Ā  Rec.MODIFY;
      END;
      Using the debugger, I stepped through line by line, and the first code to execute after the MODIFY is my subscriber. Once in the subscriber function, the debugger shows that xRec.Field1 and Rec.Field1 are both 5.Ā 

      What would cause this?

      ——————————
      Greg Enns
      ERP Coordinator
      Technical Prospects
      Kaukauna WI
      ——————————

    • Naveen Jain

      Member

      February 20, 2018 at 7:55 PM

      You code should trigger on the OnValidate of the field.

      ——————————
      Naveen Jain
      Director of IT
      Symbex Companies
      Santa Fe Springs CA
      ——————————
      ——————————————-

    • Greg Enns

      Member

      February 21, 2018 at 9:15 AM

      Hi Naveen,

      That trigger is skipped if the field is set in code like in my example above. That’s why I chose not to use that trigger.
      This blog? shows how OnBeforeModify event is supposed to hold the xRec values.

      ——————————
      Greg Enns
      ERP Coordinator
      Technical Prospects
      Kaukauna WI
      ——————————
      ——————————————-

    • Suresh Kulla

      Member

      February 21, 2018 at 10:12 AM

      Greg,
      I just tried a simple example with showing the rec and xrecĀ values with message box and they are showing different. One thing I noticed is you are using Rec.GET which will again retrieve the old record and that could be the reason why both values are the same, why are you using Rec.GET statement.

      ——————————
      Thanks
      Suresh Kulla
      NAV Technical Architect
      ArcherPoint Inc.
      Atlanta GA
      ——————————
      ——————————————-

    • Greg Enns

      Member

      February 21, 2018 at 11:35 AM

      Thanks . Sorry I was unclear.
      My Rec.GET? was just for my test code to modify the record.
      So the full code looks like this…
      Test CodeunitĀ  OnRun
      Rec.GET(‘PK1’);
      IF Rec.Field1 = 0 THEN BEGIN
      Ā  Ā  Ā  Rec.Field1 := 5;
      Ā  Ā  Ā  Rec.MODIFY; (This triggers the event)
      END;

      Event Function OnBeforeModifyRec
      IF xRec.Field1 <> Rec.Field1 THEN
      Ā  Ā  Ā Executecode…

      If I modify Field1 in the UI manually, then the Subscriber function recognizes the change and knows that xRec.Field1 <> Rec.Field1. But when I modify it via code like with my test codeunit above, then xRec.Field1 = Rec.Field1. I just tested this on a stock NAV table.field, and got the same results.
      If I add an xRec.GET at the beginning of the Subscriber everything works properly.Ā 
      Is this a bug? Is it fixed in a later CU?Ā Ā 
      I’m on NAV 2016 CU 19.

      ——————————
      Greg Enns
      ERP Coordinator
      Technical Prospects
      Kaukauna WI
      ——————————
      ——————————————-

    • Suresh Kulla

      Member

      February 21, 2018 at 12:16 PM

      Did you try using Rec.MODIFY(TRUE) instead of Rec.MODIFY.

      ——————————
      Thanks
      Suresh Kulla
      NAV Technical Architect
      ArcherPoint Inc.
      Atlanta GA
      ——————————
      ——————————————-

    • Greg Enns

      Member

      February 21, 2018 at 12:31 PM

      Yes. No luck. I also triedĀ  Rec.VALIDATE(Field1,5);
      That didn’t work either.

      ——————————
      Greg Enns
      ERP Coordinator
      Technical Prospects
      Kaukauna WI
      ——————————
      ——————————————-

    • Jason Wilder

      Member

      February 22, 2018 at 4:29 PM

      Certainly seems to be a bug.Ā  I get the same behavior in NAV 2017.Ā  At least you have a work around which is to GET xrec.Ā  I did something like this per your suggestion (using the Customer table in my example):
      xRec.GET(xRec.”No.”); and everyting seems to work fine.

      I think it would be a good idea for people to check their Subscriptions to see if they are using Xrec in an OnBefore Event and make this change at the top of the EventSubscriber Function if they are.

      Good find!

      ——————————
      Jason Wilder
      Senior Application Developer
      Stonewall Kitchen
      York ME
      ——————————
      ——————————————-

    • Greg Enns

      Member

      February 23, 2018 at 2:47 PM

      Thanks for testing this on 2017Ā  !Ā 
      Anybody able to help submit this to Microsoft?? I’d be happy to be part of the process–I’ve never done it myself.

      ——————————
      Greg Enns
      ERP Coordinator
      Technical Prospects
      Kaukauna WI
      ——————————
      ——————————————-

    genns@technicalprospects.com replied 7 years, 6 months ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

The discussion ‘xRec in OnBefore Modify Event’ is closed to new replies.

Start of Discussion
0 of 0 replies June 2018
Now

Welcome to our new site!

Here you will find a wealth of information created for peopleĀ  that are on a mission to redefine business models with cloud techinologies, AI, automation, low code / no code applications, data, security & more to compete in the Acceleration Economy!