Wednesday, 29 January 2014

How To Get Date A Year Before & A Year After in AX 2012

To get date after 1 year from a date in Microsoft Dynamics AX, we can use function "nextYr(date _date)" and we can use function "prevYr(date _date)" to get 1 year before.

Static Void PrevNextYear_test()
{
    date      testDate = str2date("30/1/2014", 123);
    ;
    info(strfmt("This Date : %1, 1 Year After : %2", testDate, nextYr(testDate)));
    info(strfmt("This Date : %1, 1 Year Before : %2", testDate, prevYr(testDate)));
}

If we take start date from user and End Date comes a year after then.
Write Following code in AOT > Forms > FormName > Design > Design > Start Date > modified()
Set Auto Declaration property to Yes.

public boolean modified()
{
    boolean ret;
    ret = super();
    TableName.EndDate = str2date(StartDateControlName.valueStr(), 1234);
    TableName.EndDate = nextYr(TableName.EndDate)-1;
    return ret;
}

Filter Records Based On Unbound Controls in Ax 2012

1. In the AOT, expand Forms, and then expand the form where you want to add fields to the filter group.
2. Expand Designs, expand Design,  click New Control, and then click the type of control you want to use. The control is         added to the group.
3. Expand the control node, right-click Methods, click Override methods, and then click LookUp. 
public void lookup()
{
    QueryBuildDataSource qbds;
    Query query = new Query();
    SysTableLookup sysTableLookup;
    super();
    sysTableLookup = SysTableLookup::newParameters(tableNum(Table1),this);
    qbds = query.addDataSource(tableNum(Table1));
    sysTableLookup.addLookupfield(fieldNum(Table1, RelatedId));
    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();
}
In the same Control, right-click the Methods, click Override method, and then click modified
public boolean modified()
{
    boolean ret;
    ret = super();
    Table1_ds.executeQuery();
    return ret;
}

4. Expand the Methods node of the form, right-click classDeclaration,
public class FormRun extends ObjectRun
{
    QueryFilter queryFilter;
}
5. Expand the Data Sources node, and then find the table that has the field that has the      values that you will use to filter the list.
6. Expand the table node, right-click Methods, click Override method, and then click init. 
public void init()
{
    super();
    queryFilter = Table1_ds.query().addQueryFilter(Table1_ds.queryBuildDataSource(),"relatedID");
}
In the same table, right-click the Methods, click Override method, and then click executeQuery
public void executeQuery()
{
    queryFilter.value(element.design().controlName("StringEdit").valueStr());
    super();
}

Tuesday, 21 January 2014

Based on Company salesid has to change through X++ in ax 2012

class SalesIdLookup extends RunBase
{
   FormStringControl    companyIdCtrl,salesIdCtrl;
}

protected Object dialog(DialogRunbase dialog, boolean forceOnClient)
{
    DialogRunBase dialogRunBase;
    ;
    dialogRunBase = super(dialog, forceOnClient);

    companyIdCtrl = dialogRunBase.formBuildDesign().addControl(FormControlType::String,"CompanyId");
    companyIdCtrl.extendedDataType(71);

    salesIdCtrl = dialogRunBase.formBuildDesign().addControl(FormControlType::String,"SalesId");
    salesIdCtrl.extendedDataType(473);
    return dialogRunBase;
}

public void dialogPostRun(DialogRunbase dialog)
{
    ;
    super(dialog);
    dialog.dialogForm().formRun().controlMethodOverload(true);
    dialog.dialogForm().formRun().controlMethodOverloadObject(this);

    companyIdCtrl = dialog.dialogForm().formRun().design().controlName("CompanyIdCtrl");
    salesIdCtrl   = dialog.dialogForm().formRun().design().controlName("salesIdCtrl");
}

public container pack()
{
    return connull();
}

void salesIdLookup()
{
    Query               query = new Query();
    SysTableLookup      sysTableLookup = SysTableLookup::newParameters(      tablenum(SalesTable),salesIdCtrl);
    ;

    sysTableLookup.addLookupfield(fieldNum(SalesTable,SalesId));
    sysTableLookup.addLookupfield(fieldNum(SalesTable,SalesName));

    query.addDataSource(tableNum(SalesTable));
    query.allowCrossCompany(true);
    query.addCompanyRange(companyIdCtrl.text());

    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();
}

public boolean unpack(container packedClass)
{
    return true;
}

public static void main(Args args)
{
    SalesIdLookup   sil = new SalesIdLookup();
    ;

    if(sil.prompt())
    {
        sil.run();
    }
}

Generate Text File through the selected records in ax 2012

void clicked()
{
    LedgerJournalTrans    ledgerJournalTrans1;
    BinData                    binData;
    TextBuffer            textBuffer;
    CustTable             custtable;
    ;
    textBuffer = new TextBuffer();
    textBuffer.setText('');
    ledgerJournalTrans1 = LedgerJournalTrans_ds.getFirst(1);
    while(ledgerJournalTrans1)
    {
        textBuffer.appendText(strfmt(' %1 %2 %3 %4 %5 %6 \r\n',DimensionAttributeValueCombination::find(LedgerJournalTrans.LedgerDimension).DisplayValue,
                                                              ledgerJournalTrans1.Txt,
                                                              ledgerJournalTrans1.AmountCurDebit,
                                                              ledgerJournalTrans1.AmountCurCredit,
                                                              ledgerJournalTrans1.OffsetAccountType,
                                                              DimensionAttributeValueCombination::find(LedgerJournalTrans.OffsetLedgerDimension).DisplayValue));
        ledgerJournalTrans1 = LedgerJournalTrans_ds.getNext();
    }
    textBuffer.getText();
    binData = new BinData();
    binData.setStrData(textBuffer.getText());
    binData.saveFile(@"d:\CustomerPaymentDetails.txt");
    info("Text File has been generated in 'D' Drive with CustomerPaymentDetails name");

}

Monday, 20 January 2014

how to split purchase order through company wise in ax 2012

void clicked()
{
    Dialog      dialog;
    DialogField SelectedOrder, SelectedCompany, SelectedQuantity;
    PurchTable purchTable,purchTablelocal,purchTableMain;
    PurchLine  LocPurchLine,purchLine,purchlinelocal;
    VendTable       vendTable1 ;
    AxPurchTable    axPurchTable;
    AxPurchLine     axPurchLine;
    PurchFormLetter purchFormLetter;
    NumberSeq      numberSeq;
    str         PurchId, compny, p_order,ordr_act,currency,vend_group,language,name,item,item_name,unit;
    real qty,price,net_amt,p=0,q=0;

    container  othercompany;
    ;
    super();
    purchTable = PurchaseTable_ds.getFirst(1);
    PurchId    = purchTable.PurchId;
    ordr_act    = purchTable.OrderAccount;
    currency  = purchTable.CurrencyCode;
    vend_group = purchTable.VendGroup;
    language = purchTable.LanguageId;
    name = purchTable.PurchName;
    select ItemId,PurchPrice,PurchUnit,LineAmount from purchLine join * from purchTable where purchTable.PurchId==purchLine.PurchId;
    item = purchLine.ItemId;
    price = purchLine.PurchPrice;
    unit = purchLine.PurchUnit;
    //net_amt = purchLine.LineAmount;
    //item_name = purchLine.itemName();

    dialog = new Dialog("Split Purchase Order");
    selectedOrder = dialog.addFieldValue(extendedTypeStr(PurchId),PurchId, "Selected Purchase Order ");
    dialog.addText("Select New Company ");
    SelectedCompany= dialog.addField(extendedTypeStr(SelectableDataArea));
    dialog.addText("Enter new quantity");
    SelectedQuantity = dialog.addField(extendedTypestr(PurchOrderedQty));

    dialog.run();
    if (dialog.closedOk())
    {
        info(selectedOrder.value());
        info(SelectedCompany.value());
        info(SelectedQuantity.value());

        qty = any2real(SelectedQuantity.value());
        compny = any2str(SelectedCompany.value());
        p_order = any2str(selectedOrder.value());

        ttsbegin;
        changeCompany(compny)
        {
            purchTable   = null;
            purchLine   = null;
            //purchTablelocal = null;
            // numberSeq = NumberSeq::newGetNumFromCode(purchParameters::numRefPurchaseOrderId().NumberSequence,true);
             select forUpdate purchTablelocal  where purchTablelocal.PurchId ==selectedOrder.value()&& purchTablelocal.dataAreaId==SelectedCompany.value();
            if(!purchTablelocal.RecId)
            {
                purchTablelocal.initValue();
                purchTablelocal.PurchId = p_order;                     //numberSeq.num();
                purchTablelocal.OrderAccount = ordr_act;
                purchTablelocal.InvoiceAccount=ordr_act;
                purchTablelocal.CurrencyCode=currency;
                purchTablelocal.VendGroup= vend_group;
                purchTablelocal.PurchName=name;
                purchTablelocal.LanguageId=language;
                purchTablelocal.initFromVendTableMandatoryFields();
                purchTablelocal.insert();
                info(strFmt("Created a purchase order %1 in %2 Company",purchTablelocal.PurchId,SelectedCompany.value()));
            }
            select forUpdate LocPurchLine  where LocPurchLine.PurchId ==selectedOrder.value()&& LocPurchLine.dataAreaId==SelectedCompany.value();
            if(!LocPurchLine.RecId)
             {
                LocPurchLine.PurchId = purchTablelocal.PurchId;
                LocPurchLine.ItemId = item;
                LocPurchLine.itemName();
                LocPurchLine.PurchQty = qty;
                LocPurchLine.PurchUnit = unit;
                LocPurchLine.VendGroup =  vend_group;
                LocPurchLine.PurchPrice = price;
                LocPurchLine.LineAmount = qty*price;
                //LocPurchLine.createLine(true, true, true, true, true, false);
                LocPurchLine.insert();

            }

        }
        ttscommit;

        changeCompany('USMF')
        {

            ttsBegin;
            select forUpdate purchlinelocal  where purchlinelocal.PurchId == selectedOrder.value();

            {

                   if(purchlinelocal.RecId)
                {
                   p = purchlinelocal.PurchQty ;
                   q = p - qty ;
                   purchlinelocal.PurchQty = q ;
                   purchlinelocal.LineAmount = q*price;
                   purchlinelocal.update();
                }
            ttsCommit;
            }

        }

    }
 }

How to send email through ax 2012

public void mail()
        {
            CustComlaint       _custComlaint;
            sysMailer          sysmailer;
            Email              emailID;
            ID                 id;
            sysemailparameters parameter = sysemailparameters::find();
            InteropPermission  _interopPermission;
            ;
            sysmailer = new sysmailer();
            _interopPermission = new InteropPermission(InteropKind::ComInterop);
            _interopPermission.assert();
            select reverse _custComlaint;
                ID      = _custComlaint.ID;
                emailID = _custComlaint.Email;
            sysmailer.quickSend("anant.dubey@s3infotech.in",
                                 emailID,
                                 "Complaint Request",
                                 strFmtLB("Dear Sir, your complaint has been received & your complaint number is" +"-"+strFmt('%1',id)));
}

Update enum field through dialog enum

void clicked()
{
Dialog          dialog;
HcmWorker       hcmWorker1;
dialog = new Dialog("Select value to Update field");
dialog.addText("Update Worker");
field = dialog.addField(enumStr(UpdateWorker));
dialog.run();
if (dialog.closedOk())
{
hcmWorker1 = HcmWorker_ds.getFirst(1);
while (hcmWorker1)
{
hcmWorker1.UpdateWorker = field.value();
hcmWorker1.update();
hcmWorker1 = HcmWorker_ds.getNext();
}
}
}