Monday, February 17, 2014

AX Tables – Update field values without affecting system fields



In AX if we update any field values in table means it will also change the system fields like Modified DateTime, Modified by.
We also can able to update those table fields without changing those system fields or we can change as per our requirement.

Note: It will update the system fields but this blog is not recommended to update the system fields. Because it will lead to date inconsistency. It may affect few business logic's those written based on the system fields.


In our example i took the Modified date time field.


1. Updating table without modifying system fields :


The following steps will update the sales name field in the “Sales Table”

Steps :

1. Create a new class and set the class property “Run on" as "Server”
2. Create a new method and write the below code in that method
3. Call that method from the place (e.g Forms) you want to update the table.
4. In my example i wrote the method called “SystemFieldsOverwrite” in class name “TestClass” to update the sales name of the particular sales id.


Code: (Class -> TestClass->SystemFieldsOverwrite)


Public str systemFieldsOverwrite(SalesId _salesId,SalesName _salesName)
{
   SalesTable salesTable;
   str Result;
   ;
   salesTable = SalesTable::find(_salesId,true);
   Result += "Before Update the Sales Order\n";
   Result += " Sales ID : " + _salesId+"\n";
   Result += " Sales Name : " + salesTable.SalesName+"\n";
   Result += " Modified DateTime : " + strfmt("%1",salesTable.modifiedDateTime)+"\n";
   ttsbegin;
            new OverwriteSystemfieldsPermission().assert();
           salesTable.overwriteSystemfields(true);
           salesTable.SalesName = _salesName;
           salesTable.(fieldnum(SalesTable,modifiedDateTime)) = salesTable.modifiedDateTime;
           salesTable.doUpdate();
           salesTable.overwriteSystemfields(false);
           CodeAccessPermission::revertAssert();
  ttscommit;
  Result += "After Update the Sales Order\n";
  Result += " Sales ID : " + _salesId+"\n";
  Result += " Sales Name : " + salesTable.SalesName+"\n";
  Result += " Modified DateTime : " + strfmt("%1",salesTable.modifiedDateTime);
  return Result;
}

5. I am calling this particular method from the Job called “SystemFieldTest”.


Code: (Job->SystemFieldTest)


static void SystemFields_Test(Args _args)
{
     TestClass testclass = new TestClass();
     SalesId salesId = "SO-101398";
     SalesName salesname = "Testing";
     str Result;
     ; 
     Result = testclass.SystemFieldsOverwrite(salesId,salesname);
     info(Result);
}

Output:

Here the Sales Name is updated but the Modified date time remains the same.

2. Updating the Modified DateTime as per our requirement.


Instead of passing the existing modified date time in the “systemFieldOverwrite” method we can pass our own date time.

Note: It will update the system fields but this is not recommended to update the system fields. Because it will lead to date inconsistency. It may affect few business logic's those written based on the system fields.

Ex :

salesTable.(fieldnum(SalesTable,modifiedDateTime)) = str2datetime("2010/04/03 11:00:00",321);


Output:

Note : We can able to edit or change "Created DateTime and Created by " fields only at the time of insertion (salesTable.insert()) of a new record.



Thanks for read this blog.


Tuesday, February 4, 2014

Dynamics AX User Roles Retrieval

User Roles retrieval:

 To retrieve the roles of a particular user based on username in AX.

  Tables:
             The user and their roles are relate with three different tables. All three tables are system tables and those tables are listed below,
                             1. SecurityUserRole
                                    2. SecurityRole
                                    3. UserInfo          
            
 Relation:
            The relations between those tables are,
                         1. Userinfo.Networkalias == username
                         2. SecurityUserRole.user == userinfo.id
                         3. SecurityRole.recid == SecurityUserRole.securityrole


The below job is used to List the roles of a particular user by Passing the user name like a parameter.

Code :
       static void retriveUserRoles(Args _args)
      {
          //Tables
         SecurityUserRole    _secUserRole;
         SecurityRole           _secRole;
         UserInfo                 _userInfo;

         //Parameter
         NetworkAlias         _userName = "Admin";
    
         //List to store the roles 
         List userroles = new List(Types::String);

         select  _userInfo where _userInfo.networkAlias==_userName;
        
         while select  _secUserRole where _secUserRole.User==_userInfo.id
        {
           while select  _secRole where _secRole.RecId == _secUserRole.SecurityRole
            {
                  userroles.addEnd(_secRole.Name); //we can also add more role details in List
            }
        }
        setPrefix("The roles assigned for user : " + _userName);   
        info(userroles.toString());   
    }

Output :

       


Thanks for read this blog.