Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Supply chain | Supply Chain Management, Commerce
Answered

How to find the line of code that is causing the error?

(0) ShareShare
ReportReport
Posted on by 314

Hi friends, my name is Antony.

I recently encountered an error when I posted a Job Card on a Production Order.
The error says:
Unable to cast object of type 'Dynamics.AX.Application.ProdRouteJob' to type 'Dynamics.AX.Application.ProdJournalBOM'.

2262.pastedimage1622908699481v2.png

I've tried to debug this line of code by putting a breakpoint on the class that runs on Post Button and also attaching the process to w3wp.exe.

7343.pastedimage1622909097883v3.png

Then the error appears in output window, but I don't know how to find out which method/class/line is causing the error.
Is this method of debugging is correct? How do I find the line/method that is causing the error?

Any suggestions will be very helpful. Thank you.

  • Michael Antony Profile Picture
    314 on at
    RE: How to find the line of code that is causing the error?

    Hi Martin. Thanks for your advice.

    I accidentally found the error when I checked the ProdRouteJob extension class. In the event handler method I incorrectly declared a variable that should be ProdRouteJob instead ProdJournalBOM which caused the error.

    But I'll try and learn your suggested approach to dealing with this kind of error.

    Regards,

    Antony

  • Michael Antony Profile Picture
    314 on at
    RE: How to find the line of code that is causing the error?

    Hi Nikolaos. Thank you for your advice.
    I've tried putting a breakpoint in Classes\Info\add but the call stack doesn't show up when this error occurs.

    Regards,
    Antony

  • Martin Dráb Profile Picture
    232,996 Most Valuable Professional on at
    RE: How to find the line of code that is causing the error?

    I don't think that Info.add() is called for managed exceptions like this. At least it didn't work for me the last time when I tried it.

  • Verified answer
    nmaenpaa Profile Picture
    101,158 Moderator on at
    RE: How to find the line of code that is causing the error?

    In addition to Martin's suggestion, a general good approach is to put a breakpoint in Classes\Info\add, this way you can always find out the call stack of any process that prints in the infolog. Including your current exception.

  • Verified answer
    Martin Dráb Profile Picture
    232,996 Most Valuable Professional on at
    RE: How to find the line of code that is causing the error?

    A fast way of finding the place may be configuring Visual Studio to break on InvalidCastException.

    Go to Debug > Windows > Exception Settings, search for InvalidCastException and tick System.InvalidCastException in the list of Common Language Runtime Exceptions. Then attach the debugger and try the process again. The debugger should stop at the place where the exception is thrown.

  • Suggested answer
    André Arnaud de Calavon Profile Picture
    294,261 Super User 2025 Season 1 on at
    RE: How to find the line of code that is causing the error in AX365?

    Hi Michael,

    What is the version of your Dynamics 365 application? Do you have customizations? If so verify if this could be causing it.

    If you don't have customizations, you can search on LCS issue search for known issues. I couldn't find this specific error, but some other which were identical on other application areas. It might be a bug if you don't have customizations yourself. In that case, contact Microsoft Support.

  • Ludwig Reinhard Profile Picture
    Microsoft Employee on at
    RE: How to find the line of code that is causing the error in AX365?

    moved to the d365 forum

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 232,996 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans