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 :
Finance | Project Operations, Human Resources, ...
Answered

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

(1) ShareShare
ReportReport
Posted on by 183
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.
I have the same question (0)
  • Verified answer
    Layan Jwei Profile Picture
    8,097 Super User 2025 Season 2 on at
    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
  • Bharani Preetham Peraka Profile Picture
    3,634 Moderator on at
    Please share us the exact code since we are not able to understand what is being used in the lookup as a table and also if you need a recid or string since you are using SysTableReferenceTableLookup. Also please give us more information on the business requirement.
  • spidey1010 Profile Picture
    183 on at
    HI Layan,
    Thanks for the response, SysTableLookup solved my problem.
     

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 663 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 348 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans