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();
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.
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.
Patrik, again, thanks for all!
Regards
[]s
Pichler
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.
In fact, I was using this method while doing data migrations. Then you should sometime bypass all business logic...
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
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.
Post a Comment