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. 



                                                                                     




  
                         





             







1 comment:

  1. Nice post you can subscribe Ax ERP Related video from here

    https://www.youtube.com/user/sksingh1980

    ReplyDelete