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.

Friday, 10 May 2013

Financial dimension sets balance update issue

Recently we noticed that the one of our Financial dimension sets balance is not being updated. When we opened the form and looked at the record, "Rebuild balances" was greyed out as shown below


Running the "Update balances" resulted in the following error.


What this error really means is that the rebuild/update process is already running. Apparently the rebuild balance process never finished on its previous run (maybe due to process being killed by someone/the AOS crash or electricity failure etc.). We could not determine why this happened. Folks at Microsoft helped us in  resetting the status and we were able to rebuild the balances. Following job was used.

 static void ResetFinDimState(Args _args)  
 {   
   DimensionHierarchy dimensionHierarchy;  
     
   update_recordSet dimensionHierarchy  
     setting FocusState = DimensionFocusBalanceInitializationState::Initialized  
     where dimensionHierarchy.FocusState == DimensionFocusBalanceInitializationState::InProcess &&  
        dimensionHierarchy.StructureType == DimensionHierarchyType::Focus &&  
        dimensionHierarchy.DeletedVersion == 0;  
 }  

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

Sunday, 5 May 2013

SQL script to rebuild all indexes for a table

Ideally I would recommend to use Ola Hallengren's SQL Maintenance Solution but in some cases you may have to rebuild indexes on individual tables manually. Following script will allow you to do that. Just replace <TableName> with the actual table name and if you want to build the index offline (might cause locks) set @Build_Online to 0.

 DECLARE @IndexName VARCHAR (100);  
 DECLARE @TableName VARCHAR (100);  
 DECLARE @IndexId int;  
 DECLARE @Build_Online int;  
   
 SET @TableName = '<TableName>';  
 SET @Build_Online = 1;  
   
 DECLARE indexes_cursor CURSOR FOR SELECT  
      si.index_id,  
      si.name  
 FROM sys.objects so  
 INNER JOIN sys.indexes si  
      ON so.object_id = si.object_id  
 WHERE so.name = @TableName;  
   
 OPEN indexes_cursor;  
   
 FETCH NEXT FROM indexes_cursor  
 INTO @IndexId, @IndexName;  
   
 WHILE @@FETCH_STATUS = 0  
 BEGIN  
   
      PRINT 'Defragmenting ' + @TableName + ' --> ' + @IndexName  
   
      IF @Build_Online = 1  
      BEGIN  
           EXEC ('ALTER INDEX ' + @IndexName + ' ON ' + @TableName + ' REBUILD WITH (ONLINE = ON)')  
      END  
      ELSE  
      BEGIN  
           EXEC ('ALTER INDEX ' + @IndexName + ' ON ' + @TableName + ' REBUILD')  
      END  
        
      SELECT  
           *  
      FROM sys.dm_db_index_physical_stats  
      (  
      DB_ID('AxaptaLive'),  
      OBJECT_ID(@TableName),  
      @IndexId,  
      NULL,  
      NULL  
      );  
   
      FETCH NEXT FROM indexes_cursor  
      INTO @IndexId, @IndexName;  
   
 END  
   
 CLOSE indexes_cursor;  
 DEALLOCATE indexes_cursor;  
 GO  

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

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.