Tuesday, 22 November 2016

How to mark Vendor open settlement transaction using x++ code in Ax 2012

public void settlementnew(InvoiceId   _invoice)
{
    custvendopentransmanager            manager;
    VendTransOpen                       vendTransOpen;
    VendTrans                           vendTrans;
    ExchangeRateHelper                  exchangeRateHelper;
    AmountCur                           totalSettlement;

    //To mark particular invoice based on Acc num
    select vendtransopen where vendTransOpen.AccountNum ==AccountNumb
                join vendTrans where vendTrans.Invoice == _invoice
                     && vendTrans.RecId == vendtransopen.RefRecId
                    && vendTrans.AccountNum == vendTransOpen.AccountNum;
   
    if(vendtransopen)
    {
        //To check transaction line which we want to settle
        manager = custvendopentransmanager::construct(ledgerJournalTrans);
        manager.updateTransMarked(vendTransOpen,true);
   
        //To get total settlement
        exchangeRateHelper = ExchangeRateHelper::newCurrency(Ledger::primaryLedger(CompanyInfo::findDataArea(ledgerJournalTrans.Company).RecId),ledgerJournalTrans.CurrencyCode);
        totalSettlement    = SpecTransManager::getTotalSettleAmountForSpecReference(
                                ledgerJournalTrans.Company,
                                ledgerJournalTrans.TableId,
                                ledgerJournalTrans.RecId,
                                ledgerJournalTrans.CurrencyCode,
                                ledgerJournalTrans.Company,
                                ledgerJournalTrans.TransDate,
                                exchangeRateHelper.prepareExchangeRateForStorage(ledgerJournalTrans.crossrate()));
        //To update in ledgerJournal trans
        ttsBegin;
        ledgerJournalTrans.selectForUpdate(true);
        ledgerJournalTrans.AmountCurDebit   = abs(totalSettlement);
        ledgerJournalTrans.SettleVoucher    = SettlementType::SelectedTransact;
        ledgerJournalTrans.update();
        ttsCommit;
    }

}

http://www.andesoft.net/automatic-mark-settlement-transactions-payment-journal-ax-2012/

Wednesday, 16 November 2016

How to save report using x++ code ax 2012

static void generatePDF(Args _args)
{
    SrsReportRunController          projInvoiceController = new SrsReportRunController();
    //ProjInvoiceContract             projInvoiceContract   = new ProjInvoiceContract();
    PSAProjInvoiceContract          projInvoiceContract = new PSAProjInvoiceContract();
    SRSPrintDestinationSettings     settings;
    SrsReportEMailDataContract      emailContract;
    Args                            args = new Args();
    str                             ReportPath;
    ReportPath = 'C:\\ProjectInvoice.pdf';

    select firstOnly projInvoiceJour;
    args.record(projInvoiceJour);

    projInvoiceController.parmReportName(ssrsReportStr(PSAProjInvoice, Report));
    projInvoiceController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
    projInvoiceController.parmShowDialog(false);

    projInvoiceContract.parmProjInvoiceJourRecId(projInvoiceJour.RecId);
    projInvoiceContract.parmReportTitle("Invoice");
    projInvoiceContract.parmCountryRegionISOCode(SysCountryRegionCode::countryInfo());
    projInvoiceController.parmArgs(args);

    projInvoiceController.parmReportContract().parmRdpContract(projInvoiceContract);

    // Change print settings as needed
    settings = projInvoiceController.parmReportContract().parmPrintSettings();
    settings.printMediumType(SRSPrintMediumType::File);
    settings.fileFormat(SRSReportFileFormat::PDF);
    settings.overwriteFile(true);
    settings.fileName(ReportPath);

    // Execute the report
    projInvoiceController.runReport();

}

how to send email with attachment using x++ code
https://blogs.msdn.microsoft.com/dynamicsaxbi/2012/03/09/how-to-directing-reports-to-email/ 

Tuesday, 15 November 2016

How to get exchange rate using x++ code in ax 2012

static void exchangeRate(Args _args)
{
    ExchangeRateHelper            exchangeRateHelper;
    TransDate      transdate;      
    CurrencyCode    currencycode = "USD";
    CurrencyExchangeRate    ex1,ex2;
 
    transdate = mkDate(16,11,2016);
 
    exchangeRateHelper = exchangeRateHelper::newExchangeDate(Ledger::current(),currencycode,transdate);
    ex1 = exchangeRateHelper.getExchangeRate1();
    ex2 = exchangeRateHelper.getExchangeRate2();
 
    info(strFmt("%1 - %2",ex1,ex2));
}

public real balanceInINR()
{
    ExchRate                   exchRate_abc,exchRates_IN;
    ExchangeRate               exchangeratetab;
    ExchangeRateCurrencyPair   exchangeratecurrecypair;
    real s,p,BalanceINR;

    if(Curext() != "HMIN")
    {
          s = amountCurOpen;//CustTrans.RemainAmountMST();//tmpAccountSum.Balance01;

        select validtimestate(exchRateDate) exchangeratetab order by ValidFrom desc
                                where exchangeratetab.ValidFrom   <=  exchRateDate
            join exchangeratecurrecypair where exchangeratecurrecypair.RecId         == exchangeRatetab.ExchangeRateCurrencyPair
                                        && exchangeratecurrecypair.FromCurrencyCode == CompanyInfo::standardCurrency()
                                        && exchangeratecurrecypair.ToCurrencyCode   == "INR";
        if(exchangeratetab.RecId)
        {
            exchRate_abc = exchangeratetab.ExchangeRate/100;
        }
        else
        {
            select validtimestate(exchRateDate) exchangeratetab order by ValidFrom desc
                                where exchangeratetab.ValidFrom   <=  exchRateDate
                join exchangeratecurrecypair where exchangeratecurrecypair.RecId         == exchangeRatetab.ExchangeRateCurrencyPair
                                        && exchangeratecurrecypair.FromCurrencyCode == "INR"
                                        && exchangeratecurrecypair.ToCurrencyCode   == CompanyInfo::standardCurrency();//CompanyInfo::findByCompany_IN(companyDomainList.companyId).HM_standardCurrency() ;
            if(exchangeratetab.RecId)
            {
                exchRate_abc = exchangeratetab.ExchangeRate/100;
            }
        }
        BalanceINR = s*exchRate_abc ;

        return s*exchRate_abc;
    }
    else
    {
        if(custtrans.CurrencyCode == "INR")
        {
            BalanceINR = amountCurOpen;//CustTrans.RemainAmountMST();//tmpAccountSum.Balance01;
        }
        else
        {

            select validtimestate(exchRateDate) exchangeratetab order by ValidFrom desc
                            where exchangeratetab.ValidFrom   <=  exchRateDate
            join exchangeratecurrecypair where exchangeratecurrecypair.RecId         == exchangeRatetab.ExchangeRateCurrencyPair
                                    && exchangeratecurrecypair.FromCurrencyCode == custtrans.currencycode
                                    && exchangeratecurrecypair.ToCurrencyCode   == "INR";

            if(exchangeratetab.RecId)
            {
               exchRates_IN =exchangeratetab.ExchangeRate/100;
               //BalanceINR = /*tmpAccountSum.Balance01Cur*/CustTrans.RemainAmountCur() *exchRates_IN;//commented by raj
                BalanceINR = amountCurOpen*exchRates_IN;
            }
            else
            {
                select validtimestate(exchRateDate) exchangeratetab order by ValidFrom desc
                            where exchangeratetab.ValidFrom   <=  exchRateDate
                join exchangeratecurrecypair where exchangeratecurrecypair.RecId         == exchangeRatetab.ExchangeRateCurrencyPair
                                    && exchangeratecurrecypair.FromCurrencyCode == "INR"
                                    && exchangeratecurrecypair.ToCurrencyCode   == custtrans.currencycode;
                exchRates_IN =exchangeratetab.ExchangeRate/100;
                //BalanceINR = /*tmpAccountSum.Balance01Cur*/CustTrans.RemainAmountCur() *exchRates_IN;//commented by raj
                BalanceINR = amountCurOpen *exchRates_IN;
            }
        }
         return BalanceINR;
    }
}



public real balanceInUSD()
{
    ExchangeRate                    exchangeratetab;
    ExchangeRateCurrencyPair        exchangeratecurrecypair;
    ExchRate                        exchRate_abc,exchRate_usd,exchRates1;
    real                            s ,TestBalance,BalanceInUSD ;
    // ExchRates   exchRatesz,ExchRates;

    if(custtrans.CurrencyCode != "USD")
    {
        exchRate_usd =1;

        select validtimestate(exchRateDate)  exchangeratetab order by ValidFrom desc
                                where exchangeratetab.ValidFrom   <=  exchRateDate
            join exchangeratecurrecypair where exchangeratecurrecypair.RecId         == exchangeRatetab.ExchangeRateCurrencyPair
                                        && exchangeratecurrecypair.FromCurrencyCode == custtrans.currencycode
                                        && exchangeratecurrecypair.ToCurrencyCode   == "USD";
        if(exchangeratetab.RecId)
        {
            exchRate_usd = (exchangeratetab.ExchangeRate/100);
        }
        else
        {
            exchangeratetab.clear();
            exchangeratecurrecypair.clear();
            select validtimestate(exchRateDate) exchangeratetab order by ValidFrom desc
                                where exchangeratetab.ValidFrom   <=  exchRateDate
            join exchangeratecurrecypair where exchangeratecurrecypair.RecId         == exchangeRatetab.ExchangeRateCurrencyPair
                                        && exchangeratecurrecypair.FromCurrencyCode == "USD"
                                        && exchangeratecurrecypair.ToCurrencyCode   ==  custtrans.currencycode;
            if(exchangeratetab.RecId)
            {
                exchRate_usd = (exchangeratetab.ExchangeRate/100);
                return amountCurOpen/exchRate_usd;//CustTrans.RemainAmountCur()/exchRate_usd;
            }
            else
            {
                exchRate_usd = 0;
            }

        }

        if(custTransOpenPerDateTmp.HM_BalanceInINR !=0)
        {
            BalanceInUSD = amountCurOpen*exchRate_usd;//CustTrans.RemainAmountCur()*exchRate_usd; //CustTrans.RemainAmountCur()*exchRate_usd;//BalanceINR/exchRate_usd;
        }
        else
        {
            BalanceInUSD = 0;
        }
    }
    else if(custTrans.CurrencyCode == "USD")
    {
        BalanceInUSD = amountCurOpen;//CustTrans.RemainAmountCur();/*tmpAccountSum.Balance01Cur*/
    }
    return balanceInUSD;
}

Friday, 11 November 2016

How to get ledger dimension values individually using x++ code in Ax 2012

static void getLedgerDimension(Args _args)
{
    GeneralJournalAccountEntry     generalJournalAccountEntry;
    DimensionAttributeLevelValueAllView dimAttrView; //View that will display all values for ledger dimensions
    DimensionAttribute dimAttr; //Main dimension attribute table
 

    while select generalJournalAccountEntry
    {
     
        select DisplayValue from dimAttrView where dimAttrView.ValueCombinationRecId ==                                 generalJournalAccountEntry.LedgerDimension   //5637149079
                join BackingEntityType from dimAttr
            where dimAttr.RecId == dimAttrView.DimensionAttribute
                && dimAttr.Name == "BusinessUnit";
           //  && dimAttr.Name == "Project";
           //  && dimAttr.Name == "Cutomer";
    //{
           if(dimAttrView.DisplayValue)
           info(strFmt("Business Unit: %1", dimAttrView.DisplayValue));
        }
    }
}

Another way
static void getDimensionValue(Args _args)
{
    DimensionAttributeValueSetStorage    dimStorage;
    Counter                              i;
    str                                  BusUnit;
    ;

    dimStorage = DimensionAttributeValueSetStorage::find(5637147906); //default dimension value

    for (i=1 ; i<= dimStorage.elements() ; i++)
    {
        if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "BusinessUnit")
        {
            BusUnit = dimStorage.getDisplayValueByIndex(i);
        }
    }
    //return BusUnit;
    info(strFmt("%1",BusUnit));


}



Another way

OMOperatingUnit                         omOperatingUnit;
DimensionFinancialTag                   dimFinancialTag;
DefaultDimensionView                    defaultDimensionVWBU, defaultDimensionVWLOB;
ProjTable                                         dimProjTable;


defaultDimensionVWBU.clear();
                 
select crossCompany defaultDimensionVWBU
                        where defaultDimensionVWBU.DefaultDimension == dimProjTable.DefaultDimension
                            && defaultDimensionVWBU.Name      == "BU"
                            && defaultDimensionVWBU.DisplayValue  == omOperatingUnit.OMOperatingUnitNumber;
                 
                    defaultDimensionVWLOB.clear();
                    select crossCompany defaultDimensionVWLOB
                        where defaultDimensionVWLOB.DefaultDimension == dimProjTable.DefaultDimension
                            && defaultDimensionVWLOB.Name      == "LOB"
                            && defaultDimensionVWLOB.DisplayValue  == dimFinancialTag.Value;

How to find missing Cost Centre dimension values for Sales Orders in D365 F&O

class ExportSalesOrderCostCentreMissing

{

    public static void main(Args _args)

    {

        DimensionAttributeValueSetStorage   dimStorage;

        SalesTable                          salesTable;

        Counter                             i, totalCount;

        boolean                             costCenterFound;

       

        while select SalesId, DefaultDimension,CustAccount, SalesType, CustomOrderStatus,RetailChannelTable from salesTable

            where salesTable.SalesStatus != SalesStatus::Invoiced

            &&  salesTable.DataAreaId    == 'uk01'           

        {

            dimStorage          = DimensionAttributeValueSetStorage::find(salesTable.DefaultDimension);

            costCenterFound     = false;

            for (i = 1 ; i <= dimStorage.elements() ; i++)

            {

                if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "CostCentre")

                {

                    costCenterFound = true;

                }

            }

            if (costCenterFound == false)

            {

                Info(strFmt("Sales Id %1 Customer account %2 Order type %3 Channel %4 Twc status %5", salesTable.SalesId, salesTable.CustAccount, enum2Str(salesTable.SalesType), salesTable.getRetailChannelName(), salesTable.CustomOrderStatus));

                totalCount++;

            }      

        }

    }

}

Calculate tax for project invoice using x++ code Ax 2012

static void projectinvoiceTax(Args _args)
{
    ProjInvoiceJour         projInvoiceJour;
    TmpTaxWorkTrans         taxWorkTrans;
    ProjProposalTotals      ProjProposalTotals;
    TaxTable                taxTable;
    tax                     tax;
 
    projInvoiceJour     = ProjInvoiceJour::findRecId(5637146078); //projInvoiceJour.recid
    ProjProposalTotals  = new  ProjProposalTotals(ProjProposaljour::find( projInvoiceJour.ProposalId));
    ProjProposalTotals.calc();
    tax = ProjProposalTotals.tax();
   
    taxWorkTrans.setTmpData(tax.tmpTaxWorkTrans());
    while select taxWorkTrans
        join taxTable
        where taxTable.taxcode == taxWorkTrans.taxcode
    {
        info (strFmt('%1        %2     %3', taxWorkTrans.TaxCode, taxTable.showTaxValue(), taxWorkTrans.TaxAmount));

     
    }
 
}

convert Qty from a Unit to another Unit

convert Qty from a Unit to another Unit

Ax 2012
Qty salesQty;
salesQty = UnitOfMeasureConverter::convert(
_QtyBatch,
UnitOfMeasure::unitOfMeasureIdBySymbol(_itemUnit),
UnitOfMeasure::unitOfMeasureIdBySymbol(_salesUnit),
NoYes::Yes,
InventTable::itemProduct(_itemId));
return salesQty;
Ax 2009
Unitconvert::qty();




Wednesday, 2 November 2016

Bridge transaction in Ax 2012

Bridge transaction is not reflecting in General journal line in Ax 2012,






Solution
Need to write code on
Table     : LedgerTransFurtherPosting
Method : CreateledgerjournaltransfromGenJour

ledgerJournalTransTaxExtensionIN = LedgerJournalTransTaxExtensionIN::findByLedgerJournalTrans(ledgerJournalTrans.RecId);


        if (!ledgerJournalTransTaxExtensionIN.RecId)
        {
            ledgerJournalTransTaxExtensionIN.clear();
            ledgerJournalTransTaxExtensionIN.initValue();
            ledgerJournalTransTaxExtensionIN.TaxModelDocLineExtensionIN::init(ledgerJournalTrans);
            ledgerJournalTransTaxExtensionIN.LedgerJournalTrans = ledgerJournalTrans.RecId;

            if (ledgerJournalTransTaxExtensionIN.validateWrite())
            {
                ledgerJournalTransTaxExtensionIN.insert();
            }

        }