Showing posts with label SuperType. Show all posts
Showing posts with label SuperType. Show all posts

Saturday, 11 May 2013

Security issue for tables with inheritance

One of my colleague is working on a project where they require to integrate using WCF service. He came across an issue where they were not getting the Product name. The issue was that on a restart of the AOS their WCF call would not return the product name.  However after someone accesses the product from within AX, the WCF would return the name.

They asked for my assistance in getting the issue resolved. The symptoms that they described were hinting that it is a security related issue. The fact that product name was returned once someone accesses the product from within AX clearly showed that the AOS is caching the item name. So after cache was filled in the WCF call will get the name from the cache instead of getting it directly from the table. Usually AX will throw an error if the user did not have access to the table. But in this case there was no error.

They were using InventTable.itemName() method to return the product name. We looked at the security role/duties/privileges developed by them and all looked fine. We then decided to do things one step at a time to find out the exact place where things stopped working. We looked at the call stack of method itemName and started replacing the method with the code inside the method.

Eventually we found the code that was causing the issue. The code is in InventTable.productName.

   return EcoResProduct::find(productRecId).productName(_languageId);  

When we replaced this call with the code inside the EcoResProduct.productName it started working. The developer did gave access of EcoResProduct to the role/duty. So it was really confusing why this was not working. Then I realised that this is a SuperType/SubType table and EcoResProduct is not the only table that requires access. Type hierarchy browser indicated that we need access to two more tables. Once the access was granted product name was accessible even if was not cached.

The question to ask is "Why AX does not throw error in tables involving hierarchy when the user does not have access".

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.