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:
Is there anything wrong with the code?
*This post is locked for comments