Saturday, 11 July 2020

Create and post pending vendor invoices without purchase order in D365 FO using x++

If you don't have purchase order details but still needs to create and post pending vendor invoice then the below code may help.


class RunnableClass1
{
    
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        VendInvoiceInfoTable            vendInvoiceInfoTable;
        VendInvoiceInfoSubTable     vendInvoiceInfoSubTable;
        VendInvoiceInfoLine             vendInvoiceInfoLine;
        VendInvoiceInfoSubLine      vendInvoiceInfoSubLine;
        PurchParmUpdate                 purchParmUpdate;

        PurchFormletterParmData     purchFormletterParmData = PurchFormletterParmData::newData(DocumentStatus::Invoice, VersioningUpdateType::Initial);
        PurchFormLetter         purchFormLetter;
        
        purchFormletterParmData.init();
        purchFormletterParmData.parmOnlyCreateParmUpdate(true);
        purchFormletterParmData.createData(false);
        purchParmUpdate = purchFormletterParmData.parmParmUpdate();

        ttsbegin;
        vendInvoiceInfoTable.clear();
        vendInvoiceInfoTable.initValue();

        vendInvoiceInfoTable.DocumentOrigin  = DocumentOrigin::Manual;
        vendInvoiceInfoTable.DeliveryName = '';
        //vendInvoiceInfoTable.Num = "INV015"; //add invoice number in here
        vendInvoiceInfoTable.InvoiceAccount = '1001';
       
        vendInvoiceInfoTable.defaultRow(null, null, true);
        vendInvoiceInfoTable.Num = "INV031";
        vendInvoiceInfoTable.VendInvoiceSaveStatus = VendInvoiceSaveStatus::Pending;
        vendInvoiceInfoTable.DocumentDate = systemDateGet();
        vendInvoiceInfoTable.LastMatchVariance = LastMatchVarianceOptions::OK;
        vendInvoiceInfoTable.CashDiscDate  = systemDateGet() + 10; 

        vendInvoiceInfoTable.insert();
        
        if(vendInvoiceInfoTable)
        {
            vendInvoiceInfoSubTable.clear();
            vendInvoiceInfoSubTable.initValue();
            vendInvoiceInfoSubTable.defaultRow();
 
            vendInvoiceInfoSubTable.ParmId = vendInvoiceInfoTable.ParmId;
            vendInvoiceInfoSubTable.OrigPurchId = vendInvoiceInfoTable.PurchId;
            vendInvoiceInfoSubTable.PurchName = vendInvoiceInfoTable.PurchName;
            vendInvoiceInfoSubTable.TableRefId = vendInvoiceInfoTable.TableRefId;
 
            vendInvoiceInfoSubTable.insert();
        }

        vendInvoiceInfoLine.clear();
        vendInvoiceInfoLine.initValue();
 
        vendInvoiceInfoLine.DeliveryName = vendInvoiceInfoTable.DeliveryName;
        vendInvoiceInfoLine.TableRefId = vendInvoiceInfoTable.TableRefId;
        vendInvoiceInfoLine.currencyCode = vendInvoiceInfoTable.CurrencyCode;
        vendInvoiceInfoLine.LineNum = 1;
        
        InventTable inventTable = InventTable::find("C0001");


        vendInvoiceInfoLine.InvoiceAccount = vendInvoiceInfoTable.InvoiceAccount;
        vendInvoiceInfoLine.OrderAccount  = vendInvoiceInfoTable.OrderAccount;
        vendInvoiceInfoLine.ItemId = inventTable.ItemId;
        vendInvoiceInfoLine.modifiedField(fieldNum(VendInvoiceInfoLine, ItemId));
 
        vendInvoiceInfoLine.DocumentOrigin = DocumentOrigin::Manual;
 
        vendInvoiceInfoLine.ReceiveNow = 1.2;
        vendInvoiceInfoLine.RemainBefore = 1.2;
        vendInvoiceInfoLine.RemainBeforeInvent = 1.2;
 
        vendInvoiceInfoLine.PurchPrice = 5.5;
        vendInvoiceInfoLine.InventNow = 1.2;
        vendInvoiceInfoLine.LineAmount = 6.6;
 
 
        vendInvoiceInfoLine.insert();
        ttscommit;

        vendInvoiceInfoTable vendInvoiceInfoTableLoc;

        select vendInvoiceInfoTableLoc where vendInvoiceInfoTableLoc.Num == vendInvoiceInfoTable.Num;

        purchFormLetter = PurchFormLetter_Invoice::newFromSavedInvoice(vendInvoiceInfoTableLoc);
        purchFormLetter.update(vendInvoiceInfoTableLoc.purchTable(), vendInvoiceInfoTableLoc.Num);
    }

}

2 comments:

  1. following line cause error in my code:
    purchFormLetter.update(vendInvoiceInfoTableLoc.purchTable(), vendInvoiceInfoTableLoc.Num);

    if we want to create pending invoice without purchase order, why do we call purchTable method? i tried to use null instead of that method, but the result was same.

    ReplyDelete

Insert/Update or remove the default dimension value in D365 FSCM via x++

Use below method to insert/update the dimension values, just pass the parameter values to the method and it will return the updated value: p...