Monday 11 February 2013

Copy Duplicate Record

Background:
==================================================
May times there comes a situation, when we need to duplicate a record exactly same as the existing one.
Obviously its very tidy job to write code as under
MyTable myTableBuffer1;
MyTable myTableBuffe2r;
select firstonly myTableBuffer1
where myTableBuffer1.AnyField = = anyValue;
ttsbegin;
myTableBuffe2r.Field1 = myTableBuffer1.Field1;
myTableBuffe2r.Field2 = myTableBuffer1.Field2;
myTableBuffe2r.Field3 = myTableBuffer1.Field3;
.
.
myTableBuffe2r.FieldN = myTableBuffer1.FieldN;
myTableBuffe2r.Insert()
ttscommit;
Why its kind of hectic job, because a  table can contain large number of fields. So creating a new record assigning values to fields could be a a headache many times.
==================================================
Solution:
Very simple, provided by Dynamics AX/ X++ . The solution is as under with description and how to code.

.data method[dot data method]
.data method copies a record and overwrites the system fields with new values. here system fields. Apart from system fileds, it copies the data of all fields in new record, exactly. Thus it duplicates the Record. Here is the code sample.

CustTable custTable1;
CustTable custTable2;
; custTable1 = CustTable::find(’1104′);
ttsbegin;
custTable2.data(custTable1);
custTable2.AccountNum = ’1105′;
custTable2.PartyId = ”;
custTable2.PartyId = DirParty::createPartyFromCommon(custTable2).PartyId;
if (!custTable2.validateWrite())
{
throw Exception::Error;
}
custTable2.insert();
ttscommit;




 

No comments:

Post a Comment