Wednesday, January 2, 2013

Dynamics AX 2012: Import Inventory Journal Lines through code


01-02-2012

Import Inventory Journal Lines Through code in Dynamics AX 2012

Problem: My task was to import the journal lines in dynamics AX 2012 through code.

Solved:     There are two ways to import the code office add-ins and X++ code. Here I am going to explain importing journal lines through code. Because there are lot of blogs explaining the office –add in method. This code was given by my colleague Mahendran.

In below you can find the code used to import the journal lines. Here I missed out something; I hope you people can manage those things. Like,
·         X++ code to read data from excel
·         Proper class declarations and variable declarations
I am just going to give the heart of the solution. If you can’t make it up to this please feel free to mail me at hapkarm@outlook.com

    //Getting the essential values from Excel
    itemId                               = cells.item(row, 1).value().bStr();
    inventSiteId                       = cells.item(row, 2).value().bStr();
    inventLocationId               = cells.item(row, 3).value().bStr();
    inventBatchId                    = cells.item(row, 4).value().bStr();
    qty                                    = cells.item(row, 5).value().double();
    costPrice                           = cells.item(row, 6).value().double();
    mainAcc                            = cells.item(row, 7).value().bStr();
    department                        = cells.item(row, 8).value().bStr();
   
    locInventDim.inventBatchId                   = inventBatchId;
    locInventDim.InventSiteId                       = inventSiteId;
    locInventDim.InventLocationId             = inventLocationId;
    journalTrans.clear();
    journalTrans.initFromInventJournalTable(journalTable);
    journalTrans.TransDate                             = systemDateGet();
    journalTrans.ItemId                                    = itemId;

    //Finding out the invent dim id from given value
    journalTrans.InventDimId                        = InventDim::findOrCreate(locInventDim).inventDimId;
    journalTrans.PriceUnit                               = 1;
    journalTrans.Qty                                          = qty;
    journalTrans.CostPrice                              = costPrice;
    journalTrans.CostAmount                        = journalTrans.Qty * journalTrans.CostPrice;
    //Except these below two lines above things are same as like in dynamics AX 2009
    //If you want to insert the dimension value with account
    //In below code I have no dimension with main account so I just put 0, Instead I have two        
    //dimension
    //write a code like “[“MainAccount”, mainAcc, 2, Dinemsion1, dimVal1, Dimension2, dimVal2)
    offsetEntryPattern                                      = ["MainAccount",mainAcc,0];
    journalTrans.LedgerDimension              = AxdDimensionUtil::getLedgerAccountId(offsetEntryPattern);
    //Default dimension Value either you can insert the dimension value here.
    defaultDim                                                     = [1,"Department",department];
    journalTrans.DefaultDimension             =        AxdDimensionUtil::getDimensionAttributeValueSetId(defaultDim);
    journalTrans.insert();

If you need any assistance from above code feel free to put a mail on hapkarm@outlook.com

Thursday, December 20, 2012

Go to main table reference for display fields


12-18-2012
Go to main table reference for Display fields
Problem:   My task was to enable the go to main table reference for display fields (using display methods instead of table fields)
Solved:      In normal table fields EmplId used formref properties to enable the go to main table reference for display fields. There are no such properties. Rather than dynamics provides one method called jumpref. You can override this method in forms to enable this go to main table functionality. Here I given some sample code.
 
                         args = new Args();
                         args.caller(element);
                         args.record(Datasource);
 menuFunction = new MenuFunction(menuitemdisplaystr        (menuitemname),MenuItemType::Display);
             menuFunction.run(args);

Tuesday, May 29, 2012

Batch Job System Date Problem


05-29-2012

Batch job system date problem

Problem: I created the exchange rate update automation (previous blog) for client. But when I check this exchange rate I changed the system date to verify whether the add-on working on month end or not. After i changed the system to original date since the batch job is not working unless the system date changed to the future date.

Solved:    I tried various forums; I didn’t find any solution related to this. At last I found the solution in community dynamics blog . In this article the author explained the whole batch job process in depth. Here I found out the vital point for batch processing. Please read the article before get into details that I am going to explain here.

BatchGlobal -This table responsible for Batch processing and Cleanup the dead tasks. What i am saying here is, it is responsible for changing the job from waiting to executing status vice versa and Executed tasks changed to end status.
                        This table contains the date and time of job when to execute and when to end. You cannot open the table directly, AOS won’t allow it. So you can open it using a job. See the below screen
Batch Infolog

                        When you change the system date, batchGlobal dates changed to future date (last executed job date and time). So the current date jobs cannot be executed the Batch engine wait for the future date to come.
                        Changing the date to current date in batchGlobal table will solve this problem. But you can’t change it in AOS you must go to sqlserver to change the date to current date (time should be 5 hours behind the current time you will understand this if you read the blog), and then wait sometime it won’t affect quickly it take some time.

Thursday, December 1, 2011

Importing Vendor Master in Dynamics AX 2009


01-11-2011

Importing Vendor Master through Excel sheet in Dynamics Ax 2009

Problem: I was asked to import vendor master table. After i created the definition group for vendtable then i import the data from excel sheet but the error occurs “the data already exist”.

Solved:   The problem occurs because of addressBookId. In my excel sheet my addressBookId was empty. I need to fill that field with unique id or run some scripts to satisfy the error.

                I solved this problem by using Number sequence set the number sequence for address book Id. Navigate to Basic>Setup>GlobalAddressBook>Parameters. Click the number sequences tab and select the number sequence for Address Book Id

Thursday, October 27, 2011

Change form background color in dynamics Ax

How to change form background color overall in AX

Problem : I was assigned by a task to change the background color of a form by company 
               so user can differentiate between companies.

Solution
               
                 Class Name : SysSetupFormRun

              Overrides the run method with following code

              public void run()
              {
                            ;
                            super();
                            switch(curext)
                            {
                                    case "AYN":
                                                       this.design().colorScheme(FormColorScheme::RGB);
                                                       this.design().backgroundColor(WinAPI::RGB2int( 204,255,0));  
                                                       break;
                                   case "CAT":
                                                       this.design().colorScheme(FormColorScheme::RGB);
                                                       this.design().backgroundColor(WinAPI::RGB2int (155,255,0));
                                                       break;
                             }
                 }
                 it solved my problem

                    
                 


Friday, October 21, 2011

Get last form values in Dynamics Ax 2009


Last Form values by user in Dynamics AX 2009

Problem : I got one task that i need to store last form values by user specified. In my task need to store a selection in enum values. 

Solution   : I searched for resources to solve this issue. Ax has inbuilt feature to store user-wise and  version wise form information. I described  this feature.
             
                   consider one form it has enum field with 6 possible selection. what i am going to do is storing the last selection of enum field according to user. here follow the steps.
          
              




                  * You need to update the class declaration method in form by below code
                                

                 ABC category;
                 #define.CurrentVersion(1)
                 #localmacro.CurrentList 
                 category,
                 #endmacro
 


                    show status  - declaration of variable. ABC enum field i used, and the variable used to store the selection of user

                    #define.currentVersion(1) -Version stored in the cache by list (how many values added here 1) what if you are going to add one more field on that list. then you need to increase the version by 1 (at your choice but the version must differ numerically like #define.currentVersion(2)) because cache knows version 1 has 1 variables and version 2 has 2 variables. i hope you understand.

                    #localmacro.currentList ...........#endmacro - it describes what are all the variables we are going to store.

                  *  Methods need to be created, written in form methods
                         
                           Public void initParmDefault()
           {
               ;
               category = ABC::None;
           }
                       above method describes the default parameter. what if no versions saved in cache. its for first use.when user first open the form the default value (declared above) display.

                          public container pack()
                {
                    return [#CurrentVersion,#CurrentList];
                }


             public boolean unpack(container packedClass)
                {
                    int version;
                    ;
                    version = RunBase::getVersion(packedClass)
                    if(version)
                       {
                           [version,#CurrentList] = packedClass;
                           return true;
                       }
                       return false;
                }

                      Above two methods used to format (Pack) and extract (unpack) data. The two methods going to called by xSysLastValue class. In our case pack method will give value of a enum field and version to xSysLastValue class then this class save those values by version in cache. unpack method called by xSysLastValue class to extract those values by user and version. below i will explain the xSysLastValue class.
         
                        
        public identifiername lastValueDesignName()
        {
            return element.args().menuItemName();
            // you can use element.args().designName();
            // this method returns the name of caller
        }
        public identifiername lastValueElementName()
        {
            return this.name();
        }
        public UtilElementType lastValueType()
        {
            return UtilElementType::Form;
        }
        public userId lastValueUserId()
        {
            return curuserid();
        }
        public dataAreaId lastValueDataAreaId()
        {
            return curext();
        }
 
                    Above methods combine to ensure the values that needed by xSysLastValue.savelast method.This values are needed to maintain different user store values different objects in different companies.

                      Override close() method by below coding

                      public void close()
             {
               ;
               Category = ComboBox.selection();
               xSysLastValue::saveLast(this);
               super();
             }

                  when the form closes this system method triggered. here the xSysLastValue class send the user selected values to cache.

               Override a run() method by below coding
                      
           public void run()
             {
                ;
                //called before opeing the form 
                super();
                //get the last value stored in cache 
                //here unpack method used
                xSysLastValue::getLast(this);   
                //set the last user selection on the field
                combobox.selection(showStatus);
             }
 
        above method used to set last values in form.
 
 
            Implement the above flow. if you had any doubt comment.