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 :
Microsoft Dynamics AX (Archived)

Trying to Post a PO and invoice by code

(0) ShareShare
ReportReport
Posted on by 59
I'm attempting to write a function that will post a purchase order and invoice it.  I'm having a few issues that I can't seem to resolve. 
First is setting the Financial dimension on the item line, "item group". It doesn't seem to set the item group when I create the invoice and it goes blank.

Second the part where the code: I get the error "Field 'Line reference' must be filled in." when it goes to validate. If i remove validateWrite it inserts.. I'm not sure how to fix this validate error.
   if (purchParmTable.validateWrite())
        purchParmTable.insert();
   else
       throw Exception::Error;

Third issue is the part of confirming and then posting the invoice. I first set the code to DocumentStatus::PurchaseOrder which goes through. Then after that runs i set the Status to Invoice. I need to add the parameters document date, invoice and description.
The problem when it goes to invoice at this part:     purchFormLetterParmData.createData(false);

I get the Error below: Object is not initialized and i can't seem to generate the invoice. I have been moving around alot of code so some is commented out and some ids replaced with text for the purpose of the example. Can anyone help me with these issues to properly post an invoice for a purchase order thanks.

List object not initialized.

Stack trace

(S)\Classes\PurchFormletterParmDataInvoice\fillOutputContract - line 5
(S)\Classes\formletterParmData\createData - line 59
(S)\Classes\PurchFormletterParmDataInvoice\createData - line 3
(C)\Jobs\CreatePO - line 184



static void CreatePO(Args _args)
{
NumberSeq numberSeq;
Purchtable Purchtable;
PurchLine PurchLine;
PurchParmTable purchParmTable;
PurchFormLetter purchFormLetter;
InventDimId _inventDimId;
    InventDim inventDim;
DocumentDate docDate;
PurchParmUpdate purchParmUpdate;
PurchFormletterParmData     purchFormLetterParmData;
Date myDate;
str test;
container  ledgerDimensions;
DimensionDefault dimensionDefault;



changeCompany("SomeCompany") //Inserts into specified company
{
   //numRefPurchaseOrderId()
ttsbegin;
numberSeq = NumberSeq::newGetNum(purchParameters::numRefPurchId());
_inventDimId="InventDimID";


// Initialize Purchase order values
Purchtable.initValue();
Purchtable.PurchId = numberSeq.num();


Purchtable.OrderAccount = 'OrderAccount';
Purchtable.initFromVendTable();

if (!Purchtable.validateWrite())
{
throw Exception::Error;
}
Purchtable.insert();


// Initialize Purchase Line items

PurchLine.clear();
PurchLine.initValue(purchTable.PurchaseType);
PurchLine.initFromPurchTable(Purchtable);
PurchLine.initFromInventTable(InventTable::find('SomeItem'));

PurchLine.PurchId = Purchtable.PurchId;
PurchLine.InventDimId="InventDimID";


PurchLine.PurchQty=1;
PurchLine.PurchUnit="EA2D";
ledgerDimensions = ['ItemGroup', 'SUMP'];

 dimensionDefault=AxdDimensionUtil::getDimensionAttributeValueSetId(ledgerDimensions);
 PurchLine.LedgerDimension=dimensionDefault;


    PurchLine.PriceUnit=1.0;
    PurchLine.PurchPrice=1;
   // purchLine.RemainPurchPhysical = purchLine.PurchQty;
   //  purchLine.QtyOrdered = purchLine.calcQtyOrdered();
    // purchLine.setPriceDisc(InventDim::find(purchLine.InventDimId));
   // PurchLine.LineDisc=1;
  // PurchLine.LinePercent=1;
   // PurchLine.PurchMarkup=1;
    //PurchLine.LineAmount=PurchLine.calcLineAmount();

   PurchLine.LineAmount=1;
//PurchLine.LinePercent=1;


PurchLine.createLine(true, true, false, true, true, false);


ttscommit;


 


    test="11/30/2018";
    myDate=str2Date(test,213);


          // Create PurchParamUpdate table
    purchFormLetterParmData = PurchFormletterParmData::newData(
        DocumentStatus::PurchaseOrder,
       VersioningUpdateType::Initial);


    purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
    purchFormLetterParmData.createData(false);
    purchParmUpdate = purchFormLetterParmData.parmParmUpdate();
    purchParmUpdate.SpecQty=PurchUpdate::All;




    // Set PurchParmTable table

    purchParmTable.clear();
    purchParmTable.TransDate                = SystemDateGet();
    purchParmTable.DocumentDate = myDate;
    purchParmTable.Ordering                 = DocumentStatus::PurchaseOrder;
    purchParmTable.ParmJobStatus            = ParmJobStatus::Waiting;
    purchParmTable.PurchId                  = purchTable.PurchId;
    purchParmTable.PurchName                = purchTable.PurchName;
    purchParmTable.DeliveryName             = purchTable.DeliveryName;
    purchParmTable.DeliveryPostalAddress    = purchTable.DeliveryPostalAddress;
    purchParmTable.OrderAccount             = purchTable.OrderAccount;
    purchParmTable.CurrencyCode             = purchTable.CurrencyCode;
    purchParmTable.InvoiceAccount           = purchTable.InvoiceAccount;
    purchParmTable.ParmId                   = purchParmUpdate.ParmId;
    purchParmTable.Description="test";
    purchParmTable.Num=purchTable.PurchId;




     //if (purchParmTable.validateWrite())
        purchParmTable.insert();
  // else
       //throw Exception::Error;



         // Create PurchParamUpdate table
    purchFormletter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder); // to post invoice change to DocumentStatus::invoice

    purchFormLetter.transDate(systemDateGet());
    purchFormLetter.proforma(false);
    purchFormLetter.specQty(PurchUpdate::All);
    purchFormLetter.purchTable(purchTable);

    purchFormLetter.parmParmTableNum(purchParmTable.ParmId);
    purchFormLetter.parmId(purchParmTable.ParmId);
    purchFormLetter.purchParmUpdate(purchParmUpdate);
    purchFormLetter.run();

     

   purchFormLetterParmData = PurchFormletterParmData::newData(
   DocumentStatus::Invoice,
   VersioningUpdateType::Initial);

    purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
    purchFormLetterParmData.createData(false);
    purchParmUpdate = purchFormLetterParmData.parmParmUpdate();
    purchParmUpdate.SpecQty=PurchUpdate::All;


    purchParmTable.clear();
    purchParmTable.TransDate                = SystemDateGet();
    purchParmTable.DocumentDate = myDate;
    purchParmTable.Ordering                 = DocumentStatus::Invoice;
    purchParmTable.ParmJobStatus            = ParmJobStatus::Waiting;
    purchParmTable.PurchId                  = purchTable.PurchId;
    purchParmTable.PurchName                = purchTable.PurchName;
    purchParmTable.DeliveryName             = purchTable.DeliveryName;
    purchParmTable.DeliveryPostalAddress    = purchTable.DeliveryPostalAddress;
    purchParmTable.OrderAccount             = purchTable.OrderAccount;
    purchParmTable.CurrencyCode             = purchTable.CurrencyCode;
    purchParmTable.InvoiceAccount           = purchTable.InvoiceAccount;
    purchParmTable.ParmId                   = purchParmUpdate.ParmId;
    purchParmTable.Description="test";
    purchParmTable.Num=purchTable.PurchId;


   // purchParmTable.Description="test";


     //if (purchParmTable.validateWrite())
        purchParmTable.insert();

  

       purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);
    purchFormLetter.transDate(systemDateGet());
    purchFormLetter.proforma(false);
    purchFormLetter.specQty(PurchUpdate::All);
    purchFormLetter.purchTable(purchTable);

        purchFormLetter.parmParmTableNum(purchParmTable.ParmId);
    purchFormLetter.parmId(purchParmTable.ParmId);
    purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());
    // purchFormLetter.reArrangeNow(true);
    purchFormLetter.run();



    if (PurchTable::find(purchTable.PurchId).DocumentStatus == DocumentStatus::Invoice)
    {
    info(strfmt("Posted invoiced journal for purchase order %1",purchTable.PurchId));
    }



}

}




*This post is locked for comments

I have the same question (0)
  • Rustem Galiamov Profile Picture
    8,072 on at

    Hi Sirus49!

    Check the link community.dynamics.com/.../x-code-to-create-purchase-order-and-post-the-invoice

    Hope it will be helpful.

  • Sirus49 Profile Picture
    59 on at

    that is very basic and doesn't include all the information i need.

  • Suggested answer
    Rustem Galiamov Profile Picture
    8,072 on at

    Are you sure about this line of code:

    PurchLine.LedgerDimension = dimensionDefault;

    Maybe you want to set value to DefaultDimension field instead of LedgerDimension?

  • Rustem Galiamov Profile Picture
    8,072 on at

    Hi Sirus49!

    Did you solve the issue?

  • Sirus49 Profile Picture
    59 on at

    No i'm still having an issue.

    I updated the code a bit, i did try your default dimension but its still not showing up.  I'm more concerned about the error when trying to invoice.  

    I need to be able to properly confirm and then invoice.  I need to attach the invoice/doc date and description etc. I only setup these parameters when I go to invoice now. But at this line: 

     purchFormLetterParmData.createData(false)

    I get the follow errors Below: I'm not sure exactly what this is trying to do but it fails at that point. Is there another way to invoice and setup the parameters that I need that will work?

    List object not initialized.

    Stack trace

    (S)\Classes\PurchFormletterParmDataInvoice\fillOutputContract - line 5
    (S)\Classes\formletterParmData\createData - line 59
    (S)\Classes\PurchFormletterParmDataInvoice\createData - line 3
    (C)\Jobs\CreatePO - line 184


    this.fillOutputContract()
    outputContract.parmActiveInvoiceListPacked(SysOperationHelper::base64Encode(activeInvoiceList.pack()));



    static void CreatePO(Args _args)
    {
    NumberSeq numberSeq;
    Purchtable Purchtable;
    PurchLine PurchLine;
    PurchParmTable purchParmTable;
    PurchFormLetter purchFormLetter;
    InventDimId _inventDimId;
        InventDim inventDim;
    DocumentDate docDate;
    PurchParmUpdate purchParmUpdate;
    PurchFormletterParmData     purchFormLetterParmData;
        PurchFormletterParmData     purchFormLetterParmData2;
    Date myDate;
        str test;
        container  ledgerDimensions;
        DimensionDefault dimensionDefault;
    
    
    
    changeCompany("SomeCompany") //Inserts into specified company
    {
       //numRefPurchaseOrderId()
    ttsbegin;
    numberSeq = NumberSeq::newGetNum(purchParameters::numRefPurchId());
    _inventDimId="SomeInventDim";
    
    
    // Initialize Purchase order values
    Purchtable.initValue();
    Purchtable.PurchId = numberSeq.num();
    
    
    Purchtable.OrderAccount = 'SomeAccount';
    Purchtable.initFromVendTable();
    
    if (!Purchtable.validateWrite())
    {
    throw Exception::Error;
    }
    Purchtable.insert();
    
    
    // Initialize Purchase Line items
    
    PurchLine.clear();
    PurchLine.initValue(purchTable.PurchaseType);
    PurchLine.initFromPurchTable(Purchtable);
    PurchLine.initFromInventTable(InventTable::find('SomeID'));
    
    PurchLine.PurchId = Purchtable.PurchId;
    PurchLine.InventDimId="SomeInventID";
    
    
    PurchLine.PurchQty=1;
    PurchLine.PurchUnit="EA2D";
    ledgerDimensions = ['ItemGroup', 'SUMP'];
    
     dimensionDefault=AxdDimensionUtil::getDimensionAttributeValueSetId(ledgerDimensions);
     PurchLine.DefaultDimension=dimensionDefault;
    
    
        PurchLine.PriceUnit=1.0;
        PurchLine.PurchPrice=1;
       // purchLine.RemainPurchPhysical = purchLine.PurchQty;
       //  purchLine.QtyOrdered = purchLine.calcQtyOrdered();
        // purchLine.setPriceDisc(InventDim::find(purchLine.InventDimId));
       // PurchLine.LineDisc=1;
      // PurchLine.LinePercent=1;
       // PurchLine.PurchMarkup=1;
        //PurchLine.LineAmount=PurchLine.calcLineAmount();
    
       PurchLine.LineAmount=1;
    //PurchLine.LinePercent=1;
    
    
    
    
    PurchLine.createLine(true, true, false, true, true, false);
    
    
    ttscommit;
    
    
        test="11/30/2018";
        myDate=str2Date(test,213);
    
     purchFormLetter = purchFormLetter::construct(DocumentStatus::PurchaseOrder);
    purchFormLetter.update(purchtable, // Purchase record Buffer
    strFmt("Inv_%1", purchTable.PurchId), // Invoice Number
    systemdateget()); // Transaction date
    
          
    
    
       purchFormLetterParmData = PurchFormletterParmData::newData(
       DocumentStatus::Invoice,
       VersioningUpdateType::Initial);
    
        purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
        purchFormLetterParmData.createData(false);
    
        purchParmUpdate.SpecQty=PurchUpdate::All;
        purchParmUpdate = purchFormLetterParmData.parmParmUpdate();
    
    
    
        purchParmTable.clear();
        purchParmTable.TransDate                = SystemDateGet();
        purchParmTable.DocumentDate = myDate;
        purchParmTable.Ordering                 = DocumentStatus::Invoice;
        purchParmTable.ParmJobStatus            = ParmJobStatus::Waiting;
        purchParmTable.PurchId                  = purchTable.PurchId;
        purchParmTable.PurchName                = purchTable.PurchName;
        purchParmTable.DeliveryName             = purchTable.DeliveryName;
        purchParmTable.DeliveryPostalAddress    = purchTable.DeliveryPostalAddress;
        purchParmTable.OrderAccount             = purchTable.OrderAccount;
        purchParmTable.CurrencyCode             = purchTable.CurrencyCode;
        purchParmTable.InvoiceAccount           = purchTable.InvoiceAccount;
        purchParmTable.ParmId                   = purchParmUpdate.ParmId;
        purchParmTable.Description="test";
        purchParmTable.Num=purchTable.PurchId;
    
    
       // purchParmTable.Description="test";
    
    
    
    
        if (purchParmTable.validateWrite())
            purchParmTable.insert();
       else
          throw Exception::Error;
    
    
    
    
           purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);
        purchFormLetter.transDate(systemDateGet());
        purchFormLetter.proforma(false);
        purchFormLetter.specQty(PurchUpdate::All);
        purchFormLetter.purchTable(purchTable);
    
            purchFormLetter.parmParmTableNum(purchParmTable.ParmId);
        purchFormLetter.parmId(purchParmTable.ParmId);
        purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());
        // purchFormLetter.reArrangeNow(true);
        purchFormLetter.run();
    
    
    
    
    
    
        if (PurchTable::find(purchTable.PurchId).DocumentStatus == DocumentStatus::Invoice)
        {
        info(strfmt("Posted invoiced journal for purchase order %1",purchTable.PurchId));
        }
    
    
    
    }
    
    }
    
    
    
    
    


  • Suggested answer
    Valery Plotnik Profile Picture
    24 on at

    Hello Everyone, hope the solution will be helpful to anyone further.

    Please check this post for answer: Post purchase invoice in code

    Resuming: just call "init" method before calling "createData" method.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Ali Zaidi Profile Picture

Ali Zaidi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans