Friday 6 December 2013

how to update the system fields of the table

Usually in the transaction table while data copying from older version to newer version, we require to copy the system fields as well like (ModifiedDateTime, ModifiedBy, CreatedDateTime, CreatedBy). These fields are system generated fields and user cant update the values in these fields In a normal manner. We have to do a bit trick to fool the compiler
Wrong way : Compiler will not allow this and give you the error “The field must be a data element that allows assignment.”
LedgerTrans ledgerTrans;  
 ;  
 ttsbegin;  
 ledgerTrans.modifiedDateTime = DateTimeUtil::utcNow();  
 ledgerTrans.update();  
 ttscommit;  
Correct way: you can make the compiler silent with below modifications
LedgerTrans ledgerTrans;  
 ;  
 ttsbegin;  
 ledgerTrans.overwriteSystemfields(true);  
 ledgerTrans.(fieldNum(LedgerTrans,modifiedDateTime)) = DateTimeUtil::utcNow();  
 ledgerTrans.update();  
 ttscommit;  

2 comments: