Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / How to insert data in ...
Finance forum
Answered

How to insert data in custom table in respective company using data entity

Posted on by 93
Hi All,

I have extension of EcoResReleasedProductV2Entity.
I have written this code in postLoad eventhandler to insert records in custom table.

But now the issue is its inserted all the companies records of entity into the current running company in the custom table.
Ex:  if i run the data management in USMF company , in entity if i am having inventTable related records in 5 company then it's inserting all the 5company information in USMF entity of my custom table. So in entity i am having records from different legal entity but in my custom table all records inserted in one legal entity.

I used crossCompany, changeCompany keywords but its not working

How to insert data in respective legal entity as of data entity data.
 
 /// <summary>
    ///
    /// </summary>
    /// <param name=/_sender/></param>
    /// <param name=/_eventArgs/></param>
    [DataEventHandler(tableStr(EcoResReleasedProductV2Entity), DataEventType::PostedLoad)]
    public static void EcoResReleasedProductV2Entity_onPostedLoad(Common _sender, DataEventArgs _eventArgs)
    {
        EcoResReleasedProductV2Entity EcoResReleasedProductV2Entity = _sender;
        EcoResReleasedProductV2Entity EcoResReleasedProductV2EntityLoc;
        ITPriceHistory  priceHistory;
        //select crosscompany EcoResReleasedProductV2EntityLoc
        //    where EcoResReleasedProductV2EntityLoc.RecId == EcoResReleasedProductV2Entity.RecId;
        changecompany(EcoResReleasedProductV2EntityLoc.DataAreaId)
        {
        ttsbegin;
        priceHistory.Price = EcoResReleasedProductV2Entity.PurchasePrice;
        priceHistory.ContractPrice = EcoResReleasedProductV2Entity.ContractPrice;
        priceHistory.ItemId = EcoResReleasedProductV2Entity.ItemNumber;
          //  priceHistory.DataAreaId = EcoResReleasedProductV2Entity.DataAreaId;
        priceHistory.insert();
        ttscommit;
        }
    }
    
Thanks in adnvance.
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 229,147 Most Valuable Professional on at
    How to insert data in custom table in respective company using data entity
    Is it limited to data import? Can you confirm that no change should be logged in ITPriceHistory if price changes inside F&O, such as by a user or in code? It would sound strange.
     
    Even if you say that it can't be changed by users and you don't have any code changing the value, it would make sense to do it on table level rather than on a particular way of changing the data. Also, even if there is no code changing the data, someone may add such code tomorrow, therefore depending on it isn't the best approach.
     
    If you insist on doing it in the entity, postLoad() is a bad choice, because that's a method called when reading the data, not when writing it. You should use something like persistEntity().
  • PJ JohnPaul Profile Picture
    PJ JohnPaul 93 on at
    How to insert data in custom table in respective company using data entity
    Hi @Martin,

    I have created one custom table ( ITPriceHistory) , whenever the data imported through data management for release product entity , in standard entity we are having a field PurchPrice, that field value i need to insert in my custom table.

    So after each import operation i need to insert the imported PurchPrice value in new custom table. So where can i perform this insert operation?
     
    Thanks
  • Martin Dráb Profile Picture
    Martin Dráb 229,147 Most Valuable Professional on at
    How to insert data in custom table in respective company using data entity
    Well, what code you should write and where you should place it depends on what logic you're trying to implement.
     
    Please start by describing the business requirement.
  • PJ JohnPaul Profile Picture
    PJ JohnPaul 93 on at
    How to insert data in custom table in respective company using data entity
    Hi @martin,

    Thanks for your reply, can you please suggest me what will be the better place i can write code to insert some data of standard entity to my custom table.

     
  • Martin Dráb Profile Picture
    Martin Dráb 229,147 Most Valuable Professional on at
    How to insert data in custom table in respective company using data entity
    Writing to another table every time when a record is loaded from database? What purpose does it serve? It sounds like a performance killer creating a plenty of duplicate records (or causing errors), because the same product surely can be exported (or used in code) more than once.
     
    It sounds like a bad idea to me.
  • Suggested answer
    Layan Jwei Profile Picture
    Layan Jwei 7,175 Super User 2024 Season 2 on at
    How to insert data in custom table in respective company using data entity
    Hi PJ_John,
     
    Isn't it because you are using EcoResReleasedProductV2EntityLoc in the change company instead of the other variable "EvoResReleasedProductV2Entity" that you filled from sender?
     
    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

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,818 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,147 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans