2012年10月29日星期一
How to achieved to copy data from origanl table instance to new table instance
One of the useful method from standard AX is the buf2Buf(), it behave similar to table's .data() method with one difference - buf2buf doesn't copy system field. Another reason of using buf2buf is copying of record from one company to another company. When using changeCompany(), the .data() method copy all field including DataAreaId and when insert(), DataAreaId field does not change, hence, the record is not inserted into the company indicated in the changeCompany(), here is where buf2Buf() comes in handy - copy all field except system field, then during insert(), let the system assign values to system field.
Sometimes, there're some functionality requires copy data from one table to another table which has similar structure (Eg. to history or logging table), in this case, the .data() and buf2Buf() cannot be used. But we can make some modification to the buf2Buf() method to copy based on field name instead of field Id.
Below are the two methods:
> global::buf2Buf() - Standard AX method
> THK_buf2BufBySameFileldName() - modified method
//Standard AX method, copy data based on field Id
1)global::buf2Buf
static void buf2Buf(
Common _from,
Common _to
)
{
DictTable dictTable = new DictTable(_from.TableId);
fieldId fieldId = dictTable.fieldNext(0);
while (fieldId && ! isSysId(fieldId))
{
_to.(fieldId) = _from.(fieldId);
fieldId = dictTable.fieldNext(fieldId);
}
}
2)THK_buf2BufBySameFileldName
//Modified method, copy data from one table to another table with similar structure
// USR Changed on 29 十月 2012 at 11:35:14 by admin
static void THK_buf2BufBySameFileldName(Common _from, Common _to)
{
DictTable dictTableFrom = new DictTable(_from.TableId);
DictTable dictTableTo = new DictTable(_to.TableId);
DictField dictFieldFrom;
FieldId fieldIdFrom = dictTableFrom.fieldNext(0);
FieldId fieldIdTo;
;
while (fieldIdFrom && ! isSysId(fieldIdFrom))
{
dictFieldFrom = new DictField(_from.TableId, fieldIdFrom);
if(dictFieldFrom)
{
fieldIdTo = dictTableTo.fieldName2Id(dictFieldFrom.name());
if(fieldIdTo)
_to.(fieldIdTo) = _from.(fieldIdFrom);
}
fieldIdFrom = dictTableFrom.fieldNext(fieldIdFrom);
}
}
订阅:
博文评论 (Atom)
没有评论:
发表评论