Friday, 26 April 2013

Error when debugging Services class method from X++

Recently one of our developer came to me with an error that he was getting while trying to debug a class method that was used as a service. The error that he was getting was

'unchecked' cannot be called on the client.

The code for the service class method was something like below.

 [SysEntryPointAttribute(true)]  
 public ProductsDataContract getItemById(ItemId _itemId)  
 {  
   this.buildInventTableQuery();  
     
   qbdsInventTable.addRange(fieldNum(InventTable,ItemId)).value(_itemId);  
   qrInventTable = new QueryRun(queryInventTable);  
     
   while (qrInventTable.next())  
   {  
     this.fillProductData(qrInventTable);  
   }  
   
   return productsDataContract;  
 }  

There is nothing unusual about it. As expected it have the SysEntryPointAttribute(true) declared at the beginning of the method. As soon as he started to debug this method from an X++ job, AX will throw the above mentioned error on the first line of the method. However if he comment out the attribute line, he could debug it with no error.

This was bit unusual as I never came across to this type of error before. With a little investigation I found that the service class was not marked to run on Server instead the RunOn property was left to default "Called from" (which would work fine for a WCF call as the call always goes to AOS but will not work from an X++ call from a job). For services it is a must that the service class should be marked to run on Server as shown below.



This posting is provided "AS IS" with no warranties. Use code at your own risk.

Saturday, 16 March 2013

Purchase order invoice error after applying Feature Pack

Last month we applied the Feature pack to our Dynamics AX 2012 environment. While testing we found out that when a user clicks on the invoice button on any purchase order form an error is thrown. The error appers only when a normal user was clicking on the invoice button. For an admin there was no error. The error is shown below.


It shows that SQL is incomplete. There is a field missing before the IN keyworld. As there are multiple joins involved and also temp tables are present in the query there was no clear indication of which filed from which table was missing before the IN keyword..

So the first thing we did was to do a trace for the user and admin of the whole process to see whether the same code path is being used and what SQL is being generated. Below is the trace


On the left is user trace and on the right is admin trace. we can see in the call stack that once the incomplete SQL is generated, system throws an error and call stack is completely different from the admin one. The highlighted parts on the left indicate the difference in the sql statement being generated.

As the security framework for normal users kick in and generate a different sql we thought it was security related issue. So we gave full permission of all the tables involved to the user, but the issue remained.

The next step was to look at the table structure and contents of the temp tables. This is how it appeared in the SSMS.


As you can see that there is a field called INSTANCERELATIONTYPE. This field is used in tables that have supertype/subtype feature of AX implemented. Other fields indicated that the temp table was related to AgreementHeader table. This table has two sub type tables PurchAgreementHeade and SalesAgreementHeader. The assumption that these are the tables related to the temp table was confirmed by the ids of these table (4902, 4903) as shown by the user trace. All the tables have configuration key "TradeBlanketOrder".

In feature pack, field PurchAgreementHeader_PSN was added to VendInvoiceInfoTable. This field has the configuration key of PublicSector. Now when feature pack was applied this key was turned on by default and in our system the key TradeBlanketOrder is off. This turned out to be the root cause of the problem.

When a form is run, system joins all the tables specified in the relation or that are supertype/subtype tables to the query and as the configuration key for the TradeBlanketOrder was off, system could not access the PurchAgreementHeader table hence generating the incomplete SQL statement.

To fix the issue we could think of three possible solutions
  • Disable the public sector configuration key.
  • Enable the TradeBlanketOrder key.
  • Change the configuration key on VendInvoiceInfoTable.PurchAgreementHeader_PSN to TradeBlanketOrder (as the field should only be used when this key is checked).
All three would fix the issue but we went for the second option.

Note: It is strange how AX silently omitted the field from the SQL statement insted of throwing the error. This should be done by AX instead of relying on the SQL server to do this job.

This posting is provided "AS IS" with no warranties. Use code at your own risk.

Saturday, 2 March 2013

Assign roles from one user to another

Sometimes while testing the security roles we need to have exactly the same roles a user have. The following piece of code will assign the security roles of a user to another (while keeping the existing roles intact).

static void CopyRolesFromUserToAnotherUser(Args _args)  
 {  
   SecurityRole      securityRole;  
   SecurityUserRole  securityUserRoleSource;  
   SecurityUserRole  securityUserRoleDestination;  
   SecurityUserRole  securityUserRoleDestinationNew;  
   boolean           added;  
   UserId            sourceUserId = 'User 1';  
   UserId            destinationUserId = 'User 2';  
   
   str getFormattedRoleName(str _roleName)  
   {  
       return SysLabel::labelId2String2(_roleName, CompanyInfo::languageId());  
   }  
   
   while select securityUserRoleSource  
     where securityUserRoleSource.User == sourceUserId  
   join securityRole  
     where securityUserRoleSource.SecurityRole == securityRole.RecId  
   notexists join securityUserRoleDestination  
     where securityUserRoleDestination.SecurityRole == securityRole.RecId  
       && securityUserRoleDestination.User == destinationUserId  
   {  
       info(strFmt('Adding role %1 to the user %2.', getFormattedRoleName(securityRole.Name), destinationUserId));  
   
       securityUserRoleDestinationNew.clear();  
       securityUserRoleDestinationNew.User = destinationUserId;  
       securityUserRoleDestinationNew.SecurityRole = securityRole.RecId;  
       securityUserRoleDestinationNew.AssignmentMode = RoleAssignmentMode::Manual;  
       securityUserRoleDestinationNew.AssignmentStatus = RoleAssignmentStatus::Enabled;  
   
       SecuritySegregationOfDuties::assignUserToRole(securityUserRoleDestinationNew, null);  
   }  
 }  

This posting is provided "AS IS" with no warranties. Use code at your own risk.

Wednesday, 27 February 2013

Find out if the user is an administrator

Today I wanted to find out if the logged in user in Dynamics AX 2012 is an administrator so I did a bit of search and found out the solution.

SecurityRights secRights = SecurityRights::construct();

if(secRights.isSystemAdministrator())
{
    info('User is an admin');
}


This posting is provided "AS IS" with no warranties. Use code at your own risk.

Saturday, 23 February 2013

Dimension framework in AX2012

Bill Frandsen from Microsoft Dynamics AX GFM team has published a series of posts on Dimensions in AX2012. It consist of 7 posts. I would strongly recommending going through these posts. The posts cover different areas of Dimension framework with showing how to set-up things in AX and then showing how it is saved in the database.

Ledger account combinations - Part 1 (Dimensions)
Ledger account combinations - Part 2 (Dimension enumerations, default dimensions)
Ledger account combinations - Part 3 (Structures and constraints)
Ledger account combinations - Part 4 (Advanced rules)
Ledger account combinations - Part 5 (Ledger dimensions (A))
Ledger account combinations - Part 6 (Ledger dimensions (B))
Ledger account combinations - Part 7 (Advanced topics)

This posting is provided "AS IS" with no warranties. Use code at your own risk.