Sunday, 25 January 2015

Bringing copy company back [AX 2012]

Back in 2013 I wrote a blog post on how to duplicate a company. I basically summarised Microsoft's guide on MSDN. http://dynamicsnavax.blogspot.com.au/2013/07/duplicate-company-in-ax-2012.html
The big issue with those steps are that it is very manual and can scare a lot of people. Imagine having to copy a company set up for 10 plus companies. This can be very tedious and time consuming. Plus there are some references that have to be set up manually like the journal names voucher series. So, why not automate.
I have put my project on codeplex which you can download and use (READ here carefully).

User guide

  1. Download the class from https://dax2012copycompany.codeplex.com/ under source codes tab
  2. Import it into your AOT
  3. Run the class NAVAX_CopyCompany – Right click Open.
image
  • From company – Enter the from company id
  • To company – Enter the new company id (if company doesn’t exit it will create it)
  • Use record ID compression – RecIds are recreated and in sequence
  • Release products – To release the products into the new company
  • Copy worker employment – To copy the employment record to the new company.
You will get a progress bar. It should take about 5-10 minutes on contoso demo data.
2014-09-26_0815
If there are issues importing certain tables it will give you a warning. I would advice to fix these manually.
2015-01-22_0918 

How it works

This works by exporting the dat/def to a temp folder and then creating it in the new company.
  1. Creates the Legal entity
  2. Copy the ledger set up – this includes the system accounts and account structures
  3. Copy the company/legal entity specific number sequence – resets the next number to 1. This step does not copy shared number sequence.
  4. Exports the dat/def for all tables that have the following conditions( Method is copyThisTable()).
    • Table group = Group, Reference, Parameter, Miscellaneous, Framework
    • Excludes tables that are virtual tables, system tables, temp tables.
    • I have manually added these tables PrintMgmtDocInstance and PrintMgmtSettings to include some print management tables. To add more tables that may not have the correct property, just add it to this method copyThisTableMore
    • image
  5. Imports the dat/def into the new created company and deletes the temp file
  6. Updates the voucher number sequence on the journal names – Ledger journal name, production journal name, inventory journal name. As that is company specific.
  7. Release products into the new company if the flag is ticked
  8. Copy worker employment to the new company if the flag is ticked
Now you should be able to navigate to your new company. Go to the legal entity and enter the company information (name, address etc).
You can now use Data Import Export Framework to import your customers, vendors and all your master records.

Limitations

  • This is class is a duplicate company and works with in the same partition.
  • It expects the account structure used in the from and to company are the same. If they are different I would say don’t use it. Just do the company set up manually or use other import methods.
  • There maybe other tables that need manual fixing or some coding. For example, I found the Print management setups where not set up. So, I added them to the exception tables list.
REMEMBER this is given as is. No guarantee it will work perfectly for you. IMHO Microsoft has not developed a similar copy company intentionally (similar to AX2009 copy company). As it isn't a perfect solution for AX2012. It has a lot of limitations, it would have given false impressions and negative feedback. DIXF is the future of most of Microsoft’s efforts.
CREDIT - I have taken inspiration from AX2009 copy company class. It does very similar thing of using temp dat/def.
FEEDBACK is appreciated. If you use it in any way, please give me feedback/comments/enhancements.

17 comments:

Joris de Gruyter said...

How do you deal with RecId links that are all over AX now? That is one of the big reasons why the traditional copy company was removed from AX 2012.
For example, released products link to their dimension groups (storage, tracking, etc) on the Item's RecId. Have you verified that for example these items, after a copy, still point to the same dimension groups?

Munib Ahmed said...

Hi Joris,

As far as the Release products and the worker employment - I create those through code and not via the dat/def.

The dat/def that I do is really just automating this article.
https://technet.microsoft.com/en-us/library/hh575244.aspx

ps.
I should make my warning stand out a little more. You got me worried that people will think this is a production solution.

André Arnaud de Calavon said...

Hi Munib,

I'm going to have a look soon. Thanks for sharing.
How does this tool deal with Ledger related tables? Does it copy a chart of accounts? In this area there are many more concerns.
Does this tool also take advantage of the batch framework to use multiple threads?

Munib Ahmed said...

Hi Andre,

I wrote some methods to create the ledger table (copy from the fromCompany). I dont do this by the dat/def method. It assumes the same account structure is used and same system accounts. I wrote a number of methods to deal with those complex things. Then I do the dat/def for the set up tables.

My thinking was this can be extended or done differently. For example maybe use a wizard to have some predefined tables. eg. Copy ledger name, copy number sequence etc.
Its a starting point.


I haven't done any threading or anything.

Florent Pousserot said...

Hi Munib,

In your method copyNumberSequence, i suggest you this patch :

//toNumberSequenceTable.NextRec = 1;
toNumberSequenceTable.NextRec = toNumberSequenceTable.Lowest;

I obtain many errors on this method. (segment and reference) I work on this for obtain more stable results.

Thanks for your sharing !

Jim said...

Hi Munib

This is a great start, thank you very much for sharing it.

I've made a number of improvements, including:

1) Updating the number sequence reference correctly. We now duplicate EVERY company-specific sequence, then update the parameter tables to point to the new sequences.

2) Update number sequences that have the company in them. e.g. if copying from ABC1 to ABC2, and the NS has ABC1 in the format, then we update that to ABC2.

3) Updateing voucher series on LedgerJournalName (and Invent/Prod /Proj). Your code tries to do it but has bugs

4) Import/Export of Workflow.

Are you interested in receiving copies of our code, for sharing? It's not bug-free yet....


Jim

Munib Ahmed said...

Yes. Please do share Jim.
I can update to code.

Munib Ahmed said...

I shall fix that today.
Thanks for sharing.

Jim Willsher said...

Sorry, completely the wrong class! That was one I was working on during testing. oops!

How do I share? No way to attach files here.

Munib Ahmed said...
This comment has been removed by the author.
janak talekar said...

Hi Munib,

I have imported your class in my enviromnent. The class is working fine when I run from the AX client. But when I put this class in Btach Job then it won't copy the data.

I have resolved the runtime Type casting error also. But in batch job the class not able to copy the data. Any specific modification required to run the same class in Batch Job.

Regards,
Janak

janak talekar said...

Hi Munib,

I have debug the code and found that there are some casting issue of SysDictTable and DictTable class in "\Classes\SysDataImport\importData".
It gives an error while assigning the value from one parent class (DictTable) to child class (SysDictTable) variable.

dictTable = this.setupTableImport(tableId);

Kindly give me some suggestion for casting the class.

This code is working fine in AX 2012 but when you put this class in Batch then it will gives you type cast error in CLR.

Kindly share some suggestion for the same.

Thanks in advance.

Regards,
Janak

Munib Ahmed said...

Hi Janak,
I haven't ran it in batch. Been a little busy lately.
Thanks for the feedback. I will try to have a look next time.

Tommi said...

Hi Munib,

Thanks for sharing this!

Do you have had time to update code based on Jim's code. I would appreciate a lot if I could get updated code.

Thanks and Regards, Tommi

Munib Ahmed said...

Hi all,

All the code I have received has been included.

Munib Ahmed said...

Updated some typos that no one told me about.
Guess people made sense of what I was trying to say.

Michael Hattar said...

Hi all,
I am still having an issue with the numbersequence creation process. The company element ( in the company specific sequences) in the copied-to company stays the same as the copied-from company number. does anyone have an idea?
Thanks in advance.