Created Invoice and auto Posted Invoiced

Currently, I am importing the records to a table then creating Invoice and automatically posting.
I have successfully created the Invoice but cannot post automatically. Please give me a solution. Thanks so much

TempImport Invoice - OnAfterGetRecord()

vCount := "TempImport Invoice".COUNT;

IF "Created Invoice" = FALSE THEN BEGIN
  IF (LastDocument <> "Document No.") THEN BEGIN

//Insert Purchase Invoice

  PurchHeader.INIT;
  PurchHeader."Document Type" := PurchHeader."Document Type"::Invoice;
  DocNo  := lCdu_NoSeries.GetNextNo('P-INV+',TODAY,TRUE);
  PurchHeader."No." := DocNo;
  PurchHeader.INSERT(TRUE);
  
  PurchHeader.VALIDATE("Buy-from Vendor No.","Account No.");
  PurchHeader.VALIDATE("Order Date", WORKDATE);
  PurchHeader.VALIDATE("Responsibility Center","Responsibility Center");

  PurchHeader."Vendor Invoice No.":="TempImport Invoice"."Document No.";
  PurchHeader.MODIFY;

  LineNo :=0;
END;
  LastDocument:="TempImport Invoice"."Document No.";
  
  PurchLineRec.RESET;
  PurchLineRec.SETCURRENTKEY("Document No.","Line No.");
  PurchLineRec.SETRANGE("Document Type",PurchHeader."Document Type");
  PurchLineRec.SETRANGE("Document No.",PurchHeader."No.");

  IF PurchLineRec.FINDLAST THEN
    LineNo := PurchLineRec."Line No." +10000
  ELSE
    LineNo += 10000;
    PurchLine.INIT;

//Create Purchase Line
    PurchLine.VALIDATE("Document Type", PurchLine."Document Type"::Invoice);
    PurchLine.VALIDATE("Document No.", PurchHeader."No.");
    PurchLine.VALIDATE("Line No.", LineNo);

    PurchLine.VALIDATE(Type,PurchLine.Type::"G/L Account" );
    PurchLine.VALIDATE("No.","G/L Account");  // -> Check G/L
    PurchLine.VALIDATE(Quantity,1);
    PurchLine.VALIDATE("Direct Unit Cost","Amount");
    PurchLine.VALIDATE("Dimension Set ID","Dimension Set ID");

    PurchLine.INSERT(TRUE);
    
    PurchLine."Dimension Set ID":="Dimension Set ID";
    PurchLine.MODIFY;

END;


//Process PPI Document
//    ReleasePurchDoc.PerformManualRelease(PurchHeader);
//    PostPurchaseInvoice(PurchHeader."No.");


TempImport Invoice- OnPostDataItem()
PurchaseHeader.SETRANGE("Vendor Invoice No.","History Dragon Payment"."Document No.");
 IF PurchaseHeader.FIND('-') THEN BEGIN
 REPEAT
  ReleasePurchDoc.PerformManualRelease(PurchHeader);
  PostPurchaseInvoice(PurchHeader."No.");
 UNTIL PurchaseHeader.NEXT=0;
END;

//Proc posted

PostPurchaseInvoice(DocNo : Code[20]) : Boolean

WITH PurchHeaderRec DO BEGIN
  PurchHeaderRec.SETRANGE("No.",DocNo);
  PurchHeaderRec.SETFILTER("Document Type",'%1',PurchHeaderRec."Document Type"::Invoice);
   IF PurchHeaderRec.FIND('-') THEN 
   BEGIN
 //     ReleasePurchDoc.PerformManualRelease(PurchHeaderRec);
      PurchPost.RUN(PurchHeaderRec);
   END;
END;

Related
Recommended