web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Entity key for a table with RecId

(4) ShareShare
ReportReport
Posted on by 2,009
Hi,

I have two tables:

Header table, with an index of allow duplicate No (AccountNum, Id, Date, RecId)
 
Lines table, with an index of allow duplicate No (RefRecId, LineNum)
It's relation with HeaderTable is:
Line.RefRecId = Header.RecId
 
Now, i was asked to create a new table that has a relation with the LinesTable (each Line can have more than one record in this new table that is related to it)
I made the relation as this:
NewTable.RefRecId = Line.RecId

And I made index as (RefRecId, LineNum)

Now i need to create an entity for this new table, in order to import records to it via DMF for an existing Line
How to design the entity? and what will the entity key be?

 
Categories:
I have the same question (0)
  • Suggested answer
    André Arnaud de Calavon Profile Picture
    305,643 Super User 2026 Season 1 on at
    Hi ..,
     
    You can create a data entity starting with the new (third?) table as data source. You can then, based on the table relations, add the other tables (lines and header). 
    You can then select the fields that will make the natural key, e.g. AccountNum, Id, Date, LineLineNum, NewTableLineNum. You can use this combination as entity key for the data entity. You can check for required properties or additional X++ coding on standard entities. The staging table will also use that as unique key. From the staging to the target, it can then check for existing records in the header and lines table where in the new table the records will be added or updated.
     
    Note that the index on the header table doesn't make sense to me. You are not allowing duplicates where you included the RecId field. In that case, no record will be a duplicate as the RecId is unique per table.
     
     
  • .. Profile Picture
    2,009 on at
    Hi Andre,

    i will try it and get back to you.

    But for two of the points you mentioned:

    1.  for this sentence "you can check for required properties or additional X++ coding on standard entities" Do you mean the relations won't be enough and i might need to write extra code in the entity?
     
    2. I agree that RecId is unique, so it should be enough to be in the index without the other fields (accountNum, Id, Date)
    But if u look at the standard CustInvoiceJour, they added RecId next to other fields. Why they didn't stick with RecId alone?
  • Martin Dráb Profile Picture
    239,684 Most Valuable Professional on at
    The index doesn't indeed enforce uniqueness in any better way than RecId alone, but that's not the only purpose of indexes. The index will greatly improve performance if you search or sort by InvoiceId and InvoiceDate, for example.
     
    Including RecId is beneficial for queries involving only InvoiceId, InvoiceDate, NumberSequenceGroup and RecId. Then the DB server can find all the data in the index and it doesn't have to look into the actual table at all.
  • Layan Jwei Profile Picture
    8,239 Super User 2026 Season 1 on at
    Hi @Martin Dráb,

    I think the index means that the three fields InvoiceId, InvoiceDate and numbersequence are together not unique, that's why they added RecId

    but again, adding RecId alone would have been enough, but u are saying it will be good for performance to include all. As you said this senetence:
    "Including RecId is beneficial for queries involving only InvoiceId, InvoiceDate, NumberSequenceGroup and RecId"
    But why would there be a query that looks for InvoiceId, invoiceDate, NumberSequence and RecId? i mean if we know the recId, why would the query include other fields? shouldn't the query only look for RecId?
     
  • .. Profile Picture
    2,009 on at
    Hi Andre,

    I add the tables New table (joined with the line table as read only, and this one joined with header table as read only)
    I added the following fields to the entity:
    AccountNum, Id, Date, LineLineNum, NewTableLineNum

    I exported the entity and it worked fine. Then I tried to import,
    i got an error that i need to fill all fields mentioned above in addition to reference


    Do i need to add RefRecId of the NewTable(Third) to the entity as private
    And also add RecId of the Line table to the entity as private
    then add code to mapEntityToDatasource?
    because i did this, and this time i got one error only regarding reference Field
    what to do?
      public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
        {
            next mapEntityToDataSource(_entityCtx, _dataSourceCtx);
     
            switch (_dataSourceCtx.name())
            {
                case dataEntityDataSourceStr(Entity1, ThirdTable):
                    ThirdTable thirdTable = _dataSourceCtx.getBuffer();
                    thirdTable.RefRecId = this.LineRecId;
                    break;
            }
        }


    Hi Martin,

    When you say beneficial for queries, do you mean why i do select statement and put those 4 fields in where condition like this:

    select *From custInvoiceJour where InvoiceId == xx && InvoiceDate ==yy && NumberSequenceGroup == zz && RecId == aa;

    but if i have "aa" value, then shouldn't i do this directly instead?
    select *From custInvoiceJour where custInvoiceJour.RecId == aa;
  • .. Profile Picture
    2,009 on at
    Hi,

    Actually there is no need to add refRecId of Third table and no need to add RecId of line table (i was doing sth wrong)

    i sticked to adding those fields only
    AccountNum, Id, Date, LineLineNum, NewTableLineNum
     
    Then i added this code below and it works

    but why Id was coming as empty and i had to set it by code as below, why this field specifically?
      public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
        {
            super (_entityCtx, _dataSourceCtx);
     
            switch (_dataSourceCtx.name())
            {
                case dataEntityDataSourceStr(Entity1, ThirdTable):
                    Line line;
                    Header header;
    
                    select refRecId from line 
                    where  line.LineNum == this.LineNum
                    join Id from header
                       where line.RefRecId == header.RecId
                              && header.Id == this.Id
    
                    ThirdTable thirdTable = _dataSourceCtx.getBuffer();
    
                    thirdTable.RefRecId = line.LineRecId;
                    thirdTable.Id = header.Id; //i don't know why this field is coming as empty, so i had to fill it here
                    break;
            }
        }
    
  • Martin Dráb Profile Picture
    239,684 Most Valuable Professional on at
    .., you missed my point. My statement wasn't that it's beneficial for any query; I was talking about a situation when the query uses just fields included in the index. It's not the case of your query.
     
    Your query select * from custInvoiceJour where custInvoiceJour.RecId == aa isn't an example of a query involving just those four fields, therefore the data can't be returned just from an index.
  • .. Profile Picture
    2,009 on at
    Hi Martin,

    Do you mean select Id, AccountNum, Date, RecId from custInvoiceJour?
    i mean you mean selecting fields and not the where condition?


    Also can you please help me with the code in my previous reply and the way i designed the entity. I asked why Id is empty in the previous reply and why i need to fill it by code?
  • André Arnaud de Calavon Profile Picture
    305,643 Super User 2026 Season 1 on at
    Hi ..,

    I'm a bit confused. Does the third/new table table has a reference to the header table? In your question you mentioned that the new table has a relation with the Line table. In case you have additional fields, then you either include a relation from the third table to the header or, like you have now, add some X++ logic.
  • .. Profile Picture
    2,009 on at
    Hi Andre,
     
    No the third table doesn't have a relation with header
     
    Third table has a relation with the Line
     
    And the Line has relation with the header
     
    Why do i need to add logic for the Id?
    For example AccountNum and Date also come from header, but there was no need to handle them by x++, only Id was empty which is weird. Any idea?

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 681 Super User 2026 Season 1

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 598 Super User 2026 Season 1

#3
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 579

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans