Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

Buf2Buf error in tables with Inheritance

(0) ShareShare
ReportReport
Posted on by 157

Hello to everyone. 

I have 2 tables. TableA and TableB. TableB use inheritance and extends TableA

It seems that everything works fine but I have one weird issue. 

I use a method like this: 

public void createElement(TableA _parentRecord, TableA _copyRecord)
{
    TableA newRecord;

    if (_copyRecord)
    {
        buf2Buf(_copyRecord, newRecord);
        //Other Code
    }
    else
    {
        buf2Buf(_parentRecord, newRecord);
        //Other Code
    }


I always recieve an error that inform that  "The field with ID '36' does not exist in table 'TableA'

I entered to the debugger inside the buf2Buf() to understand why this happens and I saw that the _from buffer is TableB and not TableA

even if I tried to cast again the buffers to TableA , when it goes inside the buf2buf method the type is TableB. 

Does anyone know why this happens?

  • Martin Dráb Profile Picture
    Martin Dráb 230,842 Most Valuable Professional on at
    RE: Buf2Buf error in tables with Inheritance

    I guess you mean that the type of the data source is TableA, but it doesn't necessarily mean that the type of the record is TableA too. F&O supports polymorphism in forms.

  • SGbron Profile Picture
    SGbron 157 on at
    RE: Buf2Buf error in tables with Inheritance

    Exactly

    it was solved with just a find method.

    TableA parent = TableA::findRecId(_parentRecord.RecId);
    
    buf2Buf(parent, newElement);


    And now the returned type of the Object is TableA instead of TableB

    The thing that i dont understand is how it is posible to give in the method a TableA record from the cursor() position in the form and inside the method that record is a TableB record. 

    Anyway the problem is solved. 

    Thank you Martin

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,842 Most Valuable Professional on at
    RE: Buf2Buf error in tables with Inheritance

    I don't think you'll get it working when using two different types (TableA and TableB). But if you're interested just with fields from the parent table, you could select its buffer directly.

    You can see an example below. I changed the types to DirPartyTable and DirPerson, to be able to test the code.

    void run()
    {
        DirPerson person;
        select firstOnly person;
    
        this.createElement(person);
    }
    
    public void createElement(DirPartyTable _parentRecord)
    {
        DirPartyTable input;
    
        if (_parentRecord.TableId == tableNum(DirPartyTable))
        {
            input = _parentRecord;
        }
        else
        {
            select firstonly input
                where input.RecId == _parentRecord.RecId;
        }
    
        DirPartyTable newRecord;
        buf2Buf(input, newRecord);
        info(newRecord.Name);
    }

  • SGbron Profile Picture
    SGbron 157 on at
    RE: Buf2Buf error in tables with Inheritance

    Hello Martin.

    Exactly, I want to ignore all fields that are coming from TableB. Becasue I received this error with the fieldId.

    And also I dont need them.

    .TableA does contain all the fields that I want. So what i am trying to do is to copy one record and I jsut want to modify some fields values after. Because there are many fields (almost 90%) that will remain the same.

    .data it was the first thing that I did but it didn't anything from values from the _parentRecord.

    I thought that this is becasue microsoft recomends on inheritance to use buf2buf instead of data.

    learn.microsoft.com/.../dynamics.ax.application.xrecord.data

  • Martin Dráb Profile Picture
    Martin Dráb 230,842 Most Valuable Professional on at
    RE: Buf2Buf error in tables with Inheritance

    Casting an object to a parent type indeed doesn't change its type. It's still the same object as before.

    What is your intention here? Do you want to ignore all fields define in TableB? If not, your attempt to convert TableB to TableA wouldn't meet your requirements, even if it worked.

    In short, it seems that buf2buf() doesn't support what you want. Can't you achieve your goal with data() method?

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

Announcing Our 2025 Season 1 Super Users!

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

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans