Thursday, 5 February 2015

AX 2012 default Print management settings using X++ job to Email?

First get the RECID of the Report for which you want to update the print management settings. Just go to table : PrintMgmtReportFormat and select the report for which you want to import settings. Next get the RecID and replace it in the job below. In my case the RecId is 5637144843. But you need to replace that with your respective RecID.














Next prepare your CSV file, it should look like the following:


Finally use the following job to get the desired settings. Please update the subject as desired. You can also import the email subject from the file but that would require a developer to make the required changes in the job.


static void SOInvoice_Settings(Args _args)
{
#File
    CommaTextIo        commaTextIo;
    FileIOPermission   permission;
    container          containFromRead;
    Query query;
    int                x;
    int                cols;
    int rowNum;

    str Customer,Email;
    int Done,UnDone;

    CustTable custTAble;
    SRSPrintDestinationSettings     destination;
    PrintMgmtSettings               settings1;
    PrintMgmtDocInstance            instance;
    SysDictEnum dictEnum;
    int         enumValue;

    permission = new FileIOPermission(@'C:\Users\Bilal\Desktop\CustEmail.csv',#io_read);


    permission.assert();

    commaTextIo = new CommaTextIO(@'C:\Users\Bilal\Desktop\CustEmail.csv','R');

   // Enter your file path here.

    rowNum = 0;
    Done= 0;
    UnDone= 0;

    containFromRead = commaTextIo.read();

    dictEnum = new SysDictEnum(enumNum(LogisticsElectronicAddressMethodType));

    While(containFromRead)
    {
        if(rowNum>0)
        {
            Customer= any2str(conpeek(containFromRead,1));
            Email= any2str(conpeek(containFromRead,2));

            custTable =CustTable::find(Customer);

    if(custTable)
        {
            destination = new SRSPrintDestinationSettings();
            destination.emailTo(Email);
            destination.printMediumType(SRSPrintMediumType::Email);
            destination.numberOfCopies(1);
            destination.emailSubject("Sales Invoice"); // enter email subject here
            destination.emailAttachmentFileFormat(SRSReportFileFormat::PDF);

            ttsBegin;
            instance = PrintMgmtDocInstance::find(CustTable.RecId, CustTable.TableId, PrintMgmtNodeType::CustTable, PrintMgmtDocumentType::SalesOrderInvoice, 1);

            if(instance)
                instance.selectForUpdate(true);

            instance.DocumentType = PrintMgmtDocumentType::SalesOrderInvoice;
            instance.NodeType = PrintMgmtNodeType::CustTable;
            instance.PrintType = PrintMgmtDocInstanceType::Original;
            instance.PriorityId = 1;
            instance.ReferencedTableId = CustTable.TableId;
            instance.Suppress = NoYes::Yes;
            instance.ReferencedRecId =custTable.RecId ;

            if(instance)
                instance.update();
            else
                instance.insert();

            select firstOnly settings1 order by PriorityId desc where settings1.ParentId == instance.RecId ;

            query = new Query(queryStr(CustInvoiceJour));
            Query.dataSourceTable(tableNum(CustInvoiceJour)).addRange(fieldNum(CustInvoiceJour, InvoiceAccount)).value(CustTable.AccountNum);
            Query.pack();

            settings1.ParentId = instance.RecId;
            settings1.ReportFormat = 5637144843; // enter the recid of the email format here.
            settings1.PrintJobSettings = destination.pack();
            settings1.PriorityId = settings1.PriorityId + 1;
            settings1.NumberOfCopies = 1;
            settings1.Description = strFmt('%1  %2', CustTable.AccountNum, CustTable.name());
            settings1.QueryPacked = Query.pack();
            settings1.insert();

            ttsCommit;
            Done++;
        }
        else
        {
            info("Customer not found " + Customer);
            UnDone++;
        }

        }
        containFromRead = commaTextIo.read();
        rowNum++;
    }
    commaTextIo = null;

    info(strFmt("Inserted : %1 Not inserted: %2", Done, UnDone));
}

Friday, 30 January 2015

AX 2012 - how to evaluate mathematical expressions using code?

Many times its required to evaluate a mathematical expression using the order of execution.
For example, we have a string '((2+3)*4)'. We want to evaluate this expression and return the result.
Such expressions can be validated and evaluated using the X++ class 'XPPCompiler'.




Following is the code snippet:


static void calculateExpression(Args _args)
{
    XppCompiler comp;
   
    str expr;
   
    expr = "(((1+2)*6)/5)";
   
    comp = new XppCompiler();
   
    info(strFmt('%1',comp.compileExpr(expr)));//this validates the expression
   
    info(strFmt('%1',comp.execute(expr)));//this returns the result.
}

AX 2012, X++ expression, BODMAS in AX 2012, validate mathematical expressions, validate formula in AX, formula validation, XPPCompiler, Dynamics AX 2012, Formula calculation, string to mathematical expression, Paranthesis in AX 2012

Tuesday, 24 June 2014

Select the batch tab in dialog by default using SysOperation controller class

Hi,
Many times we have use sysOperation framework without using any data contract. In that case, if you run the class to open the dialog you would see a blank tab. To select the batch tab by default just place this code in your controller class.

protected void dialogPostRun()
{
    sysOperationDialog sysOperationDialog;
    DialogTabPage batchTab;
    FormRun formRun;

    super();

    sysOperationDialog = this.dialog() as SysOperationDialog;

    formRun = sysOperationDialog.formRun();

    batchTab = sysOperationDialog.batchDialogTabPage();

    formRun.selectControl(batchTab.control());
}

Wednesday, 18 June 2014

Generate Excel Template of AX 2012 tables-all columns

Hi, This job can be used in data migration when your client asks for a template of a particular table.

static void Wipfli_TemplateGenerator(Args _args)
{
    SysExcelWorksheetHelper worksheetHelper;
    SysExcelHelper          sysExcelHelper;
    SysExcelWorksheet       worksheet;
    int                     column = 1;
    int                     row = 1;
    str                     worksheetName;
    SysDictField            dictField;
    SysExcelCell            excelCell;
    int                     redColor = WinAPI::RGB2int(255, 0, 0);
    SysExcelRange           range;
    COMVariant              cellValue = new COMVariant(COMVariantInOut::Out);
    DictTable DictTable;

    int fieldCount, _x, fieldId,enumValues, enumValue;

    sysExcelHelper = SysExcelHelper::construct();

    sysExcelHelper.initialize();

    worksheet = sysExcelHelper.addWorksheet("Template");
    worksheetHelper = SysExcelWorksheetHelper::construct(worksheet);

    DictTable = new DictTable(tableNum(InventTable));// Use your table name here.

    fieldCount = DictTable.fieldCnt();
    fieldId = DictTable.fieldNext(0);

    while (fieldId)
    {
      DictField = DictTable.fieldObject(fieldId);
      worksheetHelper.addColumn(column, dictField.name(),Types::AnyType);

      fieldId = DictTable.fieldNext(fieldId);
      column++;
    }

    sysExcelHelper.launchExcel();

}

Thursday, 29 May 2014

Purchase order- line number increments

  1. Click System administration > Setup > System parameters.
  2. Click General, and then select options for the general system parameters:
    • Increment – Enter the increment to use when new line numbers are assigned to purchase order lines. This increment is used for all purchase order lines. Line increments are applied for purchase orders only.

Monday, 26 May 2014

Make update changes on Packing slip post?

Many times we are required to make certain changes right after the packing slip has been posted. To do that, use the EndPost() method in the SalesPackingSlipJournalPost class. This method is called after the packing slip is successfully posted.

Friday, 16 May 2014

Job to fetch entry points for a Role in AX 2012 security

static void JobSecurityRoleEntryPoints(Args _args)
{
  SecurityTaskEntryPoint  taskEntryPoint;
    SecurityRole            role;
    SecurityRoleTaskGrant   taskGrant;
    SecuritySubTask         subTask;
    SecurityTask            privilege;
    SecurityTask            securityTask;
    SecurableObject         securableObject;
    RecId                   roleRecId;

    #AOT
    str userlicense, maintainlicense, privName;
    TreeNode treeMenuItem;
    TreeNode treeMenu;

    while select RecId, Name from role where role.AotName == 'TradeCustomer'
    {
        roleRecId = role.RecId;

        while select securityTask, SecurityRole from taskGrant
            join RecId, Type, AOTName from securitytask where securityTask.RecId == taskGrant.SecurityTask
                && taskGrant.SecurityRole == roleRecId && securitytask.Type == SecurityTaskType::Duty
            join securityTask, SecuritySubTask from subTask where subTask.SecurityTask == securityTask.RecId
            join securityTask, EntryPoint from taskEntryPoint where taskEntryPoint.SecurityTask == subtask.SecuritySubTask

            {
               select RecId, Type, Name from securableObject
                    where securableObject.RecId == taskEntryPoint.EntryPoint && (securableObject.Type == SecurableType::MenuItemDisplay
                        || securableObject.Type == SecurableType::MenuItemAction || securableObject.Type == SecurableType::MenuItemOutput);

                if( securableObject)
                {
                    select RecId, Type, AOTName from privilege where privilege.RecId == subTask.SecuritySubTask && SecurityTaskType::Privilege == privilege.Type;

                    privName = privilege.AotName;

                    info(strFmt("Role: %1   Duty: %2   Priv:  %3   Menu item : %4", role.Name,securitytask.AotName,privName,securableObject.Name));

                }
        }
       
        //sometimes a role has a privielge direclty assigned instead of a duty. So this code is for those privileges. In this case duty will not exist.
        while select SecurityTask, SecurityRole from taskGrant
            join RecId, Type, AOTName from securitytask where securityTask.RecId == taskGrant.SecurityTask
                 && taskGrant.SecurityRole == roleRecId && securitytask.Type == SecurityTaskType::Privilege
            join securityTask, EntryPoint from taskEntryPoint where taskEntryPoint.SecurityTask == securitytask.RecId

            {
                select RecId, Type, Name from securableObject
                    where securableObject.RecId == taskEntryPoint.EntryPoint && (securableObject.Type == SecurableType::MenuItemDisplay
                        || securableObject.Type == SecurableType::MenuItemAction || securableObject.Type == SecurableType::MenuItemOutput);

                if( securableObject)
                {
                   info(strFmt("Role: %1   Duty:   Priv:  %2   Menu item : %3", role.Name,securitytask.AotName,securableObject.Name));
                }
        }
    }
}