5/16/2018

How to get error message

The error messages in AX are stored in the global object infolog. So when trying to print the message, we need to access infolog instance and fetch the information. I created a static method in my utility class so I can reuse it whenever I want to do that in my code.
The method is,

public static str getErrorMsg(int _infologPosition = 0)
{
    str msg;
    int currentLine;
    ;
    for (currentLine = _infologPosition + 1; currentLine <= infologline(); currentLine++)
    {
        msg += infolog.text(currentLine) + '\n';
    }

    return msg;
}


So in code, when I want to capture the message, I can just,

try
{
......
}
catch
{
errorMsg = UtilityClass::getErrorMsg();
......
}

8/24/2017

How to add .dll file into AX 2012

When I was trying to add a .dll into AX 2012, I could not find a document how to do that. After many testings I did, eventually, I figured out how it works. Basically, this post is for those developers who have not used .dll in AX 2012. Hope it could help them understand how to do it.
I found that to let AX use the .dll we need to,

1. Let AX know that .dll, so we can compile the code.

  • Add reference



















If the .dll is already added to GAC on the client machine, we can select it from the assembly list. Otherwise, we'll need to select the .dll file by browsing the file.

  • If .dll is not in GAC on the client machine, we additionally need to copy the .dll file to the client/bin folder and restart the client so we can compile the code which consumes the .dll. 


2. Let AX be able to find the .dll to consume it.

  • If the code is run at server side, we need to make sure the .dll could be correctly consumed. So we need to either add that .dll to GAC on the server machine or add it to server/[InstanceName]/bin folder and restart the AOS.
  • If the code is run at client side, we need to do the same to the client machine.


Once we've done above, the .dll should be able to be called by AX 2012.

4/25/2017

How to email report in AX 2012 without outlook?

In out of box AX 2012, the user has to have outlook to mail the report which is kind of annoying, cause not all of the AX user has outlook. We can find code snippet to bypass outlook to do that online, but you'll notice those codes are only for the old AX report instead of SSRS report. In AX 2012,  the reports are all SSRS reports which means we cannot modify the code in Info\reportSendMail as we did for AX 2009 to bypass outlook.
In fact, it's even easier to do that in AX 2012. In AX 2012, when emailing reports, the class 'SrsReportRunMailer' is executed. What we need to do is just change that class. If you open that class, you'll see the method 'initMailer' decides whether to use SMTP or outlook. When mailing in a batch, it uses SMTP, but when emailing at client, it uses outlook. To change the logic to always go with SMTP, we only need to comment out the 'if' clause and leave the code which takes SMTP. See below,

private void initMailer()
{
    fromAddress = SrsReportRunMailer::buildFromEmailAddress();
    // Always use SMTP, never outlook.
    //if (isRunningOnServer())
    //{
        // get the mailer object
        mailer = this.parmSysMailer();

        // validate the from email addresses
        if (!fromAddress ||
            !SysEmailDistributor::validateEmail(fromAddress))
        {
            error(strfmt("@SYS134596", "@SYS4083"));
            return;
        }
    //}
    //else
    //{
        //inetMailer = new SysINetMail();
    //}



2/24/2017

Access the private project created by other users

Sometimes we do want to access the private project created by other users, e.g. when importing a model we get a private project conflict, to resolve this we have to find the project and delete it. If the owner of those projects are gone, we'll not be able to access them as normal, we'll have to do something unusual to get it.
Even though there're many different approaches to get this resolved in others' blogs/forum, I found my way is probably easiest. Here it is,
1. Find the AX database on SQL server.
2. Open table ModelElement with the private project type only.The private project type is 38.
3. Change the prefix of the private project you are looking for from the original user Id to your user Id by a simple update like.
  update [AX1111_2].[dbo].[ModelElement]
  set Name = 'Admin_AOTExport2012_DynamicsPerfDirect'
  where Name = 'JasonPAdmin_AOTExport2012_DynamicsPerfDirect' and ElementType = 38
4. Reopen AX client with your account, and you'll see the project appears in your private project

11/03/2016

Programmatically settle a payment journal trans against a specific open transaction.

In the following example, I used a customer invoice as an example of open transaction, the logic applies to vendor open transaction as well.
To make sure the amount can be fully settled, I checked if the payment amount matches the open transaction amount. According to the specific requirement, we can ajust the logic.
The key point of this code snippet is the use of CustVendOpenTransManager.

static void SettleCustOpenTrans(Args _args)
{
    CustVendOpenTransManager manager;
    LedgerJournalTrans ljt;
    CustTransOpen cto;
    CustTrans ct;
    ;
    ct = CustTrans::findFromInvoice('Invoice Number', 'Customer Account');

    // Get cust trans open.
    if (ct.RecId)
    {
        cto = CustTransOpen::findRefId(ct.RecId);

        // Check amount to make sure the open trans was not partially settled before.
        if (cto.AmountCur != ct.AmountCur)
            throw error('invoice has been partially paid');
    }
    else
    {
        throw error('invoice could not be found');
    }

    // Fetch the added LedgerJournalTrans.
    ljt = LedgerJournalTrans::find('Journal number', 'Jounal trans voucher', true);

    // Check if the journal trans is paying the invoice wholly.
    if (ljt.AmountCurCredit - ljt.AmountCurDebit != cto.AmountCur)
        throw error('this payment journal line is not paying the invoice wholly');


    ttsBegin;
    // Mark the invoice.
    manager = CustVendOpenTransManager::construct(ljt);
    manager.updateTransMarked(cto, true);

    // Update journal trans.
    ljt.MarkedInvoice = ct.Invoice;
    ljt.SettleVoucher = SettlementType::SelectedTransact;
    ljt.update();
    ttsCommit;

    info('done');
}


10/28/2016

Observations in sysoperation framework Synchronous execution mode

Observations in Synchronous execution mode.

Called from: Client
Run on: Server (If the service class is registered in AXClient service group, it will run on client, cause the executeOperationWithRunAs returns false in that case.)
Session: CIL (If the service class is registered in AXClient service group, it will run in an interpreter session, cause the executeOperationWithRunAs returns false in that case.)

Possible errors:
  • As I said above, normally synchronous service will be executed on server and in CIL session, but if it's executed on client and interpreter session, we cannot set 'Runon' of the service class to server any longer. Otherwise, we'll see a data contract error like this,
  • If the method is published as an AIF service entry (with [SysEntrypointAttribute]), and the 'Runon' is 'Call from', we'll see this 'uncheced' error when executing,

Conclusion,
In AX, we cannot call a method in a servie class in both synch and asynch mode, because of the 2 errors above and asynch mode needs the service to be registered in AXClient service group.



8/08/2016

How to run SSRS report from X++ code in AX? 2. SrsReportRunController

SrsReportRunController is new in AX2012. It extends the SysOperation framework.
Here's a sample.

SrsReportRunController 

SrsReportRunController controller;
;
//Initilize
controller = new SrsReportRunController();
//Set report name.
controller.parmReportName(ssrsReportStr(Test, PrecisionDesign1));

//Run report
controller.startOperation();

To set parameter

  • If the data source type is Report Data Provider, we need to use the a data contract instance to set parameter. See sample below,
//Initilize
controller = new SrsReportRunController();
//Set report name.
controller.parmReportName(ssrsReportStr(Test, PrecisionDesign1));
//Get data contract instance.
rdpContract = controller.parmReportContract().parmRdpContract() as TestDataContract;
//Set parameter value;
rdpContract.parmSalesId('so-000050');
controller.startOperation();

  • If the data source type is Query, and the dynamic filer is turned off, we can set the parameter thru a RDL contract instance.
//Initilize
controller = new SrsReportRunController();
//Set report name.
controller.parmReportName(ssrsReportStr(Test, PrecisionDesign1));
//Get data contract instance.
rdpContract = controller.parmReportContract().parmRdpContract() as TestDataContract;
//Set parameter value;
rdpContract.parmSalesId('so-000050');
controller.startOperation();

  • If the data source type is Query, and the dynamic filter is turned on, we need to get the query of the report and set range to that query.

//Initilize
controller = new SrsReportRunController();
//Set report name.
controller.parmReportName(ssrsReportStr(Test, PrecisionDesign1));
//Get data contract instance.
query = controller.parmReportContract().parmQueryContract().lookup('Dataset1_DynamicParameter');
//Set parameter value;
query.dataSourceNo(1).clearRange();
query.dataSourceNo(1).addRange(fieldNum(SalesTable, SalesId)).Value('so-000050');

controller.startOperation();

Remember: parmQueryContract() returns the map of the queries, and we can lookup by the parameter name

Run  multiple reports to screen

When printing multiple reports to screen in code, the previous printed report will block the next one, untill user closes the previous one, the next one starts rendering. This is anoying if user needs to print multiple reports to screen as a batch. The solution to this is pretty simple, we just need to create a class to extend SrsReportRunController and overwrite the methods dialogShow and dialogClose. See sample below,

protected void dialogShow()
{
    SysOperationDialog sysOperationDialog;
    FormRun formRun;

    if (useReportViewerForm)
    {
        dialog.run();
        this.dialogPostRun();

        sysOperationDialog = dialog as SysOperationDialog;
        formRun = sysOperationDialog.formRun();
        formRun.detach();
    }
    else
    {
        super();
    }
}



protected void dialogClose()
{
    if(!useReportViewerForm)
    {
        super();
    }
}