Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Finance forum / How to get Orig in Dat...
Finance forum

How to get Orig in DataEventHandler "Updated"

(0) ShareShare
ReportReport
Posted on by 25

Hi

I have (In Dynamics 365 FO) made a class as a DataEventHandler for Updated on the table CustTable.

When I want to compare a field, to see is the data has been changed, I call CustTable.Orig().

This works just fine, if there is a change. But if there is no change upon clicking "Update", there is no orig, and I get this error: "Object reference not set to an instance of an object."

I would like to check whether to orig is null or not, befor using it, so I don't get the error.

How do I code (X++) a check for whether the Orig is null ?

Best Regards

Peter

  • Oestfeldt Profile Picture
    25 on at
    RE: How to get Orig in DataEventHandler "Updated"

    Hi again

    I have now tried, and you are right. Orig is not null  :-)

    Teh error comes when I compare CustTable.PaymTermId with origCustTable.PaymTermId.

    The error must be that the Orig is not instanciated when there is no change.

    I will have to dig deeper into this.

    Thanks

  • Martin Dráb Profile Picture
    232,866 Most Valuable Professional on at
    RE: How to get Orig in DataEventHandler "Updated"

    Unfortunately it didn't answer my question. Please walk through the following piece of code in debugger and tell me at which line it fails.

    CustTable origCustTable = custTable.orig();
    if (origCustTable) {}
    if (custTable.orig()) {}
    

    You can see exception details (including the stack trace) in the debugger, at Locals:

    Locals.jpg

  • Oestfeldt Profile Picture
    25 on at
    RE: How to get Orig in DataEventHandler "Updated"

    I will try to be more precise.

    Orig is not null if an update is performed with change.

    Orig is null if an update is performed without change.

    If in the code I write "if (CustTable.Orig()), the error is thrown in D365FO. I see no stack trace. The error only comes if the orig is null. If there is a field in CustTable that has been changed, the orig is not null, and all is good.

    I hope this is more clear.

  • Martin Dráb Profile Picture
    232,866 Most Valuable Professional on at
    RE: How to get Orig in DataEventHandler "Updated"

    It doesn't make sense to me. You're saying that CustTable.orig() returns null, i.e. it doesn't throw any exception. And then you're saying that if (CustTable.orig()) fails, but because CustTable.orig() returns null, it's effective the same as if (null). I don't see where is could fail.

    Can you please explicitly confirm that CustTable.orig() works and if (CustTable.orig()) throws "Object reference not set to an instance of an object"?

    What is the stack trace of the exception?

  • Oestfeldt Profile Picture
    25 on at
    RE: How to get Orig in DataEventHandler "Updated"

    P.S. It does not throw an error when building the code. The error comes upon executing from D365FO

  • Oestfeldt Profile Picture
    25 on at
    RE: How to get Orig in DataEventHandler "Updated"

    Hi Martin

    Thanks for your fast respons.

    orig() returns null, and if I use "if (CustTable.Orig())" or something like that, I get the error.

    Therefore I need to check whether orig is null or not.

    I have tried "if (CustTable.Orig())" and "if (CustTable.Orig() != null)". Both throws the error.

  • Martin Dráb Profile Picture
    232,866 Most Valuable Professional on at
    RE: How to get Orig in DataEventHandler "Updated"

    Are you saying that orig() throws an exception, or that orig() returns null and you get an exception when you're trying to access fields values of this null reference? If it's the latter, simply use something like if (origCustTable).

    Nevertheless it sounds a bit strange to me. The fact that you didn't change anything doesn't mean that there was no pre-update version of the record, which is what orig() returns. What if you use CoC on CustTable.update() and get orig() before calling next? Will it behave correctly?

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,095 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans