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 :
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.

I have the same question (0)
  • Ludwig Reinhard Profile Picture
    Microsoft Employee on at

    moved to the d365 forum

  • Suggested answer
    André Arnaud de Calavon Profile Picture
    300,928 Super User 2025 Season 2 on at

    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.

  • Verified answer
    Martin Dráb Profile Picture
    237,817 Most Valuable Professional on at

    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.

  • Verified answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    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.

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

    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.

  • Michael Antony Profile Picture
    314 on at

    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

  • Michael Antony Profile Picture
    314 on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Supply chain | Supply Chain Management, Commerce

#1
Siv Sagar Profile Picture

Siv Sagar 239 Super User 2025 Season 2

#2
Laurens vd Tang Profile Picture

Laurens vd Tang 213 Super User 2025 Season 2

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 132 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans