Thursday, September 08, 2011

Connect to an External Database from X++ Code

Create a DSN


To create a Data Source Name (DSN) go to Administrative Tools > Data Sources (ODBC).

Create the DSN on the tier where the X++ code will call the DSN from. This will be either on the client computer or on the AOS computer.

X++ Code Example with ODBC


// X++ job
static void TestOdbcJob()
{
    LoginProperty loginProperty;
    OdbcConnection odbcConnection;
    Statement statement;
    ResultSet resultSet;
    str sql, criteria;
    SqlStatementExecutePermission perm;
    ;

    // Set the information on the ODBC.
    loginProperty = new LoginProperty();
    loginProperty.setDSN("dsnName");
    loginProperty.setDatabase("databaseName");

    //Create a connection to external database.
    odbcConnection = new OdbcConnection(loginProperty);

    if (odbcConnection)
    {
        sql = "SELECT * FROM MYTABLE WHERE FIELD = "
            + criteria
            + " ORDER BY FIELD1, FIELD2 ASC ;";

        //Assert permission for executing the sql string.
        perm = new SqlStatementExecutePermission(sql);
        perm.assert();

        //Prepare the sql statement.
        statement = odbcConnection.createStatement();
        resultSet = statement.executeQuery(sql);

        //Cause the sql statement to run,
        //then loop through each row in the result.
        while (resultSet.next())
        {
            //It is not possible to get field 3 and then 1.
            //Always get fields in numerical order, such as 1 then 2 the 3 etc.
            print resultSet.getString(1);
            print resultSet.getString(3);
        }

        //Close the connection.
        resultSet.close();
        statement.close();
    }
    else
    {
        error("Failed to log on to the database through ODBC.");
    }
}

Wednesday, September 07, 2011

Create a custom site theme for SharePoint and make it available to others


Here are the steps you can follow to create your own custom theme and make it available to others in your organization. For this example, I created a new theme called "My Custom Theme" based on the Classic theme in SharePoint.

1 - Identify a theme that closely resembles the theme you want to create

The first step is to first take a look at the existing themes available in SharePoint and determine if one of those themes closely resembles the theme that you want to create.
  1. Go to the SharePoint site where you want to create the theme (log on, if necessary).
  2. Click the Site Actions menu and choose Site Settings.
  3. Under Look and Feel, click Site theme.
  4. Look through the themes until you find one that closely resembles the theme you want to create. Note the name of the theme so you can find the files you need for later steps.
  5. For this example, I know that I want to use the Classic theme.
  6. Cancel out of the Site Theme page.
2- Create your custom theme folder
  1. Log onto the server that hosts your SharePoint site and browse to the following folder:
    C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\THEMES
  2. Copy the folder that corresponds with our theme. In my case, I copied the Classic folder.
  3. Paste this into this same THEMES folder, so that you end up with a Copy of THEMENAME folder. In my case, I ended up with a Copy of CLASSIC folder.
  4. Rename this folder to MyCustomTheme.
3 - Modify the theme setup information file (INF)
  1. Open the MyCustomTheme folder and locate the name of the theme you chose as a starting point for your new theme. For example, CLASSIC.INF.
  2. Rename CLASSIC.INF (or the INF file for the theme you chose) to MYCUSTOMTHEME.INF (yes, please use upper case).
  3. Edit MYCUSTOMTHEME.INF in Notepad (or your preferred text editor).
  4. Perform a search and replace in this file, replacing every instance of Classic (or the theme you chose) withMyCustomTheme.
  5. Save and close MYCUSTOMTHEME.INF.
4 - Modify the SPTHEMES.XML file
  1. Browse to the following folder:
    C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033
  2. Locate SPTHEMES.XML.
  3. Edit SPTHEMES.XML in Notepad (or your preferred text editor).
  4. Add the following text after the <SPThemes> tag so that it matches the other templates in this file:
    <Templates>
     <TemplateID>MyCustomTheme</TemplateID>
     <DisplayName>My Custom Theme</DisplayName>
     <Description>My Custom Theme.</Description>
     <Thumbnail>images/mycustomtheme.gif</Thumbnail>
     <Preview>images/mycustomtheme.gif</Preview>
    </Templates>
  5. Save and close SPTHEMES.XML.
  6. Reset the server so that your theme shows up on the Site Theme page in SharePoint. You can reset the server by entering the following text in the Start > RUN line:
    iisreset computer name
    (where computer name is the name of the server)
5 - Apply your custom theme to your site
  1. Return to your SharePoint site.
  2. Click the Site Actions menu and choose Site Settings.
  3. Under Look and Feel, click Site theme.
  4. In the list of themes, select My Custom Theme.
  5. Click Apply.
    Your site will look no different from the Classic theme since we based your custom theme on it. 

Tuesday, September 06, 2011

AX 2009 : Create and Post Inventory Journal


Following is job which will create and post the Inventory Journal in ax 2009 :-

static void createMovJournal(Args _args)
{ InventJournalTable journalTable;
InventJournalTrans journalTrans;
InventJournalTableData journalTableData;
InventJournalTransData journalTransData;
InventTable inventTable;
InventDim inventDim;
Counter cnt;
InventJournalCheckPost journalCheckPost = new InventJournalCheckPost();
DialogButton dbtn;
;

journalTableData = JournalTableData::newTable(journalTable);
journalTransData = journalTableData.journalStatic().newJournalTransData(journalTrans,journalTableData);

// Init JournalTable

journalTable.clear();

journalTable.JournalId = journalTableData.nextJournalId();
journalTable.JournalType = InventJournalType::Movement;
journalTable.JournalNameId = journalTableData.journalStatic().standardJournalNameId(journalTable.JournalType);

journalTableData.initFromJournalName(journalTableData.journalStatic().findJournalName(journalTable.JournalNameId));

// Init JournalTrans
select firstonly inventTable;
for(cnt=1;cnt<10;cnt++)
{
journalTrans.clear();
journalTransData.initFromJournalTable();

journalTrans.TransDate = systemdateget() + 1 div 2;
journalTrans.ItemId ='1103';    //inventTable.ItemId;
journalTrans.Qty = 100;
journalTrans.CostAmount = 100;
journalTrans.LedgerAccountIdOffset='110170';

// Dimension details

inventDim.InventLocationId = '11';
journalTrans.InventDimId ='00000061_069'; //InventDim::findOrCreate(inventDim).inventDimId;

journalTransData.create();



}

journalTable.insert();

// Call the static method to post the journal
if(InventJournalCheckPost::newPostJournal(journalTable).validate())

if(box::yesNo("Do you want to Post Now?",DialogButton::No)==DialogButton::Yes)
{
InventJournalCheckPost::newPostJournal(journalTable).run();
}
else
{
 box::info("Not Posted");
}
info("done");

}


Enjoy DAX.
Cheers !!@

Posting SalesOrder Confirmation with SalesFormLetter Class in Ax 2009

The Book of orders in Microsoft Dynamics AX is done through the class "sales form letter" or one of its more concrete (derived) classes.
Each reservation type (eg confirmation, delivery note, invoice) is represented by a class that is derived from the base class "sales form letter" (see illustration).
To reserve an order by code, an object of class "sales form letter" can be created.
salesFormLetter = SalesFormLetter::construct(DocumentStatus::Confirmation);
This is done as common practice in Microsoft Dynamics AX, on the "construct" method of the class. must be specified as parameters to this method the desired type of booking (eg confirmation, delivery note, invoice).
The "construct" a method generates the appropriate transaction type, object and returns that (in this case, a "SalesFormLetter_Confirm" object created).
The actual book is called the method "update". Since this method can all be submitted for the booking necessary data as parameters, is a single assignment of such a mandate which is to be posted not necessary.
For example:
// — — Book without print 
static void PostingConfimation(Args _args) 

SalesFormLetter salesFormLetter; 
SalesTable salesTable; 
SalesId salesId; 
PrintJobSettings printJobSettings; 

//Angabe des Auftrags, welcher gebucht werden soll. 
salesId = "00423_036"; 
salesTable = SalesTable::find(salesId); 
// Bestimmen des Buchungstyps durch Angabe des DocumentStatus 
salesFormLetter = SalesFormLetter::construct(DocumentStatus::Confirmation); 
//Buchen des Auftrags (aber nicht Drucken). 
salesFormLetter.update(salesTable, 
SystemDateGet(), 
SalesUpdate::All, 
AccountOrder::None, 
NoYes::No, 
NoYes::No); 
}

This example is good to see that necessary for the posting of a job essentially only two steps.
  1. On the method "construct" the company produce a type PCX object.
  2. By calling the method "update" to book the order.
Of course, even more extensive or more specialized booking scenarios with the class are ready to buy "sales form letter." For example it is possible to print the same booking with these documents (once, several times and in different formats), open to the mask for the booking (for the user to influence within the company) or the booking is not directly run, but this provide for batch processing.
Thus, it is not too complex, just one more example of booking and simultaneously print the appropriate documents.
// — — Book with print 
static void PostingConfimation(Args _args) 

SalesFormLetter salesFormLetter; 
SalesTable salesTable; 
SalesId salesId; 
PrintJobSettings printJobSettings; 

salesId = "00423_036"; 
salesTable = SalesTable::find(salesId); 
salesFormLetter = SalesFormLetter::construct(DocumentStatus::Confirmation); 
salesFormLetter.update(salesTable, 
SystemDateGet(), 
SalesUpdate::All, 
AccountOrder::None, 
NoYes::No, 
NoYes::Yes); 
printJobSettings = new PrintJobSettings(salesFormLetter.printerSettingsFormletter( 
PrintSetupOriginalCopy::Original)); 
printJobSettings.setTarget(PrintMedium::File); 
printJobSettings.format(PrintFormat::PDF); 
printJobSettings.fileName(@"C:\Test_Order.pdf"); 
salesFormLetter.updatePrinterSettingsFormLetter(printJobSettings.packPrintJobSettings()); 
salesFormLetter.printJournal(); 
}
http://blogs.bojensen.eu/?p=330
Source:

Post a Ledger Journal in AX 2009 using X++


Following is the code for Posting a Ledger Journal using X++ :


static void ExampleLedgerJournal(Args _args)
{
LedgerJournalName LedgerJournalName;
LedgerJournalTable ledgerJournalTable;
LedgerJournalTrans ledgerJournalTrans;
LedgerJournalCheckPost ledgerJournalCheckPost;
NumberSeq numberseq;
LedgerJournalNameId LedgerJournalNameId = 'GenJrn';
BankAccountID BankAccountID = 'EUR OPER';
ledgerAccount offsetAccount = '601500';
amountCur amountCur = 102;
;
ttsbegin;
// Find a ledgerJournalName record
select firstonly LedgerJournalName
where LedgerJournalName.JournalName ==LedgerJournalNameId;
//Created the ledgerJournalTable
ledgerJournalTable.JournalName =LedgerJournalName.JournalName;
ledgerJournalTable.initFromLedgerJournalName();
ledgerJournalTable.Name = 'Hotel';
ledgerJournalTable.insert();
numberseq =NumberSeq::newGetVoucherFromCode(ledgerJournalName.VoucherSeries);
ledgerJournalTrans.Voucher = numberseq.voucher();
//Generate the transaction line
ledgerJournalTrans.JournalNum =ledgerJournalTable.JournalNum;
ledgerJournalTrans.CurrencyCode = 'EUR';
ledgerJournalTrans.ExchRate =Currency::exchRate(ledgerJournalTrans.CurrencyCode);
ledgerJournalTrans.AccountNum = BankAccountID;
ledgerJournalTrans.AccountType =LedgerJournalACType::Bank;
ledgerJournalTrans.AmountCurCredit = amountCur;
//Pass the Date . You can also use str2Date('08/08/2008') if period is not defined.
ledgerJournalTrans.TransDate = today();
ledgerJournalTrans.Txt = 'Room Stay';
ledgerJournalTrans.OffsetAccount = offsetAccount;
ledgerJournalTrans.OffsetAccountType =LedgerJournalACType::Ledger;
ledgerJournalTrans.insert();
info(strfmt('Journal Id:%1',ledgerJournalTable.JournalNum));
//Post the Journal
ledgerJournalCheckPost= ledgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,NoYes::Yes));
ledgerJournalCheckPost.run();
ttscommit;
}

Enable UAT database over OneBox DevTest environment using JIT

 Dear Friends, In this Article I will show you how to enable UAT database access for Development machine using just-in-time (JIT). Many time...