Alfasith AX

Alfasith AX
اللَّهُمَّ انْفَعْنِي بِمَا عَلَّمْتَنِي، وَعَلِّمْنِي مَا يَنْفَعُنِي، وَزِدْنِي عِلْمًا

Wednesday, July 17, 2013

Save the SSRS report as pdf or print it through code in Dynamic AX

Ax 2012 Reports (SSRS) Print Utilities
This article gives the examples, to print the reports of Dynamics Ax 2012 (SSRS Reports), in different ways.
1)Sending the Ax report through mail.
2)Save the report as file in local computer. (example covered to save the report as .pdf file)
Mail the report

static void myJobPrintReportMail(Args _args)
{
SrsReportRun                        reportRun;
Query                               query;
SRSReportPrintDestinationSettings   SRSReportPrintDestinationSettings;
SRSPrintDestinationSettings         srsPrintSettings;
;
delete_from SRSReportPrintDestinationSettings;
reportRun = new SRSReportRun();
reportRun.reportName(“Vend.Report”);  //<ReportName>.<DesignName>
// Set printersettings
srsPrintSettings = reportRun.printDestinationSettings();
srsPrintSettings.printMediumType(SRSPrintMediumType::Email);
srsPrintSettings.emailTo(“mail@gmail.com”);
srsPrintSettings.emailAttachmentFileFormat(SRSReportFileFormat::PDF);
srsPrintSettings.emailSubject(strfmt(‘vendor report – %1′, systemdateget()));
srsPrintSettings.pack();
reportRun.showDialog(false);
reportRun.init();
reportRun.run();
}
Save the Report To PDF File
static void myJobPrintReportPDF(Args _args)
{
SrsReportRun                        reportRun;
SRSPrintDestinationSettings         srsPrintSettings;
SRSReportPrintDestinationSettings   SRSReportPrintDestinationSettings;
;
delete_from SRSReportPrintDestinationSettings;
reportRun = new SRSReportRun();
reportRun.reportName(“Vend.Report”);  //<ReportName>.<DesignName>
// Set printersettings
srsPrintSettings = reportRun.printDestinationSettings();
srsPrintSettings.overwriteFile(true);
srsPrintSettings.printMediumType(SRSPrintMediumType::File);
srsPrintSettings.fileFormat(SRSReportFileFormat::PDF);
srsPrintSettings.fileName(“D:\\Vendors.pdf”);
srsPrintSettings.pack();
reportRun.showDialog(false);
//reportRun.saveParameters();                         //For Report parameters
reportRun.init();
reportRun.run();
}
In case of Parameterized reports
If the report has parameters (Using Contract Class),
Use the following line, to read the parameters and save to the table called,SRSReportParameters
reportRun.saveParameters();
For the same parameters, provide the required values to run the report.
I have tried the example of Vendor Transactions report, with my customized version. This standard report has 4 parameters, have used same parameters and provided the values inSRSReportParameters table to run the report.
For clear idea, have a quick look in to the following picture.



UI Builder class in Dynamic AX

UI Builder class 

Today i will share how to give a filter lookup using an UI Builder class.                                                                                            

Step1 : First create a DP class for a customer report


[
    SRSReportQueryAttribute(queryStr(BSCustTable)),
    SRSReportParameterAttribute(classStr(SRCustomLookUpContract))

]
class SRCustomLookupDP extends SRSReportDataProviderBase
{
    SRCustomLookUpContract contract;
    CustNameTmp        CustNameTmp;
    AccountNum         AccountNum;
    CustGroupId       CustGroupId;

}

private Query buildQuery(
    Query                   _query,
    AccountNum                _AccountNum
    )
    {

    if(_AccountNum)
        _query.dataSourceTable(tablenum(CustTable), 1).addRange(fieldnum(CustTable, AccountNum)).value(queryvalue(_AccountNum));

        return _query;
     }

[
    SRSReportDataSetAttribute('CustNameTmp')
]
public CustNameTmp getCustNameTmp()
{
    select * from CustNameTmp;

    return CustNameTmp;
}

private void getReportParameters()
{
    SRCustomLookUpContract SRCustomLookUpContract = this.parmDataContract();

    if (SRCustomLookUpContract)
    {
        AccountNum    = SRCustomLookUpContract.parmAccountNum();
        CustGroupId  = SRCustomLookUpContract.parmCustGroup();
        //AccountNum    = SRCustomLookUpContract.parmAccountNum2();
    }
 }


public  void insertCustNameTmp(AccountNum _accountNum)
{
    CustTable  CustTable;

    ;
    while select CustTable where CustTable.AccountNum == _accountNum
    {
       CustNameTmp.AccountNum = _accountNum;
       CustNameTmp.Name     = CustTable.name();

       CustNameTmp.insert();
    }

}


/// <summary>
/// executes the logic based on the parameter entries
/// </summary>
/// <remarks>
/// fills up the temp table
/// </remarks>
[SysEntryPointAttribute]
public void processReport()
{
    Query query;
    QueryRun queryRun;
    CustTable       custTable;
    ;
    this.getReportParameters();
    queryRun = new QueryRun(this.buildQuery(this.parmQuery(),AccountNum));
    while(queryRun.next())
    {
        custTable = queryRun.get(tableNum(custTable));
        this.insertCustNameTmp(custTable.AccountNum);
    }
}

Step2 : Second create a Contract class for a customer report


[DataContractAttribute,
SysOperationContractProcessingAttribute(classstr(SRCustomLookupsUIBuilder))
]
class SRCustomLookUpContract
{
    AccountNum        accountNum;
    CustGroupId      CustGroupId;

}


[DataMemberAttribute('AccountNum')
    ]
public AccountNum parmAccountNum(AccountNum _accountNum = accountNum)
{
    accountNum = _accountNum;
    return accountNum;
}


[DataMemberAttribute('CustGroup')
    ]
public CustGroupId parmCustGroup(CustGroupId _CustGroupId = CustGroupId)
{
    CustGroupId = _CustGroupId;
    return CustGroupId;
}


Step3 : Third create a UI Builder class for a customer report


class SRCustomLookupsUIBuilder extends SrsReportDataContractUIBuilder
{
      DialogField   dialogAccountNum,dialogCustGroup; //dialogCreditRating;
      DialogGroup   dialogGroup, dialogGroup2;
      boolean       enable;


}


private void accountNumLookup(FormStringControl accountNumLookup)
{
    Query                   query = new Query();
    QueryBuildDataSource    qbds_CustTable;
    SysTableLookup          sysTableLookup;
    QueryBuildRange         qbr;

    if (accountNumLookup != null)
    {
        // Create an instance of SysTableLookup with
        // the current calling form control.

        sysTableLookup = SysTableLookup::newParameters(tablenum(CustTable), accountNumLookup);
        //sysTableLookup.addLookupMethod(
        // Add fields to be shown in the lookup form.
        qbds_CustTable = query.addDataSource(tableNum(CustTable));
         sysTableLookup.addLookupfield(fieldnum(CustTable, CustGroup),true);
        sysTableLookup.addLookupfield(fieldnum(CustTable, AccountNum), false);

        qbr = qbds_CustTable.addRange(fieldNum(CustTable,CustGroup));
        qbr.value("");
        sysTableLookup.parmUseLookupValue(false);

        sysTableLookup.parmQuery(query);

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

    }
}


/// <summary>
///    Builds the dialog.
/// </summary>
/// <remarks>
///    The dialog appears with the parameters.
/// </remarks>

public void build()
{
    SRCustomLookUpContract rdpContract =  this.dataContractObject();

    dialogAccountNum = this.addDialogField(methodstr(SRCustomLookUpContract,parmAccountNum),rdpContract);
    dialogAccountNum.lookupButton(3);

    dialogCustGroup = this.addDialogField(methodstr(SRCustomLookUpContract,parmCustGroup),rdpContract);
    dialogAccountNum.lookupButton(2);
}


private void CustGroupLookup(FormStringControl CustGroupLookup)
{
    Query                   query = new Query();
    QueryBuildDataSource    qbds_CustTable;
    SysTableLookup          sysTableLook;
    QueryBuildRange         qbr;
    CustTable               CustTable;
    if (CustGroupLookup != null)
    {
        // Create an instance of SysTableLookup with
        // the current calling form control.

        sysTableLook = SysTableLookup::newParameters(tablenum(CustTable), CustGroupLookup);
        //sysTableLookup.addLookupMethod(
        // Add fields to be shown in the lookup form.
        qbds_CustTable = query.addDataSource(tableNum(CustTable));
        sysTableLook.addLookupfield(fieldnum(CustTable, CustGroup),false);
         sysTableLook.addLookupfield(fieldnum(CustTable, AccountNum), true);

        qbr = qbds_CustTable.addRange(fieldNum(CustTable,CustGroup));
        qbr.value(dialogAccountNum.value());
        sysTableLook.parmUseLookupValue(false);

        sysTableLook.parmQuery(query);

        // Perform the lookup.
        sysTableLook.performFormLookup();

    }
}


public boolean customerAccModified(FormStringControl _control)
{
    dialogAccountNum.value(_control.valueStr());

    return true;
}


public void postRun()
{
    Dialog dialogLocal = this.dialog();
    DialogField dialogField, dialogField1;

    super();

    // This method should be called in order to handle events on dialogs.
    dialogLocal.dialogForm().formRun().controlMethodOverload(false);

    // Override the methods of department  field.

    dialogField1 = this.bindInfo().getDialogField(this.dataContractObject(), methodstr(SRCustomLookUpContract, parmCustGroup));
    dialogField1.registerOverrideMethod(methodstr(FormStringControl, lookup), methodstr(SRCustomLookupsUIBuilder,CustGroupLookup), this);

    dialogField = this.bindInfo().getDialogField(this.dataContractObject(), methodstr(SRCustomLookUpContract, parmAccountNum));
    dialogField.registerOverrideMethod(methodstr(FormStringControl, lookup), methodstr(SRCustomLookupsUIBuilder,accountNumLookup), this);

}

SQL code to upate one Legal entity banner to all the legal entity in D365

 Hi, update companyimage set  companyimage.Image  = companyimageA.Image  from  ( select Image from companyimage where dataAreaid = 'USF...