hljs.configure({cssSelector: "code"}); hljs.highlightAll();

Monday, October 11, 2021

X++ to code to capture infolog log messages in D365FO

  Public Str getErrorStr()

{

SysInfologEnumerator sysInfologEnumerator = SysInfologEnumerator::newData(infolog.infologData());

str retvalue ;

        while (sysInfologEnumerator.moveNext() )

        {

            switch (sysInfologEnumerator.currentException())

            {

                case Exception::Error:

                    retvalue = strFmt("%1", sysInfologEnumerator.currentMessage());

                    break;

            }

      }

Return retvalue ;

}

Monday, July 12, 2021

To Send the email with attachment in D365FO(Print Management)

 [ExtensionOf(classStr(SRSPrintDestinationSettings))]

final class SRSPrintDestinationSettings_Extension

{

    [SubscribesTo(classStr(SRSPrintDestinationSettingsDelegates), delegateStr(SRSPrintDestinationSettingsDelegates, toSendEmail))]

    public static void SRSPrintDestinationSettingsDelegates_toSendEmail(System.Byte[] reportBytes, SrsReportRunPrinter printer, SrsReportDataContract dataContract,Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] paramArray, EventHandlerResult result)

    {

        CustParameters              parameters = CustParameters::find();

        str                         messageBody;

        SysEmailMessageTable        message;

        LanguageId languageId    =  CompanyInfo::find().LanguageId;

        SRSPrintDestinationSettings printSettings = dataContract.parmPrintSettings();

        SysEmailTable               sysEmailTable = SysEmailTable::find(parameters.EmailTemplate);

        Filename                    fileName;

        SalesConfirmContract        contract = new SalesConfirmContract();

        SalesTable                  salesTable;

        SalesLine                   salesLine;

        CustConfirmJour             custConfirmJour;

        Map                         mappings = new Map(Types::String, Types::String);

        ServiceOrderBatchJobService  service = new ServiceOrderBatchJobService();

        contract = dataContract.parmRdpContract();

        custConfirmJour = CustConfirmJour::findRecId(contract.parmRecordId());

        select firstonly salesTable

            where salesTable.SalesId == custConfirmJour.SalesId;

        select firstonly salesLine

            where salesLine.SalesId == salesTable.SalesId;

        if(!languageId)

        {

            languageId = sysEmailTable.DefaultLanguage;

        }

// Place holders        

mappings = service.placeHolders(salesTable,salesLine,'');

        message     = SysEmailMessageTable::find(sysEmailTable.EmailId, sysEmailTable.DefaultLanguage);

        messageBody = message.Mail;

        messageBody = SysLabel::resolveLabels(messageBody, languageId);

        messageBody = SysEmailMessage::stringExpand(messageBody, SysEmailTable::htmlEncodeParameters(mappings));

        fileName    = SysEmailMessage::stringExpand(printSettings.parmFileName(), SysEmailTable::htmlEncodeParameters(mappings));

        printSettings.parmEMailContract().parmBody(messageBody);

        printSettings.parmFileName(fileName);

    }

}

Thursday, June 10, 2021

X++ code to get trade agreement price (sales ) based on price group in AX7/D365

     public Real GetPriceRetail(str itemNumber,str size, str color, str style, str channel)

    {

        PriceDisc               priceDisc;

        RetailStoreTable        storeTable;

        CustTable               custtable;

        InventTable             inventTable = InventTable::find(itemNumber);

        UnitOfMeasureSymbol     unitId  = inventTable.inventTableModuleInvent().UnitId;

        Price                   retPrice;

        InventDim               inventDim;

        select firstonly storeTable

            where storeTable.RetailChannelId == channel;

        custtable = CustTable::find(storeTable.DefaultCustAccount);

        InventDim.InventSizeId = size;

        InventDim.InventColorId = color;

        InventDim.InventStyleId = style;

        inventDim = InventDim::findOrCreate(inventDim);

        PriceDiscParameters parameters = PriceDiscParameters::construct();

        parameters.parmModuleType(ModuleInventPurchSales::Sales);

        parameters.parmItemId(itemNumber);

        parameters.parmInventDim(inventDim);

        parameters.parmUnitID(unitId);

        parameters.parmPriceDiscDate(systemDateGet());

        parameters.parmQty(1);

        parameters.parmAccountNum(custtable.AccountNum);

        parameters.parmCurrencyCode(custtable.Currency);

        priceDisc = PriceDisc::newFromPriceDiscParameters(parameters);

        // From Trade agreement

        if (priceDisc.findPrice('Retail')) // clearance ,club price

        {

            retPrice = priceDisc.price();

        }

return retPrice ;

}


  public Price getRRPPrice(salesLine  salesLine)

    {

        PriceDiscTable  priceRetail;

        InventDim     inventDimRetail,inventDim;

        inventDim = InventDim::find(salesLine.InventDimId);

        select firstonly priceRetail

            join inventDimRetail

            where priceRetail.ItemCode == PriceDiscProductCodeType::Table

            && priceRetail.ItemRelation==salesLine.ItemId

            && (priceRetail.FromDate <= DateTimeUtil::getToday(DateTimeUtil::getUserPreferredTimeZone()) || priceRetail.FromDate == Global::DateNull())

            && (priceRetail.ToDate >= DateTimeUtil::getToday(DateTimeUtil::getUserPreferredTimeZone()) || priceRetail.ToDate == Global::DateNull()) 

            && priceRetail.AccountCode == PriceDiscPartyCodeType::GroupId

            && priceRetail.AccountRelation ==  'Retail'

            && inventDimRetail.InventStyleId == inventDim.InventStyleId

            && inventDimRetail.InventColorId == inventDim.InventColorId

            && inventDimRetail.InventSizeId == inventDim.InventSizeId

            && priceRetail.InventDimId == inventDimRetail.inventDimId;


        return priceRetail.Amount;

    }

Wednesday, June 9, 2021

X++ code to get financial dimensions in AX7/D365


public void dimensions(defaultDimension)

{

VendParameters                          vendParameters = VendParameters::find();

DimensionAttribute                      attribute                = DimensionAttribute::findByName(vendParameters.DepartmentDim);

DimensionAttribute                      attributeProfitCentre    = DimensionAttribute::findByName(vendParameters.SiteDim);

MainAccountNum                          mainAccountNum;       

dimensionAttributeValueSetItemView.clear();

select DisplayValue from dimensionAttributeValueSetItemView

     where dimensionAttributeValueSetItemView.DimensionAttributeValueSet == DefaultDimension

        && dimensionAttributeValueSetItemView.DimensionAttribute    == attribute.RecId;

DimensionValue = dimensionAttributeValueSetItemView.DisplayValue;

dimensionAttributeValueSetItemView.clear();

select DisplayValue from dimensionAttributeValueSetItemView

      where dimensionAttributeValueSetItemView.DimensionAttributeValueSet == DefaultDimension

           && dimensionAttributeValueSetItemView.DimensionAttribute  ==  attributeProfitCentre.RecId;

DimensionValueprofitCentre = dimensionAttributeValueSetItemView.DisplayValue;

mainAccountNum = MainAccount::findByMainAccountId(LedgerDimensionFacade::getMainAccountIdFromLedgerDimension(paymode.InterCompanyLedgerDimension)).MainAccountId;

ledgerDim = CreateAndPostVendorJournal::getDimension(mainAccountNum,DimensionValue,DimensionValueprofitCentre);

}

//custom method

    public static RecId getDimension(str _ledgerAccount,DimensionValue _costCentre,DimensionValue _profitCentre)

    {

        //DimensionServiceProvider            DimensionServiceProvider    = new DimensionServiceProvider();

        LedgerAccountContract               LedgerAccountContract       = new LedgerAccountContract();

        DimensionAttributeValueContract     ValueContract;

        List                                ListValueContract = new List(Types::Class);

        dimensionAttributeValueCombination  dimensionAttributeValueCombination;

        DimensionStorage                    dimStorage;

        VendParameters salesParameters = VendParameters::find();

        if(_costCentre)

        {

            ValueContract = new DimensionAttributeValueContract();

            ValueContract.parmName(salesParameters.DepartmentDim);

            ValueContract.parmValue(_costCentre);

            ListValueContract.addEnd(ValueContract);

        }

        if(_profitCentre)

        {

            ValueContract = new DimensionAttributeValueContract();

            ValueContract.parmName(salesParameters.SiteDim);/\                              ValueContract.parmValue(_profitCentre);

            ListValueContract.addEnd(ValueContract);

        }

        LedgerAccountContract.parmMainAccount(_ledgerAccount);

        //LedgerAccountContract.parm

        LedgerAccountContract.parmValues(ListValueContract);

        dimStorage = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(LedgerAccountContract);

        dimensionAttributeValueCombination = DimensionAttributeValueCombination::find(dimStorage.save());

        return dimensionAttributeValueCombination.RecId;

    }





 

Ledger settlements Automate through code in AX7/D365

 class ledgerSettlementService extends SysOperationServiceBase

{

    public void processOperation()

    {

        GeneralJournalAccountEntry     GeneralJournalAccountEntry,GeneralJournalAccountEntryLoc;

        GeneralJournalEntry            GeneralJournalEntry,GeneralJournalEntryLocal;

        LedgerTransSettlement          ledgerTransSettlement,ledgerTransSettlementloc,ledgerTransSettlementdata;

        LedgerEntryJournal             LedgerEntryJournal,LedgerEntryJournalLocal;

        real balance;

// Getting dimension from parameters

        RefRecId mainAccountRecId = MainAccount::findByMainAccountId(LedgerDimensionFacade::getMainAccountIdFromLedgerDimension(Parameters.WindcaveLedgerDimension)).RecId;


        while SELECT PaymentReference from GeneralJournalAccountEntry

            group by GeneralJournalAccountEntry.PaymentReference

                where GeneralJournalAccountEntry.MainAccount == mainAccountRecId

            JOIN  GeneralJournalEntry

                where  GeneralJournalAccountEntry.GeneralJournalEntry == GeneralJournalEntry.RecId 

                    && GeneralJournalEntry.Ledger == CompanyInfo::find().RecId

                    && GeneralJournalEntry.PostingLayer == CurrentOperationsTax::Current

            OUTER join  LedgerEntryJournal

                where generalJournalEntry.LedgerEntryJournal == LedgerEntryJournal.RecId

                NOTEXISTS join LedgerTransSettlement

                    WHERE GeneralJournalAccountEntry.RecId == LedgerTransSettlement.TransRecId

        {

          //  balance = 0;

            if(!this.balance(GeneralJournalAccountEntry,mainAccountRecId))

            {

                ledgerTransSettlementloc.SettleId = NumberSeq::newGetNum(CompanyInfo::numRefParmId()).num();


                while SELECT  * FROM GeneralJournalAccountEntryLoc

                    where GeneralJournalAccountEntryLoc.MainAccount == mainAccountRecId

                        && GeneralJournalAccountEntryLoc.PaymentReference == GeneralJournalAccountEntry.PaymentReference

                        JOIN  GeneralJournalEntryLocal

                    where  GeneralJournalAccountEntryLoc.GeneralJournalEntry == GeneralJournalEntryLocal.RecId &&

                        GeneralJournalEntryLocal.Ledger == CompanyInfo::find().RecId

                        && GeneralJournalEntryLocal.PostingLayer == 0

                        OUTER join  LedgerEntryJournalLocal

                    where GeneralJournalEntryLocal.LedgerEntryJournal == LedgerEntryJournalLocal.RecId

                        NOTEXISTS join ledgerTransSettlementdata

                    WHERE GeneralJournalAccountEntryLoc.RecId == ledgerTransSettlementdata.TransRecId

                //while select GeneralJournalEntryLoc

                //    where GeneralJournalEntryLoc.PaymentReference == GeneralJournalAccountEntry.PaymentReference

                //     && GeneralJournalEntryLoc.LedgerDimension == T07_TenderParameters::Find().WindcaveLedgerDimension

                {

                    ledgerTransSettlementloc.TransRecId = GeneralJournalAccountEntryLoc.RecId;

                    ledgerTransSettlementloc.insert();

                }

            }

        }


    }


    public real balance(GeneralJournalAccountEntry _generalJournalEntry,RefRecId   mainAccount)

    {

        GeneralJournalAccountEntry     GeneralJournalAccountEntry,GeneralJournalEntryLoc;

        GeneralJournalEntry            GeneralJournalEntry;

        LedgerTransSettlement ledgerTransSettlement,ledgerTransSettlementloc;

        RecordInsertList settlementCollection;

        LedgerEntryJournal LedgerEntryJournal;

        real balance = 0;


        while SELECT  GeneralJournalAccountEntry

                where GeneralJournalAccountEntry.MainAccount == mainAccount

             && GeneralJournalAccountEntry.PaymentReference == _generalJournalEntry.PaymentReference

            JOIN  GeneralJournalEntry

                where  GeneralJournalAccountEntry.GeneralJournalEntry == GeneralJournalEntry.RecId

                    && GeneralJournalEntry.Ledger == CompanyInfo::find().RecId

                    && GeneralJournalEntry.PostingLayer == CurrentOperationsTax::Current

            OUTER join  LedgerEntryJournal

                where generalJournalEntry.LedgerEntryJournal == LedgerEntryJournal.RecId

                NOTEXISTS join LedgerTransSettlement

                    WHERE GeneralJournalAccountEntry.RecId == LedgerTransSettlement.TransRecId

        //while select GeneralJournalEntryLocal

        //    where GeneralJournalEntryLocal.PaymentReference == GeneralJournalEntry.PaymentReference

              //  && GeneralJournalEntryLocal.LedgerDimension == T07_TenderParameters::Find().WindcaveLedgerDimension

        {

            balance += GeneralJournalAccountEntry.AccountingCurrencyAmount;

        }

        return balance;

    }


}

Thursday, January 28, 2021

Avoid Duplicates In LookUp In Ax/D365Fo

Static void Job1()

   Query query = new Query();

    QueryBuildDataSource qbds1;

    QueryBuildRange qbr;

    SysTableLookup sysTableLookup;

    sysTableLookup = sysTableLookup::newParameters(tablenum(InventTable),this);

    sysTableLookup.addLookupfield(fieldnum(InventTable,ItemId));

    sysTableLookup.addLookupfield(fieldNum(InventTable,ItemId)); 

    qbds1 = query.addDataSource(tableNum(InventTable));

    qbds1.addGroupByField(fieldNum(InventTable,ItemId));

    qbds1.orderMode(OrderMode::GroupBy);

    sysTableLookup.parmQuery(query);

    sysTableLookup.parmUseLookupValue(False);

    sysTableLookup.performFormLookup();

}

Wednesday, January 27, 2021

X++ Code to Retrieve Customer Contact information in AX/D365Fo

 

static void Getcontact(Args _args)

{

    CustTable                   custTable;

    DirPartyLocation            dirPartyLocation;

    LogisticsElectronicAddress  logisticsElectronicAddress;

    select firstOnly * from custTable

        where custTable.AccountNum == 'us-002'

    join dirPartyLocation

        where custTable.Party == dirPartyLocation.Party

    join logisticsElectronicAddress

        where logisticsElectronicAddress.Location == dirPartyLocation.Location

    && logisticsElectronicAddress.Type == LogisticsElectronicAddressMethodType::Phone;

    info(strFmt("Customer: %1, PostalAddress: %2",  custTable.AccountNum,                                                   logisticsElectronicAddress.Locator));


}

Cannot edit a record in Order lines (SalesLine). An update conflict occurred due to another user process deleting the record or changing one or more fields in the record.

Some times when we perform delete or update actions then we might get error like 

"Cannot edit a record in Order lines (SalesLine). An update conflict occurred due to another user process deleting the record or changing one or more fields in the record."

Solution for this error is just add reread() before update statement.

salesLine.Reread()

salesLine.Update();


 Reference :Refer to this