Monday, April 18, 2016

HOW TO CREATE A DIALOG IN DYNAMICS AX : X++ CODE

CREATE A DIALOG IN AX : X++ CODE 


class CustDialog extends RunBase
{
DialogField fieldAccount;
DialogField fieldName;
DialogField fieldGroup;
DialogField fieldCurrency;
}

pack() and unpack(0 are used to retain the last used values.
public container pack()
{
return conNull();
}
public boolean unpack(container _packedClass)
{
return true;
}
protected Object dialog()
{

Dialog dialog;
DialogGroup groupCustomer;
DialogGroup groupPayment;
dialog = super();
dialog.caption("Customer information"); dialog.allowUpdateOnSelectCtrl(true);
fieldAccount = dialog.addField(
extendedTypeStr(CustAccount), "Customer account");
fieldName = dialog.addField(extendedTypeStr(CustName));
fieldName.enabled(false);
dialog.addTabPage("Details");
groupCustomer = dialog.addGroup("Setup");
fieldGroup = dialog.addField(
extendedTypeStr(CustGroupId)); fieldCurrency = dialog.addField(
extendedTypeStr(CurrencyCode)); fieldGroup.enabled(false);
fieldCurrency.enabled(false);
groupPayment = dialog.addGroup("Payment");
return dialog;
}

Below method is used to dynamically assign the different values of the cust table
public void dialogSelectCtrl()
{
CustTable custTable;
custTable = CustTable::find(fieldAccount.value()); fieldName.value(custTable.name()); fieldGroup.value(custTable.CustGroup); fieldCurrency.value(custTable.Currency);
}
public static void main(Args _args)
CustSelect custSelect = new CustSelect();
if (CustSelect.prompt()) {
CustSelect.run();
}
}

READ COMMA SEPARATED FILES : DYNAMIC AX CODE X++

READ COMMA SEPARATED FILES : DYNAMIC AX CODE X++
PUBLIC VOID READCOMMAFILE()
{
CommaTextIo         file;
container           line;
#define.filename(@'C:\\accounts.csv') #File
file = new CommaTextIo(#filename, #io_read);
if (!file || file.status() != IO_Status::Ok)
{
throw error("File cannot be opened.");
}
line = file.read();
while (file.status() == IO_Status::Ok)
{
info(con2Str(line, ' - '));
line = file.read();
                }


}

CREATE COMMA SEPARATED FILES : DYNAMICS AX X++ CODE

CREATE COMMA SEPARATED FILES : DYNAMICS AX X++ CODE
public void createcommafiles()
{
CommaTextIo         file;
container            line;
MainAccount           mainAccount;
#define.filename(@'C:\accounts.csv') #File
file = new CommaTextIo(#filename, #io_write);
if (!file || file.status() != IO_Status::Ok)
{
throw error("File cannot be opened.");
}
while select MainAccountId, Name from mainAccount
{
line = [
mainAccount.MainAccountId,
mainAccount.Name];
file.writeExp(line);
}
info(strFmt("File %1 created.", #filename));

}

CODE TO ADD A NOTE IN A DOCUMENT REFERENCE FOR A TABLE

CODE TO ADD A NOTE IN A DOCUMENT REFERENCE 
FOR A TABLE

static void TabledocuAdd(Args _args) {
DocuRef      docuRef;
VendTable vendTable;
vendTable = VendTable::find('123');
docuRef.RefCompanyId = vendTable.dataAreaId;
docuRef.RefTableId    = vendTable.TableId;
docuRef.RefRecId        = vendTable.RecId;
docuRef.TypeId        = 'Note';
docuRef.Name            = 'Imported';
docuRef.Notes         = 'This vendor was imported.';
docuRef.insert();

}

Outbound and Inbound file generation using File system adapater in AX 2012

Outbound  and Inbound file generation using File system adapter in AX 2012


Below is the code to generate the outbound xml file for the selected record.
static void GenerateXmlSelectedRecord(Args _args)
{
    AxdSendContext      axdSendContext  = AxdSendContext::construct();
    AifEntityKey        aifEntityKey    = AifEntityKey::construct();
    AifEntityKeyList    aifEntityKeyList = AifEntityKeyList::construct();
    Map                 keyData;
    AifConstraintList   aifConstraintList   = new AifConstraintList();
    AifConstraint       aifConstraint       = new AifConstraint();
    CustTable           custTable;
    int i,j;
    CustCustomerService CustCustomerService = CustCustomerService::construct();
    ;
    custTable = CustTable::find('Cust001');

    keyData = SysDictTable::getKeyData(custTable);
    aifEntityKey.parmTableId(custTable.TableId);
    aifEntityKey.parmRecId(custTable.RecId);
    aifEntityKey.parmKeyDataMap(keyData);

    aifEntityKeyList.addEntityKey(aifEntityKey);


    axdSendContext.parmXMLDocPurpose(XMLDocPurpose::Original);
    axdSendContext.parmSecurity(false);


    aifConstraint.parmType(AifConstraintType::NoConstraint) ;
    aifConstraintList.addConstraint(aifConstraint) ;

    info(strFmt("%1",custTable.AccountNum));
    AifSendService::SubmitDefault(  classnum(CustCustomerService),
                                aifEntityKey,
                                aifConstraintList,
                                AifSendMode::Async,
                                axdSendContext.pack());
}

Example: Generating the outbound xml file for all records( i.e. specified criteria on the query).
Below is the code to generate the outbound xml for all the records or specified criteria.

static void GenerateMutiplerecords(Args _args)
{
    CustTable           custTable;
    AxdSendContext      axdSendContext      = AxdSendContext::construct();
    AifEntityKey        aifEntityKey        = AifEntityKey::construct();
    AifConstraintList   aifConstraintList   = new AifConstraintList();
    AifConstraint       aifConstraint       = new AifConstraint();
    AifEndpointList     endpointList;
    AifActionId         actionId;
    Query               query;
    QueryBuildDataSource    qbds;

    query               = new Query(queryStr(AxdCustomer));
    AxdSend::removeChildDs(query);

    actionId            = AifSendService::getDefaultSendAction(classnum(CustCustomerService), AifSendActionType::SendByQuery);
    aifConstraint.parmType(AifConstraintType::NoConstraint);
    aifConstraintList.addConstraint(aifConstraint) ;
    endpointList        = AifSendService::getEligibleEndpoints(actionId, aifConstraintList);

    AifSendService::SubmitFromQuery(actionId,endpointList,query,AifSendMode::Async);
}

Saturday, April 16, 2016

LIST OF AX TABLES /TABLES FIELDS IN DYNAMICS AX THROUGH JOB

LIST OF AX TABLES /TABLES FIELDS IN DYNAMICS AX THROUGH JOB
static void findTablesinAX(Args _args)
{
    Dictionary      dictionary;
    TableId         tableId;
    tableName       tableName;
    ;
    dictionary = new Dictionary();
    tableId = dictionary.tableNext(0);
    tableName = dictionary.tableName(tableId);
    while (tableId)
    {
        info(strfmt("%1 - %2",int2str(tableId), tableName));
        tableId = dictionary.tableNext(tableId);
        tableName = dictionary.tableName(tableId);
    }
}

static void FindTableFields(Args _args)
 {
TreeNode node = TreeNode::findNode(@'\Data dictionary\Tables\CustTable\Fields');
TreeNode childNode;
TreeNodeIterator nodeIT;
                                                       tab
nodeIt = node.AOTiterator();
childNode = nodeIt.next();
while(childNode)
{
    info(strfmt("PBATable %1", childNode.treeNodeName()));
    childNode = nodeIt.next();
}
}

CURRENT COMPANY IN AX /GET ACTIVE COMPANY'S NAME IN AX

Get the active company in AX 2009 - curExt()

Use the curExt() function to get the active company in AX;

static void curExtExample(Args _arg)
{
str CompanyId;
;

CompanyId = curExt();
Info(CompanyId);
}

Or else you can also use the following code.

static void curExtExample(Args _arg)
{
str CompanyId;
;

CompanyId = CompanyInfo::Find().DataAreaId;
Info(CompanyId);
}

COLOR FORMS IN AX ENVIRONMENTS : AX 2012 CODE X++ DYNAMICS

Color forms in ax Environments


There is a way to change the color of the Dynamics forms to help indicate what environment is in use. It involved overriding the SysSetupFormRun.run() method, which will be called every time a form is opened. On the class SysSetupFormRun, 
create a new method with this code:

public void run()
{
SysSQLSystemInfo systemInfo = SysSQLSystemInfo::construct();
; 

super();
// Set the color scheme of this instance of the SysFormRun to RGB
this.design().colorScheme(FormColorScheme::RGB);
// If the database name is not the live version, change the color of the form
if (systemInfo.getloginDatabase() != 'MyDBName')
this.design().backgroundColor(0x112255);
}

AX is too slow : DELETE THE LOG FILES

AX is too slow

If your Dynamics AX is not responding as fast as it should be then you should delete the logs in the following location since they are of no use.
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\LOGS

Logs Files which are no use…
This is just one of the reasons there might be other reasons also.

AX Error WHILE OPENING AX :Connection with the Application Object Server could not be established.

Connection with the Application Object Server could not be established.



If you get the above error 
GOTO start>run>cmd>services.msc




Start the dynamics ax service .


The problem gets resolved.

AX Error :You are not a recognized User of Microsoft Dynamics AX. Contact your system administrator for the help

Error:

You are not a recognized user with admin righof Microsoft Dynamics AX. Contact your system administrator for the help


Solution:
Ask the user with admin rights to give u access in the ax through admin module>user form .


If this error is in your local PC then see the username in the dynamics services and then try to login from those credentials and give rights to the user .

DYNAMICS AX LOG FILES PATH C:\Program Files\ :VIKAS MEHTA

AX Log Files

Just in case your system is running out of space or AX is too slow then delete the log files in the following location.


C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\LOGS


Thanks,
Vikas Mehta.

HOW TO SELECT MULTIPLE/ALL RECORDS IN AX :VIKAS MEHTA

How to select multiple/all records in AX

Recently I came across an issue regarding the selection of multiple records in AX ,


You can do that in many ways:


1) Press offcourse CTRL and then select the records with the mouse.


2) Press SHIFT and scroll the mouse and select the end record(Make sure you keep the mouse button pressed.)


3) Incase you want to select all the records you need to press
 Ctrl+Shift+Home/End : To mark all records between the currently selected record and the first/last    record

 You will get the following warning.

Do you want to continue  loading all the lines? 










Click YES

Thanks,
Vikas Mehta.

Change Color of your Dynamics AX Environments/Forms

 Change Color of your Dynamics AX  Environments/Forms


To change the color of the Dynamics forms to help indicate what environment is in use. It involved overriding the SysSetupFormRun.run() method, which will be called every time a form is opened. On the class SysSetupFormRun, create a new method with this code:
public void run()
{
SysSQLSystemInfo systemInfo = SysSQLSystemInfo::construct();
; 

super();
// Set the color scheme of this instance of the SysFormRun to RGB
this.design().colorScheme(FormColorScheme::RGB);
// If the database name is not the live version, change the color of the form
if (systemInfo.getloginDatabase() != 'DynamicsAX_test')
this.design().backgroundColor(0x112255);
}



 If your live and test systems use the same database name, but the AOS is running on different servers you can modify this code to to match on systemInfo.getLoginServer() != 'MyServerName'. You can change the color by setting the hex value. It uses the RGB values in reverse order: 0xBBGGRR. 

thanks,
Vikas Mehta

HOW TO PRINT A DIFFERENT SALES INVOICE PER COMPANY IN AX :VIKAS MEHTA

How to print a different Sales Invoice per company in AX

If you want to print a different Sales Invoice per every company you have,  change the method printJournal in the table CustInvoiceJour and the form CustInvoiceJournal (MenuButton "SalesInvoiceShow" -> Copy, Original and Original print):

Modified method printJournal for the table CustInvoiceJour:
server void  printJournal(SalesFormLetter      salesFormLetter = null,
                          RecordSortedList     journalList     = null,
                          NoYes                copy            = NoYes::No)
{
    Args                parameters = new Args();
    MenuFunction        salesInvoiceMenu;
    ;

    // Show the correct report for the every company in AX
    switch (strupr(curExt()))
    {
        case "CEU":
            salesInvoiceMenu = newMenuFunction(menuitemoutputstr(OPPSalesInvoice),MenuItemType::Output);
            break;
       
        default:
            salesInvoiceMenu = newMenuFunction(menuitemoutputstr(SalesInvoice),MenuItemType::Output);
    }
    // End

    parameters.caller(salesFormLetter);

    if (journalList)
        parameters.object(journalList);
    else
        parameters.record(this);

    salesInvoiceMenu.run(parameters);
}


For every MenuItemButton below the SalesInvoiceShow, you must override the clicked method as follows:
void clicked()
{
    Args                parameters = new Args();
    MenuFunction        salesInvoiceMenu;
    ;

    // Let the menuItemButton as this, with original parameters but
    // don't call super, to avoid call directly to report SalesInvoice
    //super();

    switch (strupr(curExt()))
    {
        case "OPP":
            salesInvoiceMenu = newMenuFunction(menuitemoutputstr(OPPSalesInvoiceCopy),MenuItemType::Output);
            break;

        default:
            salesInvoiceMenu = newMenuFunction(menuitemoutputstr(SalesInvoiceCopy),MenuItemType::Output);


Thanks,
Vikas Mehta.

DYNAMICS AX X++ CODE TO CONNECT TO SQL DATABASE DSN ODBC CONNECTION LOGIN

How  to connect to an external DB from Dynamics AX using X++


1. 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
2. X++ code
static void TestOdbcJob()
{
    LoginProperty login;
    OdbcConnection con;
    Statement stmt;
    ResultSet rs;
    str strQuery, criteria;
    SqlStatementExecutePermission perm;
    ;

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

    //Create a connection to external database.
    con = new OdbcConnection(login);

    if (con)
    {
        strQuery = strfmt("SELECT * from tableName WHERE XXX = ‘%1′ ORDER BY  FIELD1, FIELD2", criteria);

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

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


        rs.close();
        stmt.close();
    }
    else
    {
        error("Failed to log on to the database through ODBC");
    }
}

SQL STATEMENT IN AX X++ CODE

SQL statement in Ax code x++ generated 

So here is another small tip based on me reading Inside Microsoft Dynamics AX 2012 R3.

If you want to know what SQL statement the SQL Server query processor generates based on a regular X++ select statement, you can add to the keyword generateOnly to the statement and afterwards call the getSQLStatement method on the record buffer.

Example:
   AccountingEvent         accountingEvent;
   SourceDocumentHeader    sourceDocumentHeader;
   
   select generateonly accountingEvent
   join sourceDocumentHeader 
       where sourceDocumentHeader.RecId == accountingEvent.SourceDocumentHeader;

   info (accountingEvent.getSQLStatement());

AX 2012 SSRS REPORT ERRORS AND TROUBLESHOOT

AX 2012 SSRS - Misc Problems and Solutions

Problem
You get a http 503 service unavailable during installation of Report Extensions.

Solution
Check the remote registry settings.Remove Report Server settings in AX.

Problem
The title of the window of a SSRS report doesn't change as expected when editing it on the menu item or from Visual Studio.

Solution
Delete usage data in AX.

Problem
Error while setting server report parameters. Error message: The DefaultValue expression for the report parameter ‘AX_CompanyName’ contains an error: Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. (rsRuntimeErrorInExpression)

Solution
Open the file C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportServer\rssrvpolicy.config
Set PermissionSetName to “FullTrust” at Name=Report_Expressions_Default_Permissions

See:
http://community.dynamics.com/product/ax/axtechnical/b/axsupport/archive/2012/02/02/microsoft-dynamics-ax-2012-reporting-extensions-error-system-security-permissions-environmentpermission-while-running-report.aspx

Problem
The reports are deployed to the wrong report folder.

Solution
AX was started with the wrong active client configuration. You need to set the client configuration to the AOS that you wish to deploy to, even though you are overriding it on the client shortcut. Don't forget to restart AX afterwards - the setting is cached when AX is started.

Problem
The report cannot be deployed because it couldn't find the network path.

Solution
Start Windows service "Remote Registry"
See: http://technet.microsoft.com/en-us/library/gg724094.aspx

Problem
Error when running SSRS in batch:
"System.InvalidCastException: Unable to cast object of type 'Microsoft.Dynamics.Ax.Xpp.DictMethod' to type 'Dynamics.Ax.Application.SysDictMethod'."

Solution
Change SysDictMethod to DictMethod on line 3 in:
\Classes\SrsReportRdpRdlWrapperContractInfo\buildMemberAndNestedObjectMap

Problem
Error while setting report parameters. Error message: An error has occurred during report processing. (rsProcessingAborted)

Solution
See: https://community.dynamics.com/ax/b/axsupport/archive/2013/03/12/cannot-be-processed-at-the-receiver-due-to-a-contractfilter-mismatch-at-the-endpointdispatcher.aspx


Related Posts Plugin for WordPress, Blogger...