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

Data entity method - to create additional line depending on the itemId

(0) ShareShare
ReportReport
Posted on by 678

Hi All,

I am using a data entity to create a InventJournalTrans_Tag record from Power Apps app.

I used the method mapEntityToDataSource to map inbound entity fields to underlying target datasource.

and now I have a requirement to create additional InventJournalTrans_Tag  line if the mapped inbound field value, itemId is a certain value. 

in which data entity method should I put the logic of adding additional InventJournalTrans_Tag line ? 

Thank you.

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    mapEntityToDataSource method would be a good candidate.

  • ShawnDEV Profile Picture
    678 on at

    Hi,

    do you mean the same method I using currently?

    I actually added the following additional code and then I get an error that 'Cannot create a record in Tag counting (InventJournalTrans_Tag). Journal: xx, 4.0000000000. The record already exists'.  This is because the data entity is trying insert two times at the same time instead of insert it in a sequence. Is my code wrong?

    public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
    {
        ...
            if (this.itemId == 'A')
            {
                inventJournalTrans_Tag.itemId = 'B';
                inventJournalTrans_Tag.insert();
            }

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Yes, you need to introduce new local variable for your new table buffer that you're going to insert.

  • ShawnDEV Profile Picture
    678 on at

    Hi,

    I introduced a new local variable 'inventJournalTrans_Explode'. But the inventJournalTrans_Tag.LineNum is equal to inventJournalTrans_Explode.LineNum which causes the error mentioned above. why is the LineNum is not updated even after introducing a new variable - I think it is because the SQL database record is not updated yet? and what should I do to get the next number?

    Thank you.

    	public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx)
    	{
    	    InventJournalTrans_Tag inventJournalTrans_Tag;
    	    InventJournalTrans_Tag inventJournalTrans_Explode;
    	    
            switch (_dataSourceCtx.name())
            {
                case dataEntityDataSourceStr(InventInventoryTagCountingJournalLineBoliaDWEntity, InventJournalTrans_Tag):
    
                    inventJournalTrans_Tag = _dataSourceCtx.getBuffer();
    	    
            		ttsbegin;
            		...
            		inventJournalTrans_Tag.LineNum = InventJournalTrans_Tag::lastLineNum(this.JournalNumber)   1;
            		
                    if (this.itemId == 'A')
                    {
            			ttsbegin;
                        inventJournalTrans_Explode.itemId = 'B';
            			inventJournalTrans_Explode.LineNum = InventJournalTrans_Tag::lastLineNum(this.JournalNumber)   1;
                        inventJournalTrans_Explode.insert();
            			ttscommit;
                    }
            		ttscommit;

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi ShawnDEV,

    debugging should help you here. I'm quit sure that no records were inserted between your calls to lastLineNum, therefore it returns the same line number for both.

    You have many simple solutions to choose from. You simply must make sure that those two lines get a different number. And you have full control of both line numbers in your code.

    Here are some possibilities:

    1) Move line 14 to happen only after your custom line is inserted. Then this inserted line and it's line number can be taken into account when you call lastLineNum again

    2) Replace line 20 with

    inventJournalTrans_Explode.LineNum = InventJournalTrans_Tag.LineNum   1;

  • ShawnDEV Profile Picture
    678 on at

    Hi,

    I think the line number is not an issue. It still shows that the record already exist.

    Let me know if you have any idea on this. Meanwhile I will try to find the cause.

    Thank you.

  • Verified answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Just check the unique index fields of that table, debug the insert method and try to find out why duplicate data is inserted. I'm quite sure that line number also matters here.

    Also are you sure that you populated all other fields that you want to insert? You only shared part of your code so we can't know whether you just didn't share some detail or whether your actual code lacks something. Such as JournalNum. 

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 646 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans