My 2p about ERP Solutions, Information Worker Solutions and other software products (mainly Microsoft Dynamics AX and Microsoft SharePoint).

05 April 2010

X++ Code Snippet: Copying records

by Patrik Luca 7 comments

Tag



This X++ Code Snippet post describes how you can easily copy records by running over all of the fields of the from-record. During this looping over all the fields, you can exclude some fields if needed.

X++ Code Snippet: Copying records

Create following method in a class:

static void CopyFields(Common _from, Common _to) 
{
    DictTable           t;
    DictField           f;
    int                 i;
    fieldId             id;
    FieldName           fieldname;
    ;

    t = new DictTable(_to.TableId);
    if (t)
    {
        for (i=1; i<=t.fieldCnt(); i++)
        {
            id = t.fieldCnt2Id(i);
            f  = new DictField(_to.TableId,t.fieldCnt2Id(i));
// excluding system fields (or add other fields to exclude during
// the copy action
            if (f && !f.isSystem())
            {
                _to.(id) = _from.(id);
            }
        }
    }
}


To copy for example a CustTrans to a new one, just call your class like this:



YourClass::CopyFields(custTrans, custTransTo);
custTransTo.insert();

Comments 7 comments
Ricardo Pichler said...

Hello Patrik, thanks for you post!!!

But I have a doubt, I would like to know when it make necessary instead of use:

table2.data(table1);
table2.insert();

Because, if you has the same structure, I don't understand why should I use the reflections.

Obs.: Maybe it's just to show de possibilities, and if this is the case, please, forget about.

Again, thanks for the post.

Patrik Luca said...

Hi Ricardo,

indeed, it is just to show the possibilities. With my example you can for example exclude some fields which you don't want to copy. In my post I exclude for example the system fields, but the same could be done for other fields, according to the business requirements.

Ricardo Pichler said...

Patrik, again, thanks for all!

Regards
[]s
Pichler

gl00mie said...

How does it differ from buf2buf() (been in the Global class for ages)? Besides, from my humble experience record copying based on field Id's is evil: such code tends to degrade to something like "copy all fields and then clear those that should not be copied". That's ugly, that's not caught by xrefs, that's against business logic where a table field is not just a variable, not just a piece of RAM or a value in a DB. In a long term initFromTableName() pattern is the only correct way to "copy" records.

Patrik Luca said...

In fact, I was using this method while doing data migrations. Then you should sometime bypass all business logic...

Unknown said...

Hello,

I've read your blog posts and I find them very interesting and helpful.

I have a task to accomplish, but don't know how. I'll explain you what's about and how I'd like to solve it.

I have to migrate selective subset of data from one aos to another (eg. all CustTable rows where name="John").
I thought to create a method on the first aos that gathers the data and send the table container to a web service. The WS would then write into the second aos instance.
I've also thought to code it using reflection to make it more generic and reusable, but since I'm not an expert X++ coder (I'm stronger in C# and Java) I'm stuck with this implementation.

Do you know a better way to solve my problem? Can you give me some ideas?

Thank you in advance and keep posting,
Francesco

Patrik Luca said...

Hi Francesco,

just use the standard available export/import tools from Dynamics AX. Just check the Administration module, section Periodic, subsection Data export/import. You'll find the tools to export your data to a file and you can reimport it into another environment.

Patrik Luca, Ieper, BELGIUM
Feel free to use or spread all of the content on my blog. In return, linking back to my blog would be greatly appreciated. All my posts and articles are provided "AS IS" with no warranties.

Subscribe feeds via e-mail
Subscribe in your preferred RSS reader

Subscribe feeds rss Most Read Entries

Subscribe feeds rss Recent Entries

Categories

Recommended Books


Subscribe feeds rss Recent Comments

This Blog is part of the U Comment I Follow movement in blogosphere. Means the comment field of this blog is made DOFOLLOW. Spam wont be tolerated.

Blog Archive

My Blog List

Followers

Links