how to update the system fields of the table like ModifiedDateTime ?

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;  

Reflections : Changing properties of AOT objects in code

Few days back i was working to update some properties of dynamics ax objects, objects count was quite high and manually doing that work could be hectic. i decided to use the reflection and i found it real powerful as it has lots of area where needs playing.

My scenario was simple where i need to find objects on my own criteria and then update the properties for those objects.

like i have to find out all those classes which starts with ‘XXX’

static void FindClassStartsWithXXX(Args _args)
 {
   UtilIdElements utilId;
 ;
   while select utilId where utilid.recordType == UtilElementType::Class
   && utilId.name like 'XXX*'
   {
     print utilId.name;
   }
   pause;
 }

i also have to update properties of selected objects, like here i am updating the name property of the class.

static void RenameXXXObjectsToYYY(Args _args)
 {
   TreeNode  node = TreeNode::findNode(@'\Classes\XXXClass');
   str oldName;
   str newName;
   #Properties
   ;
   oldName = node.AOTgetProperty(#PropertyName);
   newName = strdel(oldName,1,3);
   node.AOTsetProperty(#PropertyName, "YYY"+newName);
   node.AOTsave();
   node.treeNodeRelease();
   node = null;
 }

There are a couple of things to explain here. First of all the macro #Properties contains the names of all properties you can read and write. These are the same as what you get in the property window. Use the macro instead of literal strings, it’s safer and doesn’t violate best practices.

Next you can see there is a set of methods to get and set properties. After modifying anything you need to save the changes, just like you would when changing an AOT object in the property window. TreeNode works just like doing it manually. You can do a lot more fun stuff with the TreeNode API.

Also, if you have questions or ideas for articles don’t hesitate to let me know .

Design Pattern used in Dynamics Ax

I have found the good resource of design patters used in Microsoft Dynamics Ax.

Design Pattern Dynamics ax

Executing SQL directly from X++

The X++ language supports a number of ways to execute native SQL against any SQL data source. Below are two samples for retrieving data as well as manipulating data. Please be aware, that SQL can be really powerful, and it should be used as such.

Example #1: Retrieve data:

Since this example uses a Connecion class, data is retrieved from the database where Axapta is currently connected.

void Sample_1(void)

{
Connection Con = new Connection();
Statement Stmt = Con.createStatement();
ResultSet R =Stmt.executeQuery(‘SELECT VALUE FROM SQLSYSTEMVARIABLES’);

while ( R.next() )
{
print R.getString(1);
}
}

Example #2: Manipulating data (deletion):

void Sample_2(void)
{
str sql;
Connection conn;
SqlStatementExecutePermission permission;
;

sql = ‘delete from custTable’;
permission = new SqlStatementExecutePermission(sql);
conn = new Connection();
permission = new SqlStatementExecutePermission(sql);
permission.assert();
conn.createStatement().executeUpdate(sql);
// the permissions needs to be reverted back to original condition.
CodeAccessPermission::revertAssert();
}

Happing Daxing J Enjoy the power of SQL using x++

Follow

Get every new post delivered to your Inbox.

Join 123 other followers

%d bloggers like this: