Copy records between companies

martinjordtmartinjordt Member Posts: 18
Hi there,

I need to do some code that will maintain some records between Companies in NAV. This should be quite straight forward, I thought, but I'm seeing some strange behavior in NAV...

When I execute the shown code in Company 2 for the first time everything Works just fine. The record from Company 1 is created in Company 2.
When I execute the code for the second time the record already exist in Company 2 which means that the record should now be modified. As soon as I hit
the modify trigger I get the error "An other user has modified the record...".

61ja86vczoah.png

oebcif05q713.png

I just can't get my head around why this is?! If I assign the fields one by one instead of using transferfields or just copying record var 1 into record var 2 the modify works just fine.

Please educate me someone :-)

/Martin

Comments

  • johnroberts17johnroberts17 Member Posts: 1
    edited 2017-07-14
    :)
  • piktazpiktaz Member Posts: 9
    It's a bug. You should report it to Microsoft.

    As for now, instead of modifying, you can delete the record and re-insert it.

    Also, this behaviour doesn't occur when records are pushed to other company (get from current company and insert/modify into other).
  • martinjordtmartinjordt Member Posts: 18
    Hi all,
    I just found a solution to my problem on another forum.
    Copying from one rec directly to another still does not work, but if you use transferfields with the false parameter it actually works since the primary key fields are not touched.
    Hmmmm....
    /M
  • NavNabNavNab Member Posts: 181
    Hello @martinjordt

    You're using CHANGECOMPANY the wrong way. It should be used for READ only (no INSERT, no DELETE, no MODIFY, no RENAME). The solution you found (i.e. INSERT(FALSE)) is a bad habit unless you're sure 100% of what you're doing.

    Why? Here is some examples when it will give you some headaches (especially during upgrades):

    1- if you use dimensions on your Customer on company Satelit1 then you will force company Satelit2 to accept values that you're not sure they are available on Dimension Values of Satelit2,
    2- You do not use standard data validation (Field.VALIDATE). This will also cause orphaned data. Hopefully for table 18 you will not notice the impact. But if you decide to do the same for table 27 then you'll create some serious issues in your database.

    To sum up, the way you're doing it, you will create some beautiful orphaned data :smile:

    In order to achieve what you want properly, you have two (or three) options:
    1- simulate all table triggers => This is the hard way and I don't recommand it,
    2- use web services to copy data between your companies,
    3- use background session to copy data between your companies.

    P.S: options 2 and 3 are practically the same because you use another session that will properly use all standard triggers.
  • martinjordtmartinjordt Member Posts: 18
    Hi Nabil,
    That's exactly what I'm doing. I'm changing Company to read the data, and I'm inserting into the current Company, so that I can utilize the business logic on triggers :-)
    The example shown is just that - an example. I would never create records without using the business logic ;-)
    Breg
    Martin
  • NavNabNavNab Member Posts: 181
    Okay, glad to hear that. But, you'll need (maybe) to secure your code a little bit. You'll need to be sure your code always runs from company Satelit2 (any other company than Satelit1, otherwise the code has no meaning => Or at least I don't understand it :) ).

    Have a good day :)
Sign In or Register to comment.