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 :
Microsoft Dynamics AX (Archived)

unable to go from Query to SysQuery in wcf service call, but goes ok interactive - ax 2012

(0) ShareShare
ReportReport
Posted on by 530

Hi

Any input is appreciated for this one. I have a packingslip posting routine - which works okay - when triggered from a botton in Ax.

But when the same code is executed in a wcf service call - this cast error orccurs

urn:uuid:c3566c62-b0af-405a-a019-97b4650f3d79http://www.w3.org/2005/08/addressing/anonymouss:SenderUnable to cast object of type 'Dynamics.Ax.Application.Query' to type 'Dynamics.Ax.Application.SysQuery'. at Microsoft.BizTalk.Adapter.Wcf.Runtime.WcfClient`2.RequestCallback(IAsyncResult result)

I have tracked the error to this simple line:

Sysquery query;

Query = new query();

It is curious that if use query straight - no cast error occours - but this is not an option since the query must be used in a sysqueryrun class - which should be passed to the regular salesform letter classes.

I have tried full application compile + full cil compilation afterwards which should solve thise errors  -but not here.

regards

Søren

*This post is locked for comments

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

    The assignment SysQuery query = new Query(); is invalid in most object-oriented languages, because it's logically wrong. You can't simply said that a parent class has all data and behaviour as a child class. It's known that X++ allows such inverse polymorphism - unfortunately.

    You can safely write this code:

    Query query = new Query(); // Same types
    SysQuery query = new SysQuery(); // Same types
    Query query = new SysQuery(); // Polymorphism - allowed because SysQuery IS Query

    But this is wrong because not every Query is actually SysQuery;

    SysQuery query = new Query(); // Wrong in CLR

    It may be more obvious with another example:

    FileStream fs = new Object(); // Wrong - not every object is FileStream

    You have no troubles with the code in AX because X++ allows inverse polymorphism and there are is nothing to go wrong with SysQuery because it has only static members. If you had an instance method in SysQuery and tried to call it, X++ compiler wouldn't detect the problem and you'll get exception on runtime (Query object does not have method...). But that's not the case with CIL code.

    To resolve you problem, you should simply construct a new SysQuery, not Query instance.

  • Verified answer
    Soren Andersen Profile Picture
    530 on at

    That would be okay if it was possible to do what you suggest:

    this does not work - you cannot init a sysquery like this

    static void Job30(Args _args)
    {
        SysQuery                 query = new SysQuery();
        QueryBuildDataSource     queryBuildDSSalesTable;
        QueryBuildDataSource     queryBuildDSSalesLine;
        QueryBuildDataSource     queryBuildDSInventDim;
        SysQueryRun                            queryRun;
        SalesLine                            salesLine;
        ;

        // Setup the primary datasource.
        queryBuildDSSalesTable  = query.addDataSource(tablenum(SalesTable));

        // Setup the secondary (joined) datasource.
        queryBuildDSSalesLine   = queryBuildDSSalesTable.addDataSource(tablenum(SalesLine));
        queryBuildDSSalesLine.joinMode(JoinMode::InnerJoin);
        queryBuildDSSalesLine.relations(true);
        queryBuildDSInventDim = queryBuildDSSalesLine.addDataSource(tableNum(InventDim));
        queryBuildDSInventDim.joinMode(JoinMode::InnerJoin);
        queryBuildDSInventDim.relations(true);

        queryRun = new SysQueryRun(query);

        while(queryRun.next())
        {
            salesLine   = queryRun.get(tablenum(SalesLine));
            info(strfmt('%1',salesLine.SalesId));
        }

    but if you change the init to this:
        SysQuery                 query = new Query();

    Then you are OK. If you use an aot query into a sysQuery - you have to do a cast as well

    the problem is I have to pass a SysQueryRun object to the salesformletter class (.chooselinesquery())

    And I am not able to create this object in my service call - since it cannot be created from a query object...

    So it seems I have to settle for less tight integrated batch post routine..

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

    All right, I didn't know the rest of your code - and also the problem with QueryRun's constructor (I always use Query instance for that).
    You say that you can't use SysQuery because of QueryRun's constructor and Query neither because of SalesFormLetter class. If it's so, you need both types. But there is a solution - use Query for QueryRun and then create a SysQuery instance from Query (SysQuery q = new SysQuery(query)) and use it in SalesFormLetter.

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans