Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / Unable to cast object ...
Finance forum
Unanswered

Unable to cast object of type 'Dynamics.AX.Application.FormStringControl' to type error

Posted on by 17
Hi,
I am trying to have a lookup for line numbers in a custom table by writing the code in event handler but getting this error -'Unable to cast object of type 'Dynamics.AX.Application.FormStringControl' to type 'Dynamics.AX.Application.FormReference'. What can be resolution for the same.
 [FormControlEventHandler(formControlStr(A, B), FormControlEventType::Lookup)]
    public static void B_LineNumber_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        SysReferenceTableLookup tableLookup = SysReferenceTableLookup::newParameters(tableNum(X), sender);

        FormRun formRun =  sender.formRun();

        Query query = new Query();


        QueryBuildDataSource         qbds             = query.addDataSource(tableNum(X));          
        A                            solTable         = sender.formRun().dataSource(formDataSourceStr(A, B)).cursor();

        qbds.addRange(fieldNum(X, SolutionId)).value(solTable.SolutionId);

        tableLookup.parmQuery(query);
        tableLookup.addLookupfield(fieldNum(X, LineNumber));
        tableLookup.addLookupfield(fieldNum(X, SolutionId));

        tableLookup.performFormLookup();

        FormControlCancelableSuperEventArgs ce = e as FormControlCancelableSuperEventArgs;
        ce.CancelSuperCall();

       
    }
Please note I am getting an error at SysReferenceTableLookup line. I belive its because its fetching the recId but I want a string. Kindly let me know how may I tackle this error.
  • Layan Jwei Profile Picture
    Layan Jwei 6,964 Super User 2024 Season 2 on at
    Unable to cast object of type 'Dynamics.AX.Application.FormStringControl' to type error
    Hi,

    So you have form 'A' with control 'B'.  What's the type of control 'B'? is it string?
    Why you are not using SysTableLookup instead of SysReferenceTableLookup?

    And I'm confused, which table do you want to use. Sometimes you put X, sometimes you put B and sometimes you put A?

    I assume you want X

    maybe you meant the code to be like this? (just trying to re-write it quickly)
    [FormControlEventHandler(formControlStr(A, B), FormControlEventType::Lookup)] //here it means form A control B
    public static void B_LineNumber_OnLookup(FormControl sender, FormControlEventArgs e)
        {
            SysTableLookup tableLookup = SysTableLookup::newParameters(tableNum(X), sender);// I replaced SysReferenceTableLookup with SysTableLookup 
    
            FormRun formRun =  sender.formRun();
    
            Query query = new Query();
    
    
            QueryBuildDataSource         qbds           = query.addDataSource(tableNum(X));          
            X                            xTable         = formRun .dataSource(formDataSourceStr(A, X)).cursor(); //A is form so solTable should be of TypeX and not A and formDataSource should contain X and not B
    
            qbds.addRange(fieldNum(X, SolutionId)).value(xTable.SolutionId);
    
            tableLookup.parmQuery(query);
            tableLookup.addLookupfield(fieldNum(X, LineNumber));
            tableLookup.addLookupfield(fieldNum(X, SolutionId));
    
            tableLookup.performFormLookup();
    
            FormControlCancelableSuperEventArgs ce = e as FormControlCancelableSuperEventArgs;
            ce.CancelSuperCall();
    
           
        }
    ​​​​​​​

    Thanks,
    Layan Jweihan
    ​​​​​​​Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future

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

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,435 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,317 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans