web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

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

I have the same question (0)
  • Martin Dráb Profile Picture
    237,882 Most Valuable Professional on at

    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?

  • Oestfeldt Profile Picture
    25 on at

    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.

  • Oestfeldt Profile Picture
    25 on at

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

  • Martin Dráb Profile Picture
    237,882 Most Valuable Professional on at

    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

    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
    237,882 Most Valuable Professional on at

    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

    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

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 > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans