Monday, July 31, 2017

Update cross reference in batch in Dynamics AX 2012

static void UpdateCrossRefBatch(Args _args)
{
    ;

    xRefUpdate::truncateXrefTables();//will drop all data from cross references table
    xRefUpdateIL::updateAllXref(true, false, true);//will kick off batch job to rebuild it
    info("Done, cross reference update batch job created.");
}

Sunday, July 30, 2017

shut down AX using X++ code


static void ShutDownAxapta()
{
    SysGlobalCache  cache = appl.globalCache();
    info            info;
    ;

    cache.set(classstr(info), identifierstr(Autologoff), true);

    info = new info();
    info.shutDown(true);
}

Friday, July 28, 2017

Send Mail From AX in GMail or Outlook Server from X++

In X++ there are many functionality to send mail from X++. Dynamics AX Are very Use full in Entity Relation Ship Programming. Here i Will Create A Job in AX 2012 How to Send Mail From AX in G Mail or Outlook Server. Here you can Add Attachment, or Else you can Put your Friends in CC, Give Subject Name, Send Body Of Code or Content in AX.


static void mail_from_ax(Args _args)
{
    Description255             recipientEmail;
    Notes                            emailBody;
    Description255             subjectText;
    Filename                       fileName;
    SmmOutlookEmail       smmOutlookEmail = new SmmOutlookEmail();
    recipientEmail = "RecipientEmail@gmail.com";
    subjectText     = "Test Email";
    // fileName          = @"C:\Users\admin\Desktop\mypic.jpg";
    emailBody       = "Hi,\nThis is a test email for Dyanmics AX.\nThanks.";
    if (smmOutlookEmail.createMailItem())
    {
        smmOutlookEmail.addEMailRecipient(recipientEmail);
        smmOutlookEmail.addSubject(subjectText);
        //smmOutlookEmail.addFileAsAttachment(fileName);
        smmOutlookEmail.addBodyText(emailBody);
        smmOutlookEmail.sendEMail(smmSaveCopyOfEMail::No,false);
        //smmOutlookEmail.sendEMail(smmSaveCopyOfEMail::Yes,NoYes::Yes);
        Box::info("Mail Send Successfully !!");
    }
    else
    {
        error("Could not communicate with Microsoft Outlook Client.");
    }

}

Create Auto number from table method in ax 2012

In this article we will see about how to create auto number sequence when we create row in table. Here is the example to create auto number sequence while create row in table.

public void initValue()
{
    str                _newCode;
    int                max1;
    TaskOne      taskOne;
    super();
    select firstOnly SerialNo from taskOne order by RecId desc;
    if(!taskOne.SerialNo)
    {
        ttsBegin;
        _newCode=strRFix(int2str(1),4, "0");
        this.SerialNo = _newCode;
        ttsCommit;
    }
    else
    {
        ttsBegin;
        max1=str2int(subStr(taskOne.SerialNo,4,4))+1;
        this.SerialNo =strRFix(int2str(max1),4, "0");
        ttsCommit;
    }
}

How to use sorting in the select statements in Dynamics Ax

By default, a while select statement that returns multiple rows will sort the result ascending by the primary index on the table. You can see this in the first two examples in the previous section.
If you would like to have a statement return the rows in a different order, you have to use the order by parameter in the select statement and specify which fields you would like to sort the result by. If you have an index that corresponds with the sorting, you can use the name of the index to order by as well, but then you will have to use the index statement instead of order by. The following example will return all the records in the CarTablesorted in a descending order of mileage:

static void selectRecordsSortedDesc(Args _args)
{
    CustTable custtable;
    int records;
    info("------------------START-------------------");
    while select custtable
        order by AccountNum desc
    {
       info("--------------NEW RECORD--------------");
       info (strfmt("AccountNum: %1",custtable.AccountNum));
       info (strfmt("Name: %1",custtable.name()));
       info (strfmt("CustGroup: %1",custtable.CustGroup));
       info (strfmt("Currency: %1",custtable.Currency));
     
       records++;
    }
    info("------------------END-------------------");
    info(strfmt("%1 records was selected", records));
}

Wednesday, July 26, 2017

If you want to Open Word file through Job in Ax you can user COM variant and open word file through Job. Using COM variant or dialog Field, Word Documents through we can achieve this functionality in ax 2012.



Static void OpenWordTemplate()
{
    Dialog                       d;
    DialogField              dialogFilename;
    COM                         wordApp;
    COM                         wordDocuments;
    FileName                    fileName;
    wordApp = new COM("word.application");
    wordDocuments = wordApp.Documents();

    d = new Dialog();
    d.caption("select a file");
    dialogFilename = d.addField(extendedTypeStr(FilenameOpen),"File Name");//add a field where you select your file in a specific path
    d.run();//execute dialog
    if(d.closedOk())
    {
        filename = dialogFileName.value();//return path file value
    }
    try
    {
        wordDocuments.Open(fileName);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }
    wordApp.visible(true);
}
Happy DAX....

D365 Extensions: Extend the validateField Method on a Table

In this post I will going to show you, how to extend the validateField method on a table. As example I used the SalesLine, where I will ...