Tuesday, April 3, 2012

AXAPTA - Change Company Madness

Happened upon a frustrating little "feature" in Axapta 2009 today.  I'm pulling data from a CSV file into temporary tables in order to do further processing.  As the CSV file has data from multiple companies, I thought I'd use a single routine to modify all my companies data at once.  To do this, I'm using the "changeCompany" command in X++, and obviously I want to clean up the temporary space before I start.


 static void GPJ_Test2(Args _args)  
 {  
   ImportPurchaseOrder importTable;  
   ;  
     changeCompany("company1")  
     {  
       info(curExt());  
       delete_from importTable;  
     }  
     changeCompany("company2")  
     {  
       info(curExt());  
       delete_from importTable;  
     }  
 }  


Looks innocuous enough, right? You'll find that this simply does not work.  The reason - X++ seems to keep a tab on the company you last used the record for.  So, in this example, it will always delete records for "company1" but never for "company2" - because importTable still refers to "company1".  I found doing this works, instead:


 static void GPJ_Test2(Args _args)  
 {  
   ImportPurchaseOrder importTable;  
   ;  
     changeCompany("company1")  
     {  
       info(curExt());  
       importTable.clear();
       delete_from importTable;  
     }  
     changeCompany("company2")  
     {  
       info(curExt());  
       importTable.clear();
       delete_from importTable;  
     }  
 }  


I'm guessing it works, as it clears out that company reference. Bizarre, huh? 


P.S.
Oh, in case you're wondering, the best plan is to use separate variables for each company.  But when we simply substitute this code that's statically assigning the companies with a while loop - that's where things fall down. 


And by the way - if you're doing any querying or updates using this same variable and then deleting - give up now.  Do your deletes in a separate variable, it seems safest.


- Gavin

No comments:

Post a Comment