Showing posts with label ax2009. Show all posts
Showing posts with label ax2009. Show all posts

Sunday, February 5, 2012

Edit Methods versus Display Methods

Display Methods:

In some scenarios, we need to display some values derived from other columns and those are not associated directly with the database, like Amount fields (Unit*Price). In that case there are display methods that perform that functionality.
 display Amount amount()
{
    AmountMST  amount; 

    amount = this.unitprice * this.quantity; 

    return amount;
} 

Edit Methods:
In AX 2009, when reference controls were not available, in table if there is a relation created on the basis of recId i.e. there is a child table and it contains the record Id of the parent Table. When that child table binds to form, (to display and select the user friendly information from the parent table, lookup controls were used). The record Id of the user friendly value is saved on the table with the help of the edit methods.
To give an example of an edit method, we will create a new field in the CarTable to hold the mileage of the car and have an edit method in RentalTable that enables the users to be in the RentalTable form and still edit the field in CarTable.

We'll create an extended data type of type integer for the new field and call it Mileage. Then we'll dd the field to the CarTable.

The edit method in RentalTable will then look like this: 

//This material is copyright and is licensed for the sole use by ALESSANDRO CAROLLO on 18th December, Chapter 4 [ 105 ]

edit Mileage mileage(boolean _set, Mileage value)
{
    CarTable carTable;
    Mileage  ret;

    // find the car records from the car table with update = true
    carTable = CarTable::find(this.CarId, _set);

    if (_set)
    {
        ttsbegin; 

        carTable.Mileage = value;
        carTable.update();

        ttscommit;
    }
    else
    {
        ret = carTable.Mileage;
    }

    return ret;
} 



Saturday, February 4, 2012

How to use the regular expression to validate the name in X++ AX

How to use the regular expression to validate the name

public bool validateName(str _name)
{

    System.Text.RegularExpressions.Match regExMatch;
    bool                                 isValid;

    // verify that Name doesn’t contain bad special character like <>:”/\|?*
    // other characters used in the regular expression are part of regex syntax. 

    regExMatch = System.Text.RegularExpressions.Regex::Match(_name, @’^[^<>:"/\\|?*]*$);
   
    // return true if name matches the criteria otherwise return false
    isValid = regExMatch.get_Success();   

    return isValid;
}

Sunday, January 29, 2012

Upgrade from AX 2009 to 2012

Question: Is there any guide line to upgrade the dynamics Ax 2009 to 2012

Ans: Microsoft written a document that provided the standard steps to upgrade AX 2009 to Ax 2012

That includes the following topics 
  •  What's New: Upgrade
  •  Supported upgrade paths
  •  Hardware and software requirements
  •  Best practices for upgrade
The link shared by Ahmed El-Sayed

Thursday, January 26, 2012

lookup in AX

In the dynamics ax 2012, there are different ways to fill the combo box/drop down list

How to create a simple lookup

The SysTableLookup class is provided by the standard application to allow programmers to easily create their own lookup forms, in code.
The basic steps to using this class are as follows:
  1. Create the sysTableLookup object
  2. Create the query to select the lookup data
  3. Add  the fields shown on the lookup
  4. Performs the lookup

client static void lookup<TableName> (FormStringControl _ctrl)
{
    SysTableLookup          sysTableLookup       =  SysTableLookup::newParameters(tableNum(<tableName>),_ctrl);
    Query                   query                = new Query();

    // create the query for the lookup
    QueryBuildDataSource    queryBuildDataSource = query.addDataSource(tableNum(<tableName>));

    // Add fields that will be shown in the lookup as columns        
    sysTableLookup.addLookupfield(fieldNum(<tableName>,<FeildName1>));
    sysTableLookup.addLookupfield(fieldNum(<tableName>,<FeildName2>));

    //Add the query to the lookup form
    sysTableLookup.parmQuery(query);

    // Perform the lookup
    sysTableLookup.performFormLookup();
}

This above method of lookup was heavily used in AX 2009, and it also used in the AX 2012 when there isn’t any data source specified in the form (i.e. Dialog Form) and the StringEdit control used for the lookup

How to create a simple lookup Reference
 
 The SysReferenceTableLookup class is used to construct lookup forms for reference controls.
  1. Create the SysReferenceTableLookup object
  2. Create the query which will be used to select the lookup data
  3. Add the fields which will be shown on the lookup
  4. Perform the lookup 
This method is now the standard method used to lookup the data for drop down when there is any modification needed to override the behavior of the functionality provided by the automatic lookup


public static client <tableName> lookup<tableName>(
    FormReferenceControl        _formReferenceControl)
{
    Query                   query;
    SysReferenceTableLookup referenceLookup;

    if (_formReferenceControl == null)
    {
        throw error(Error::missingParameter(null));
    }

    referenceLookup = SysReferenceTableLookup::newParameters(
        tableNum(<tableName>),
        _formReferenceControl,
        true);

    // create the query for the lookup form
     query.addDataSource(tableNum(<tableName>));

    // Add fields that will be shown in the lookup form as columns
    referenceLookup.addLookupfield(fieldNum(<tableName>,<FeildName1>));
    referenceLookup.addLookupfield(fieldNum(<tableName>,<FeildName2>));


    // Add the query to the lookup form
    referenceLookup.parmQuery(query);

    // Perform the lookup and return the selected record
    return referenceLookup.performFormLookup() as <tableName>;
}

 

post inventory journal using code

recently i have written a quick code to to post the invent transfer jounral


/// <summary>
/// Populates the buffer of the <c>InventJournalTable</c> table data.
/// </summary>
/// <returns>
/// Buffer of the <c>InventJournalTable</c> table.
/// </returns>
Public InventJournalTable populateInventJournalTable()
{
    InventJournalTable      journalTable;
    InventJournalTableData  journalTableData;

    journalTable.clear();
    journalTable.JournalNameId  = InventParameters::find().QuickTransferJournalNameId;
    journalTableData            = JournalTableData::newTable(journalTable);
    journalTable.JournalId      = journalTableData.nextJournalId();
    journalTable.Reservation    = ItemReservation::Automatic;
    journalTable.JournalType    = InventJournalType::Transfer;
    journalTableData.initFromJournalName(journalTableData.JournalStatic().findJournalName(journalTable.journalNameId));
    journalTable.Description    = InventDescription.valueStr();
    journalTable.insert();

    return journalTable;
}


/// <summary>
/// Populates the buffer of the <c>InventJournalTrans</c> table data.
/// </summary>
/// <param name="_InventJournalId">
/// <c>JournalId</c> of the <c>InventJournalTable</c>
/// </param>
/// <returns>
/// Buffer of the <c>InventJournalTrans</c> table.
/// </returns>
public InventJournalTrans populateInventJournalTrans(InventJournalId _InventJournalId)
{
    InventJournalTrans inventJournalTrans;
    InventDim          toInventDim;

    inventJournalTrans.JournalId      = _InventJournalId;
    inventJournalTrans.JournalType    = InventJournalType::Transfer;
    inventJournalTrans.TransDate      = systemdateget();
    inventJournalTrans.ItemId         = inventSum.ItemId;
    inventJournalTrans.Qty            = InventQty.realValue();

    // Dimensions from which the transfer performs
    inventJournalTrans.InventDimId    = inventDimLocal.inventDimId;
    inventJournalTrans.initFromInventTable(InventTable::find(inventSum.ItemId), False, False);

    // Dimensions To which the transfer performs
    toInventDim.inventSiteId         = InventSite.valueStr();
    toInventDim.InventLocationId     = InventWareHouse.valueStr();
    inventJournalTrans.ToInventDimId = InventDim::findOrCreate(toInventDim).inventDimId;
    inventJournalTrans.insert();

    return inventJournalTrans;
}

/// <summary>
/// Creates and posts the Inventory Transfer Journal.
/// </summary>
/// <remarks>
/// If there is any exception then the Inventory Journal data is deleted.
/// </remarks>
public void createAndPostJournal()
{
    InventJournalTable      inventJournalTable;
    InventJournalTrans      inventjournalTrans;
    JournalCheckPost        journalCheckPost;

    ttsbegin;

    // populates the inventJournalTable table
    inventJournalTable = element.populateInventJournalTable();

   // populates the inventJournalTrans table
    inventjournalTrans = element.populateInventJournalTrans(inventJournalTable.JournalId);

    ttsCommit;

    if (BOX::yesNo('Do you want to post the Journal ? ', DialogButton::Yes) == DialogButton::Yes)
    {
        // Call the static method to create the journal check post class
        journalCheckPost = InventJournalCheckPost::newPostJournal(inventJournalTable);

        if(journalCheckPost.validate())
        {
            try
            {
                journalCheckPost.run();
            }
            catch
            {
                // Deletes the InventJournalTable table, the InventJournalTrans will auto delete because of the Delete actions.
                InventJournalTable.delete();
            }
        }
     }
}


Friday, January 20, 2012

code samples ax 2009


I have found a very good link for code samples of different things in dynamics AX 2009


The list of items includes

·        Code samples for Microsoft Dynamics AX

·        Reports - Generate a List of Reports in Microsoft Dynamics AX

·        EP - Data Binding

·        AIF - Calling the Customer Service in Microsoft Dynamics AX 2009

·        AIF - Calling the Vendor Service in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Creating Multi-Section Forms in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Persisting Data Using View State and Data Sets in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Customizing Lookups in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Connecting a Details Part to a Grid

·        Enterprise Portal Quick Start: Adding a Toolbar to a Grid in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Handling Exceptions in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Using Record Context in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Creating a Wizard (Tunnel) Form in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Connecting a Fact Box to a Grid in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Creating Links Based on Menu Items in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Creating and Using Proxy Classes in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Creating a Basic Form in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Creating a Basic Grid in Microsoft Dynamics AX 2009

·        Enterprise Portal Quick Start: Creating an Advanced Grid in Microsoft Dynamics AX 2009

·        Code for video How Do I: Integrate an Application Using the .NET Business Connector?

·        Code for video How Do I: Create a Custom AIF Pipeline Component?

Monday, January 16, 2012

How to create the file SharePoint portal through code

In a scenario, I have to write the code that creates and validates the Http path existence and create the file in that path.

There are some classes in System.IO i.e. (FileInfo, FileStream) that are used to work with the file stystem, but those API doesn’t provide support for the http protocol, so to perform that specific task, I converted the http path into shared path with the help of the System.Url class with some logic.
public static FilePath convertsToAbsolutePath(FilePath _url)

{

    System.Uri  uri;
    FilePath    filePath;
    FilePath    absolutePath;
    Name        hostName;
    try
    {
        uri          = new System.Uri(_url);
        absolutePath = uri.get_AbsolutePath();
        hostName     = uri.get_Host();

        // converts the http path into Abosolute path
        filePath = strFmt(@'\\%1%2', hostName, strReplace(absolutePath, '/' , '\\'));
    }
    catch
    {
    checkFailed("http path is either not valid path or convertable");
    }

    return filePath;
}

Wednesday, January 4, 2012

Verify the URL Link and the File/Folder path in AX


I have experienced to verify the URL link in AX, Currently AX don’t contains any API that validate the URL Link
I have written a method that verifies the URL
public static boolean WebPathExists(str _url)
{
    boolean                    isExists;
    System.Net.WebRequest      UrlWebReq;
    System.Net.WebResponse     UrlWebRes;

    try
    {
        // Encode the URL first before passing to the Create method
        _url = System.Web.HttpUtility::UrlPathEncode(_url);
        UrlWebReq = System.Net.WebRequest::Create(_url);
        // This is needed if secure link is access like the AX share point portal
        // This is works only for the client calls
        UrlWebReq.set_Credentials(System.Net.CredentialCache::get_DefaultCredentials());
         UrlWebRes = UrlWebReq.GetResponse();
        // if there isn't any exception in the response (passed the above line of code) then the path is  correct.
        isExists  = true;
    }
    catch
    {
        isExists = false;
    }

    return isExists;
}

To checks the existence of Windows directory/File Path, there are some methods available in AX WinAPI class

WinAPI::folderExists(_filePath)
WinAPI::fileExists(_folderPath)

System Language versus Client Language

There are two types of Languages in AX

·        System Language
·        Client Language
The client language is used by default on the AX

System Language
Click System administration > Setup > System parameters.
Click General, and then select options for the general system parameters:

System currency – Select the currency to use. You can define a default currency for each ledger that you set up in the Ledger form.


Below method used to retrieves the Language Id of System Currency

SystemParameters::getSystemLanguageId()

Client Currency
To set the client language
Click Tools > Options

Below method used to retrieves the Language Id of System Currency
currentUserLanguage()