web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

How to create Vendor Invoice Journal using x++ in D365 FO

Vishals786 Profile Picture Vishals786
Hi folks ,


Very common requirement these days is to create Vendor Invoice Journal through code.

Here is an example mentioned below in which I am picking the values for Vendor invoice creation from a staging table :-

Class Name :- VendInvoiceJournalCreateBatch

        #define.initFromJournal('VINV')
        #define.AEDCurrency('USD')
        #define.EmptyCurrency('')
       
        int                                                                      i;
        str                                                                      invdate , invfinaldate;
        date                                                                   dateheader , dateheaderval , dateline;
        System.DateTime                                             netDttm;
        real                                                                    amountcr;
        utcdatetime                                                       xppDttm , invdatetime;
        SupplierInvoiceHeaderDetails                         vendInvoiceJourStageHeader,                                                                                                                      vendInvoiceJourStageHeaderUpdate;
        SupplierInvoiceLineDetails                                vendInvoiceJourStageLine
                                                                                    , vendInvoiceJourStageLineds;
        LedgerJournalTable                                            journalTable;
        JournalTableData                                                journalTableData;
        LedgerJournalTrans                                            journalTrans;
        container                                                             offsetCon, offsetConLoc , mainAccountCon;
        NumberSeq                                                         numberSeq;
        Voucher                                                               voucher;
        LedgerJournalName                                            ledgerJournalName;
        AccountNum                                                       vendAccount;
        HcmWorker                                                         hcmWorker;
        DirPerson                                                            dirPerson;
        DirPersonUser                                                    dirPersonUser;
        VendInvoiceJournalCreateBatch                        classVend   =   new                                                                                                                                    VendInvoiceJournalCreateBatch();
        MainAccount                                                      mainAccount;
        Voucher                                                               vch;
        DimensionAttributeValueCombination              dimCombination1;
        String255                                                            tmpOffsetAccount, tmpVoucherNo;
        DimensionAttributeValueCombination              dimCombination, dimCombinationLoc;
        NoYes                                                                 newOffsetLine = NoYes::Yes;
        LedgerJournalId                                                 journalId;
        String10                                                              purchId;
       
        i=0;

        while select vendInvoiceJourStageHeader where vendInvoiceJourStageHeader.UpdateFlag == 0
        {
            try
            {
                ledgerJournalName            =   LedgerJournalName::find(#initFromJournal);
                journalTable.initFromLedgerJournalName(#initFromJournal);
                journalTableData            =   JournalTableData::newTable(journalTable);
                journalTable.JournalNum     =   journalTableData.nextJournalId();
                journalTable.CurrencyCode   =   #AEDCurrency;
                if (journalTable.validateWrite())
                {
                    journalTable.insert();
                }

                journalId = journalTable.JournalNum;

                select count(RecId) from vendInvoiceJourStageLineds where vendInvoiceJourStageLineds.DocumentRef  ==  vendInvoiceJourStageHeader.Docref;

                while select vendInvoiceJourStageLine order by OffsetAccount desc
                    where vendInvoiceJourStageLine.DocumentRef  ==  vendInvoiceJourStageHeader.Docref
                {
                    amountcr= 0.00;
                    tmpOffsetAccount    = vendInvoiceJourStageLine.OffsetAccount;
                    tmpVoucherNo        = vendInvoiceJourStageLine.VoucherNo;

                    select dirPersonUser where dirpersonuser.User==curUserId();

                    select dirPerson where dirPerson.RecId == dirPersonUser.PersonParty;

                    select hcmWorker where hcmWorker.Person == dirPerson.RecId;

                    select  dimCombination where dimCombination.DisplayValue    ==  vendInvoiceJourStageHeader.Vendor;

                    select  dimCombinationLoc where dimCombinationLoc.DisplayValue    ==  vendInvoiceJourStageLine.OffsetAccount;
                   
                    offsetCon                           =   [vendInvoiceJourStageLine.Ledger,vendInvoiceJourStageLine.Region,vendInvoiceJourStageLine.LOB,vendInvoiceJourStageLine.Department,vendInvoiceJourStageLine.BIN,vendInvoiceJourStageLine.Contract];
                    offsetConLoc                        =   [vendInvoiceJourStageLine.OffsetAccount,vendInvoiceJourStageLine.Region,vendInvoiceJourStageLine.LOB,vendInvoiceJourStageLine.Department,vendInvoiceJourStageLine.BIN,vendInvoiceJourStageLine.Contract];

                    numberSeq                           =   NumberSeq::newGetVoucherFromId(journalTable.NumberSequenceTable, true);

                    if(vendInvoiceJourStageLine.OffsetAccount != '')
                    {
                        select  dimCombination where dimCombination.DisplayValue    ==  vendInvoiceJourStageLine.Ledger;
                        offsetConLoc                           =   [vendInvoiceJourStageLine.OffsetAccount,vendInvoiceJourStageLine.Region,vendInvoiceJourStageLine.LOB,vendInvoiceJourStageLine.Department,vendInvoiceJourStageLine.BIN,vendInvoiceJourStageLine.Contract];

                        journalTrans.JournalNum             =   journalTable.JournalNum;
                        journalTrans.Voucher                =   numberSeq.voucher();
                        journalTrans.Invoice                =   vendInvoiceJourStageHeader.InvoiceNumber;
                        journalTrans.Txt                    =   vendInvoiceJourStageHeader.InvoiceDescription;
                        invdatetime = DateTimeUtil::anyToDateTime(vendInvoiceJourStageHeader.InvoiceDate);

                        dateheader = DateTimeUtil::date(invdatetime);

                        invdate = date2Str(dateheader,321,DateDay::Digits2,DateSeparator::Slash,DateMonth::Digits2,DateSeparator::Hyphen,DateYear::Digits4);
       
                        dateheaderval = str2Date(invdate,321);

                        journalTrans.INTC_InvDate = dateheaderval;
                        journalTrans.DocumentDate = str2Date(vendInvoiceJourStageHeader.InvoiceReceivedDate,321);
                        journalTrans.TransDate              =   today();//str2Date(journalDeatils.TransactionDate, 123);
                        journalTrans.AccountType            =   LedgerJournalACType::Ledger;
                        //journalTrans.LedgerDimension        =   dimCombination.RecId;
                        journalTrans.LedgerDimension        =   classVend.generateLedgerDimension(offsetCon, vendInvoiceJourStageLine.Ledger);
                        //journalTrans.AmountCurCredit        =   vendInvoiceJourStageLine.InvoiceAmount;
                        journalTrans.AmountCurDebit         =   vendInvoiceJourStageLine.InvoiceAmount;
                        journalTrans.AmountCurCredit         = 0.00;
                        journalTrans.OffsetAccountType      =   LedgerJournalACType::Ledger;
                        //journalTrans.OffsetLedgerDimension  =   classVend.generateLedgerDimension(offsetCon, vendInvoiceJourStageLine.Ledger);
                        //journalTrans.OffsetLedgerDimension  =   dimCombinationLoc.RecId;
                        journalTrans.OffsetLedgerDimension  =   classVend.generateLedgerDimension(offsetConLoc, vendInvoiceJourStageLine.OffsetAccount);
                        journalTrans.INTC_DocumentRef       =   vendInvoiceJourStageLine.DocumentRef;

                        if(vendInvoiceJourStageLine.Currency  ==  #EmptyCurrency)
                        {
                            journalTrans.CurrencyCode   =   #AEDCurrency;
                        }
                        else
                        {
                            journalTrans.CurrencyCode   =   vendInvoiceJourStageLine.Currency;
                        }

                        journalTrans.ExchRate = Currency::exchRate1(journalTrans.CurrencyCode, journalTrans.TransDate);
                        journalTrans.Approver = hcmWorker.RecId;
                        journalTrans.Approved =NoYes::Yes;
                        journalTrans.PostingProfile = "General";
                        journalTrans.INTC_PurchId       =   vendInvoiceJourStageLine.PONo;
                        journalTrans.TransactionType = LedgerTransType::Vend;
                        journalTrans.INTC_WorkOrderId   =   vendInvoiceJourStageLine.WorkOrderNo;
                        journalTrans.INTC_Qty           =   vendInvoiceJourStageLine.Qty;
                        journalTrans.Payment = vendInvoiceJourStageLine.PaymentTerms;
                        journalTrans.insert();

                        //======================
                        numberSeq.used();
                        //======================
                    }
                    else
                    {
                        select  dimCombination where dimCombination.DisplayValue    ==  vendInvoiceJourStageLine.Ledger;
                        select  dimCombinationLoc where dimCombinationLoc.DisplayValue    ==  vendInvoiceJourStageLine.OffsetAccount;

                        offsetCon                           =   [vendInvoiceJourStageLine.Ledger,vendInvoiceJourStageLine.Region,vendInvoiceJourStageLine.LOB,vendInvoiceJourStageLine.Department,vendInvoiceJourStageLine.BIN,vendInvoiceJourStageLine.Contract];
                        if(vendInvoiceJourStageLine.OffsetAccount != '')
                        {
                            offsetConLoc                           =   [vendInvoiceJourStageLine.OffsetAccount,vendInvoiceJourStageLine.Region,vendInvoiceJourStageLine.LOB,vendInvoiceJourStageLine.Department,vendInvoiceJourStageLine.BIN,vendInvoiceJourStageLine.Contract];
                        }
                        journalTrans.JournalNum             =   journalTable.JournalNum;

                        if(newOffsetLine == NoYes::Yes)
                        {
                            numberSeq                   = NumberSeq::newGetVoucherFromId(journalTable.NumberSequenceTable, true);
                            journalTrans.Voucher        = numberSeq.voucher();
                            vch                         = journalTrans.Voucher;
                            newOffsetLine               = NoYes::No;
                        }
                        else
                        {
                            journalTrans.Voucher        = vch;
                        }

                        purchId = vendInvoiceJourStageLine.POLineNum;

                        amountcr+=vendInvoiceJourStageLine.InvoiceAmount;
                        journalTrans.Invoice            =   vendInvoiceJourStageHeader.InvoiceNumber;
                        journalTrans.Txt                 =   vendInvoiceJourStageHeader.InvoiceDescription;
                        invdatetime                     = DateTimeUtil::anyToDateTime(vendInvoiceJourStageHeader.InvoiceDate);

                        dateheader = DateTimeUtil::date(invdatetime);

                        invdate = date2Str(dateheader,321,DateDay::Digits2,DateSeparator::Slash,DateMonth::Digits2,DateSeparator::Hyphen,DateYear::Digits4);
       
                        dateheaderval = str2Date(invdate,321);

                        journalTrans.INTC_InvDate = dateheaderval;
                        journalTrans.DocumentDate = str2Date(vendInvoiceJourStageHeader.InvoiceReceivedDate,321);
                        journalTrans.TransDate              =   today();//str2Date(journalDeatils.TransactionDate, 123);
                        journalTrans.AccountType            =   LedgerJournalACType::Ledger;
                        //journalTrans.LedgerDimension        =   dimCombination.RecId;
                        journalTrans.LedgerDimension        =   classVend.generateLedgerDimension(offsetCon, vendInvoiceJourStageLine.Ledger);
                        //journalTrans.AmountCurCredit        =   vendInvoiceJourStageLine.InvoiceAmount;
                        journalTrans.AmountCurDebit         =   vendInvoiceJourStageLine.InvoiceAmount;
                        journalTrans.AmountCurCredit         = 0.00;
                        journalTrans.OffsetAccountType      =   LedgerJournalACType::Ledger;
                        //journalTrans.OffsetLedgerDimension  =   classVend.generateLedgerDimension(offsetCon, vendInvoiceJourStageLine.Ledger);

                        if(vendInvoiceJourStageLine.OffsetAccount == '')
                        {
                            journalTrans.OffsetLedgerDimension  =   dimCombinationLoc.RecId;
                        }
                        else
                        {
                            journalTrans.OffsetLedgerDimension  =   classVend.generateLedgerDimension(offsetConLoc, vendInvoiceJourStageLine.OffsetAccount);
                        }

                        journalTrans.INTC_DocumentRef       =   vendInvoiceJourStageLine.DocumentRef;
                        if(vendInvoiceJourStageLine.Currency  ==  #EmptyCurrency)
                        {
                            journalTrans.CurrencyCode   =   #AEDCurrency;
                        }
                        else
                        {
                            journalTrans.CurrencyCode   =   vendInvoiceJourStageLine.Currency;
                        }
                        journalTrans.ExchRate = Currency::exchRate1(journalTrans.CurrencyCode, journalTrans.TransDate);
                        journalTrans.Approver = hcmWorker.RecId;
                        journalTrans.Approved =NoYes::Yes;
                        journalTrans.PostingProfile = "General";
                        journalTrans.INTC_PurchId       =   vendInvoiceJourStageLine.PONo;
                        journalTrans.TransactionType = LedgerTransType::Vend;
                        journalTrans.INTC_WorkOrderId   =   vendInvoiceJourStageLine.WorkOrderNo;
                        journalTrans.INTC_Qty           =   vendInvoiceJourStageLine.Qty;
                        journalTrans.Payment = vendInvoiceJourStageLine.PaymentTerms;
                        journalTrans.insert();

                        //======================
                        numberSeq.used();
                        //======================
                    }
                }

                select dirPersonUser where dirpersonuser.User==curUserId();

                select dirPerson where dirPerson.RecId == dirPersonUser.PersonParty;

                select hcmWorker where hcmWorker.Person == dirPerson.RecId;

                select  dimCombination1 where dimCombination1.DisplayValue    ==  vendInvoiceJourStageHeader.Vendor;
                offsetCon                           =   [vendInvoiceJourStageHeader.Vendor,vendInvoiceJourStageLine.Region,vendInvoiceJourStageLine.LOB,vendInvoiceJourStageLine.Department,vendInvoiceJourStageLine.BIN,vendInvoiceJourStageLine.Contract];
                numberSeq                           =   NumberSeq::newGetVoucherFromId(journalTable.NumberSequenceTable, true);

                //journalTrans.JournalNum             =   journalTable.JournalNum;
                journalTrans.JournalNum             =   journalId;

                journalTrans.Voucher                =   vch;

                vch = journalTrans.Voucher;

                journalTrans.Invoice                =   vendInvoiceJourStageHeader.InvoiceNumber;
                journalTrans.Txt                    =   vendInvoiceJourStageHeader.InvoiceDescription;
                invdatetime = DateTimeUtil::anyToDateTime(vendInvoiceJourStageHeader.InvoiceDate);

                dateheader = DateTimeUtil::date(invdatetime);

                invdate = date2Str(dateheader,321,DateDay::Digits2,DateSeparator::Slash,DateMonth::Digits2,DateSeparator::Hyphen,DateYear::Digits4);
       
                dateheaderval = str2Date(invdate,321);

                journalTrans.INTC_InvDate = dateheaderval;
                journalTrans.DocumentDate = str2Date(vendInvoiceJourStageHeader.InvoiceReceivedDate,321);
                journalTrans.TransDate              =   today();//str2Date(journalDeatils.TransactionDate, 123);
                journalTrans.AccountType            =   LedgerJournalACType::Vend;
                journalTrans.LedgerDimension        =   dimCombination1.RecId;
                //journalTrans.LedgerDimension        =   classVend.generateLedgerDimension(offsetCon, vendInvoiceJourStageHeader.Vendor);
                journalTrans.AmountCurDebit         =   0.00;
                journalTrans.AmountCurCredit        =   amountcr;
                journalTrans.OffsetAccountType      =   LedgerJournalACType::Ledger;
                //journalTrans.OffsetLedgerDimension  =   classVend.generateLedgerDimension(offsetCon, vendInvoiceJourStageLine.Ledger);
                //journalTrans.OffsetLedgerDimension  =   dimCombinationLoc.RecId;

                if(vendInvoiceJourStageLine.OffsetAccount == '')
                {
                    journalTrans.OffsetLedgerDimension  =   dimCombinationLoc.RecId;
                }
                else
                {
                    journalTrans.OffsetLedgerDimension  =   classVend.generateLedgerDimension(offsetConLoc, vendInvoiceJourStageLine.OffsetAccount);
                }
               
                journalTrans.INTC_DocumentRef       =   vendInvoiceJourStageHeader.Docref;
               
                if(vendInvoiceJourStageLine.Currency  ==  #EmptyCurrency)
                {
                    journalTrans.CurrencyCode   =   #AEDCurrency;
                }
                else
                {
                    journalTrans.CurrencyCode   =   vendInvoiceJourStageLine.Currency;
                }
                journalTrans.ExchRate = Currency::exchRate1(journalTrans.CurrencyCode, journalTrans.TransDate);
                journalTrans.Approver = hcmWorker.RecId;
                journalTrans.Approved =NoYes::Yes;
                journalTrans.PostingProfile = "General";
                journalTrans.INTC_PurchId       =   vendInvoiceJourStageLine.PONo;
                journalTrans.TransactionType = LedgerTransType::Vend;
                journalTrans.INTC_WorkOrderId   =   vendInvoiceJourStageLine.WorkOrderNo;
                journalTrans.INTC_Qty           =   vendInvoiceJourStageLine.Qty;
                journalTrans.Payment = vendInvoiceJourStageLine.PaymentTerms;
                if(amountcr>0)
                {
                    journalTrans.insert();
                }
                ttsbegin;
                select forupdate vendInvoiceJourStageHeaderUpdate where vendInvoiceJourStageHeaderUpdate.RecId    ==    vendInvoiceJourStageHeader.RecId;
                vendInvoiceJourStageHeaderUpdate.UpdateFlag = 1;
                vendInvoiceJourStageHeaderUpdate.update();
                ttscommit;

                //======================
                numberSeq.used();
                //======================
            }
            catch(Exception::Error)
            {
                throw Exception::Error;
            }
        }



public DimensionDynamicAccount   generateLedgerDimension(container    _conData,                                                                                                                          MainAccountNum  _mainAccountNum)
    {
        int                                                               hierarchyCount;
        int                                                               hierarchyIdx;
        RecId                                                          dimAttId_MainAccount;
        LedgerRecId                                              ledgerRecId;
        MainAccount                                             mainAccount;
        RefRecId                                                    recordvalue;
        DimensionAttribute                                   dimensionAttribute;
        DimensionAttributeValue                          dimensionAttributeValue;
        DimensionSetSegmentName                     DimensionSet;
        DimensionStorage                                     dimStorage;
        LedgerAccountContract                            LedgerAccountContract = new                                                                                                                                                          LedgerAccountContract();
        DimensionAttributeValueContract            ValueContract;

        List                                                             valueContracts = new List(Types::Class);
       
         dimensionAttributeValueCombination  dimensionAttributeValueCombination;
     

        mainAccount     =   MainAccount::findByMainAccountId(_mainAccountNum);
        recordvalue     =                                                                                                                                                         DimensionHierarchy::getAccountStructure(mainAccount.RecId,Ledger::current());
        hierarchyCount  =   DimensionHierarchy::getLevelCount(recordvalue);
        DimensionSet    =                                                                                                                                                          DimensionHierarchyLevel::getDimensionHierarchyLevelNames(recordvalue);

        for(hierarchyIdx = 1;hierarchyIdx<=hierarchyCount;hierarchyIdx++)
        {
            if(hierarchyIdx == 1)
            {
                continue;
            }
            dimensionAttribute =                                                                                                                                         DimensionAttribute::findByLocalizedName(DimensionSet[hierarchyIdx],false,"en-us");

            if(dimensionAttribute)
            {
                dimensionAttributeValue =                                                                                                                     DimensionAttributeValue::findByDimensionAttributeAndValue(
                                                         dimensionAttribute,conPeek(_conData,hierarchyIdx));

                if(dimensionAttributeValue)
                {
                    ValueContract = new DimensionAttributeValueContract();
                    ValueContract.parmName(dimensionAttribute.Name) ;
                    ValueContract.parmValue(dimensionAttributeValue.CachedDisplayValue);
                    valueContracts.addEnd(ValueContract);
                }
            }
        }
        LedgerAccountContract.parmMainAccount(_mainAccountNum);
        LedgerAccountContract.parmValues(valueContracts);
        dimStorage                          =    DimensionServiceProvider::buildDimensionStorageForLedgerAccount(LedgerAccountContract);
        dimensionAttributeValueCombination  =    DimensionAttributeValueCombination::find(dimStorage.save());
        ledgerRecId                         =    dimensionAttributeValueCombination.RecId;

        return ledgerRecId;
    }


Happy Coding !!!!


This was originally posted here.

Comments

*This post is locked for comments