Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

How to post Purchase invoice on post of sales invoice autoomatically in intercompany order.

Posted on by 182

I want to post purchase invoice on post of sales invoice automatically in intercompany order.

sales order & purchase order are connected using shortcut dimension2.

the situation is like below:

I have created the function like below:

 

Function: AutoPostPI

 

PurchOrderHeader.RESET;

PurchOrderHeader.GET(PurchOrderHeader."Document Type"::Order, SalesLine."Purchase Order No.");

IF PurchOrderHeader."Shortcut Dimension 2 Code" = SalesInvHeader."Shortcut Dimension 2 Code" THEN BEGIN

   MESSAGE('%1',PurchOrderHeader."Shortcut Dimension 2 Code");

     

 

       IF Vendor.GET(PurchOrderHeader."Buy-from Vendor No.") THEN BEGIN

               MESSAGE('%1',PurchOrderHeader."Buy-from Vendor No.");

 

               IF Vendor."IC Partner Code" <> '' THEN

                 BEGIN

 

                    PurchOrderLine.RESET;

                    PurchOrderLine.SETRANGE("Document Type", PurchOrderHeader."Document Type");

                    PurchOrderLine.SETRANGE("Document No.", PurchOrderHeader."No.");

 

                  IF PurchOrderLine.FINDSET THEN

                   BEGIN

 

                     PurchOrderLine.CALCSUMS("Qty. Rcd. Not Invoiced");

 

                     IF PurchOrderLine."Qty. Rcd. Not Invoiced" = 0 THEN EXIT;

 

                     PurchOrderLine.RESET;

                     

                     PurchOrderLine.SETRANGE("Document No.",PurchOrderHeader."No.");

 

                     PurchOrderLine.FINDSET;

 

                     PurchOrderLine.MODIFYALL("Qty. to Invoice",0);

                     PurchOrderLine.MODIFYALL("Qty. to Invoice (Base)",0);

 

 

                    PurchOrderLine.SETFILTER("Qty. Rcd. Not Invoiced",'>0');

 

 

                    IF PurchOrderLine.FIND('-') THEN

                      REPEAT

 

                        PurchOrderLine.VALIDATE("Qty. to Invoice",PurchOrderLine."Qty. Rcd. Not Invoiced");

                        PurchOrderLine.VALIDATE("Qty. to Invoice (Base)",PurchOrderLine."Qty. Rcd. Not Invoiced");

 

                        PurchOrderLine."Qty. to Invoice" := PurchOrderLine."Qty. Rcd. Not Invoiced";

                        PurchOrderLine."Qty. to Invoice (Base)" := PurchOrderLine."Qty. Rcd. Not Invoiced";

                      

                        PurchOrderLine.MODIFY;

 

                      UNTIL PurchOrderLine.NEXT <=0;

                   END;

                  ICPartner.RESET;

                  ICPartner.SETRANGE(Code,Vendor."IC Partner Code");

 

                  IF ICPartner.FINDSET THEN

                    BEGIN

 

                       SalesInvHeader2.RESET;

                       SalesInvHeader2.CHANGECOMPANY(ICPartner."Inbox Details");

                       SalesInvHeader2.SETRANGE("External Document No.",PurchOrderHeader."Purchase Order No.");

 

 

                       IF SalesInvHeader2.FINDLAST THEN

                            VendorInvNo := SalesInvHeader2."No.";

                     END;

 

                  IF VendorInvNo = '' THEN

                   VendorInvNo := SalesInvHeader."No.";

                    PurchOrderHeader."Posting Date" := WORKDATE;

                    PurchOrderHeader."Vendor Invoice No.":= VendorInvNo;

                    PurchOrderHeader.Invoice := TRUE;

                    PurchOrderHeader.Receive := FALSE;

 

                    PurchOrderHeader.MODIFY;

 

                   "Purch.-Post".RUN( PurchOrderHeader);

 

                   EXIT;

              END;

          END;

 

      END;

 

I have added the Function at the end of function the function: ONRUN:

 

But when I try to post the order it gives error below:

 

If I press yes then I get below error:

4466.Capture.PNG

 

Is there anything wrong with the code?

*This post is locked for comments

  • Mitr Profile Picture
    Mitr 182 on at
    RE: How to post Purchase invoice on post of sales invoice autoomatically in intercompany order.

    Sorry error is this:

    8764.Capture.PNG

  • Mitr Profile Picture
    Mitr 182 on at
    RE: How to post Purchase invoice on post of sales invoice autoomatically in intercompany order.

    With that code If I post the whole sales order to invoice , it posts the the sales invoice & whole purchase order to invoice.

    But If I post only one line or some quantity only it gives error:

    5633.Capture.PNG

    When I debug the errror lines are below:

    5633.Capture.PNG

    I want to know how to post only few lines in slaes order & corresponding purchase order lines to invoice.

    Please provide your comments.

  • Mitr Profile Picture
    Mitr 182 on at
    RE: How to post Purchase invoice on post of sales invoice autoomatically in intercompany order.

    Hello Alexender,

    Thanks for response.

    But I want to post only one PI at a time with lined SI.

    Is there anything wrong with my code.

    Please provide your comments.

    Thanks

  • Suggested answer
    Alexander Ermakov Profile Picture
    Alexander Ermakov 28,094 on at
    RE: How to post Purchase invoice on post of sales invoice autoomatically in intercompany order.

    You can instead look at the Batch Post Sales Orders Batch Job (msdn.microsoft.com/.../hh169230(v=nav.90).aspx) for posting multiple purchase orders that already exists. Perhaps, would be easier to run this job with needed filters, if you already have needed unposted I/C purchase orders in the system?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans