Thursday, August 30, 2018

System can't cast 'LedgerJournalEngine)' to 'LedgerJournalEngine_VendInvoice' in Batch

I came across an issue recently where my Timesheets wouldn't post in 'Batch'.

It turns out that it wouldn't perform the following code in 'Batch', but would be fine doing it in the Client.

LedgerJournalEngine_VendInvoice         ledgerJournalEngine;
ledgerJournalEngine = LedgerJournalEngine::construct(LedgerJournalType::VendInvoiceRegister);

If we go to the 'construct' method, we will see it returns the 'default' in the switch case:

default :
            ledgerJournalEngine = new LedgerJournalEngine(_formRun);

When running this in the client, it works fine.  When running it in 'Batch' on the server, it fails:

Unable to cast object of type 'Dynamics.Ax.Application.LedgerJournalEngine' to type 'Dynamics.Ax.Application.LedgerJournalEngine_VendInvoice'.

This does make sense that you can't cast a 'Child' class to it's 'Parent', however, why would this work on the Client but not in Batch?

UPDATE:

The answer here is that in X++, this is allowed.  Thus, the client works.
In CIL, it is a bit more strict and it is not allowed, therefore it fails in 'Batch'

Simple solution is to just make this 'LedgerJournalEngine'.  That is the type that is being returned anyway so it works just fine.

No comments:

Post a Comment