// Copyright (C), 2015, CEU Co., Ltd. // USR Changed by 2015-01-05T15:22:36 Fandy Xie 谢宇帆 // Version : 1.0 // Description : 更改系统日期时间 // Return : static void changeSystemdatetime(Args _args) { /* Since it's a table column controlled by the system, you can't update it. The same goes to the other system columns. The compiler won't even let you write code like the following: */ myTable.CreatedDateTime = DateTimeUtil::utcNow(); /* It'll spit out the error "The field must be a data element that allows assignment.". However, there is a workaround to be done. The xRecord.overwriteSystemFields helps us with it. Its name is pretty straightforward. We should also grant the required permission to our code, by using the OverwriteSystemFieldsPermission permission class. Here's how we should use it: */ MyTableBuffer myTableBuffer; ; myTableBuffer = myTableBuffer::find(); new OverwriteSystemfieldsPermission().assert(); myTableBuffer.overwriteSystemfields(true); myTableBuffer.(fieldNum(MyTableBuffer, CreatedDateTime)) = DateTimeUtil::utcNow(); myTableBuffer.insert() CodeAccessPermission::revertAssert(); /* There are two things to keep in mind: 1.this trick only works for insert operations. It will not work for update operations. 2.the OverwriteSystemFieldsPermission will only work for code running on the server. To make your code run on the server, add the "server" keyworkd to its name, like this: */ server public static void foo() /* Just remember that the server keyword only works for static methods. In MSDN's words: Establishes the location where the method is to be executed (on the server). Can only be used on static methods. If the method is not static, you need to specify the location using the class property RunOn. */ }