Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

Unable to cast object of type 'Dynamics.AX.Application.SalesTable' to type 'SalesLine'

(1) ShareShare
ReportReport
Posted on by 1,133
Hi,

I added a RecId field to SalesLine, then added this field as a reference group to SalesTable form.
The field is added in SalesLine grid.
When i first click add line, then click on the lookup for the control and select a value, it works fine. It's only when i empty the value from this field and move to another field i get this error:
Unable to cast object of type 'Dynamics.AX.Application.SalesTable' to type 'Dynamics.AX.Application.SalesLine'

[ExtensionOf(FormDataFieldStr(SalesTable, SalesLine, CodeXRecId))]
final class SalesTableFormSalesLineDS_CodeXRecId_Extension
{

    public void modified()
    {
        Table1                      table1;
        Table2                      table2;

        FormDataSource  formDataSource      = element.datasource();
        SalesLine       salesLine           = formDataSource.cursor(); // it errors here

        next modified();

        RefRecId CodeXRecId = salesLine.CodeXRecId;

        select firstOnly CodeY, Id from table1
            where table1.RecId == salesLine.CodeXRecId


        salesLine.ItemId = table1.CodeY;
        formDataSource.object(fieldNum(SalesLine, ItemId)).modified();

        salesLine.CodeXRecId   = CodeXRecId;
        salesLine.Id         = table1.Id;
    }

}
 
Categories:
  • .. Profile Picture
    .. 1,133 on at
    Unable to cast object of type 'Dynamics.AX.Application.SalesTable' to type 'SalesLine'
    Hi Martin,

    but why the datasource was SalesLine when filling the field, then when emptying it, it became SalesTable?
    this extension method is for a control under salesLine, why would the datasource change then?

    and if i'm going to use element.salesLine directly to get the salesLine buffer,
    then how to define datasource for this line below? how can i get the datasource if i'm not going to use element.dataSource()?
     formDataSource.object(fieldNum(SalesLine, ItemId)).modified();

    Hi Mohammed,

    It worked, but i don't understand why

    what's the difference between 
     FormDataObject 	formDataObject = any2Object(this) as FormDataObject; 
     FormDataSource 	formDataSource = formDataObject.datasource(); 
     SalesLine 	        salesLine      = formDataSource.cursor(); 

    and
     
     FormDataSource  formDataSource      = element.datasource();
    SalesLine        salesLine           = formDataSource.cursor();
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,382 Most Valuable Professional on at
    Unable to cast object of type 'Dynamics.AX.Application.SalesTable' to type 'SalesLine'
    The error says that you're trying to put a SalesTable buffer to a variable of SalesLine type. When you look at the code throwing the error, it's clear that formDataSource.cursor() returns SalesTable, while you incorrectly believe that it should return SalesLine.
     
    Your code in an extension of field of SalesLine data source, therefore you likely want the current SalesLine buffer, which isn't what you get from element.datasource(). You can simply use element.salesLine to access the buffer.
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 9,743 Super User 2024 Season 2 on at
    Unable to cast object of type 'Dynamics.AX.Application.SalesTable' to type 'SalesLine'
    Hi @..,
     
    Try this code :
    [ExtensionOf(FormDataFieldStr(SalesTable, SalesLine, CodeXRecId))]
    final class SalesTableFormSalesLineDS_CodeXRecId_Extension
    {
        public void modified()
        {
    	Table1  	table1;
            Table2       	table2;
            FormDataObject 	formDataObject = any2Object(this) as FormDataObject; 
            FormDataSource 	formDataSource = formDataObject.datasource(); 
            SalesLine 	salesLine; 
    
            next modified(); 
    
            salesLine = formDataSource.cursor(); 
            RefRecId CodeXRecId = salesLine.CodeXRecId;
    
            select firstOnly CodeY, Id from table1
                where table1.RecId == salesLine.CodeXRecId
    
    
            salesLine.ItemId 	= table1.CodeY;
            formDataSource.object(fieldNum(SalesLine, ItemId)).modified();
    
            salesLine.CodeXRecId   	= CodeXRecId;
            salesLine.Id         	= table1.Id;
        }
    }
    Best regards,
    Mohamed Amine MAHMOUDI

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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,382 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans