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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Unable to cast object of type 'Dynamics.AX.Application.PurchPurchaseOrderContract' to type 'Dynamics.AX.Application.SalesInvoiceContract'.

(0) ShareShare
ReportReport
Posted on by 90

I just shifted the code to another version and I got this error while debugging

pastedimage1653981085511v1.png

After debugging, I got this error

pastedimage1653981597745v1.png

I have the same question (0)
  • AnithaEswaran Profile Picture
    2,032 on at

    Hi ,

    The error states there is some issue in line 684 in the class 'SalesInvoiceController' .

    Have you added any custom code in the extension?

  • Ashrafhk Profile Picture
    90 on at

    No

  • AnithaEswaran Profile Picture
    2,032 on at

    In your first pic, the code looks like a custom one.

    Can you explain where this code is and what process calls this code?

  • Ashrafhk Profile Picture
    90 on at

    Yes that code was custom. And that code is related to my custom class.

    pastedimage1653991640060v1.png

    I want to run SO that process is calling this code.

  • Martin Dráb Profile Picture
    239,022 Most Valuable Professional on at

    You know the problem - there is an (invalid) attempt to put PurchPurchaseOrderContract instance to a variable of SalesInvoiceContract type.

    And you know where exactly it happens - in SalesInvoiceController.preRunModifyContract(), as line 684. It's obvious that PurchPurchaseOrderContract shouldn't be used there, therefore it seems that you've somehow put a wrong constract there.

    Unfortunately your last screenshot doesn't show any code (just the method declaration), therefore we can't comment on it. But what I said should give you an idea about where to look for the bug.

  • Ashrafhk Profile Picture
    90 on at

    I did just customization on my custom form and did not use PurchPurchaseOrderContract, SalesInvoiceContract, SalesInvoiceController.preRunModifyContract().

    Got an error(Unable to cast object of type 'Dynamics.AX.Application.PurchPurchaseOrderContract' to type 'Dynamics.AX.Application.SalesInvoiceContract') on this line of code while debugging

    salesFormLetter.update(salesTable,DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()),SalesUpdate::All,AccountOrder::None,noYes::No,noYes::Yes);

    pastedimage1654071985711v1.png

  • Verified answer
    Martin Dráb Profile Picture
    239,022 Most Valuable Professional on at

    No, this isn't the line where the error is thrown. It's thrown at SalesInvoiceController.preRunModifyContract(), as discussed above.

    It might suggests that you don't know how to debug standard code. Is it the case?

    You see that you're unable to identify the cause if you don't go deeper with your debugging.

  • Ashrafhk Profile Picture
    90 on at
    [deleted]
  • Ashrafhk Profile Picture
    90 on at

    Thanks a lot Martin, when I went deeper with my debugging I got the actual cause.

  • Ashrafhk Profile Picture
    90 on at

    I got the actual cause, someone set the custom purchase order report in sales module.

    Here is the code for new setup.

    class PrintMgtDocTypeHandlersExt

    {

       [SubscribesTo(classstr(PrintMgmtDocType), delegatestr(PrintMgmtDocType, getDefaultReportFormatDelegate))]

       public static void getDefaultReportFormatDelegate(PrintMgmtDocumentType _docType, EventHandlerResult _result)

       {

           _result.result(ssrsReportStr(SalesInvoice, Report));

       }

    }

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 617

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 461 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 298 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans