Skip to main content
Question

SO Error : Document out of Balance

  • November 11, 2024
  • 6 replies
  • 209 views

Sagar Greytrix
Captain II
Forum|alt.badge.img+3

Hi Team,

 

I have created AP Bill using AP Bills and Adjustment screen and on a custom button action I have created a Debit Adjustment out of it. Now, I wanted to apply the newly created debit adjustment against that bill. However, I am getting an issue of “Document out of Balance” error while saving the same.

Below is the code I have written which seems to be failing on (paymentGraph.Actions.PressSave()).

 

APPaymentEntry paymentGraph = PXGraph.CreateInstance<APPaymentEntry>();

APInvoice fetchedDebitAdj = PXSelect<APInvoice,
    Where<APInvoice.docType, Equal<Required<APInvoice.docType>>,
    And<APInvoice.refNbr, Equal<Required<APInvoice.refNbr>>>>>
    .Select(Base, APDocType.DebitAdj, debitAdj.RefNbr);

APPayment newPayment = paymentGraph.Document.Insert(new APPayment
{
    VendorID = apinvoice.VendorID,
    DocType = APDocType.DebitAdj,  
    CuryOrigDocAmt = fetchedDebitAdj.CuryOrigDocAmt, 
    Hold = false // Set hold status
});

// Link the Debit Adjustment reference number
newPayment.RefNbr = fetchedDebitAdj.RefNbr;
paymentGraph.Document.Update(newPayment);

// Insert APAdjust for applying the Debit Adjustment to the invoice
APAdjust adjustment = paymentGraph.Adjustments.Insert(new APAdjust
{
    AdjgDocType = newPayment.DocType,
    AdjgRefNbr = newPayment.RefNbr,
    AdjdDocType = fetchedDebitAdj.DocType,     
    AdjdRefNbr = fetchedDebitAdj.RefNbr,       
    CuryAdjgAmt = fetchedDebitAdj.CuryOrigDocAmt.Value,  
    CuryAdjdAmt = fetchedDebitAdj.CuryOrigDocAmt.Value,  
    AdjAmt = fetchedDebitAdj.OrigDocAmt.Value  
});

newPayment.CuryUnappliedBal = 0;
newPayment.UnappliedBal = 0;
paymentGraph.Document.Update(newPayment);
paymentGraph.Actions.PressSave();
paymentGraph.releaseFromHold.Press(); 
PXLongOperation.WaitCompletion(paymentGraph.UID);

 

Thank you for any help in advance.

 

Regards,

Sagar

6 replies

Sagar Greytrix
Captain II
Forum|alt.badge.img+3
  • Author
  • Captain II
  • November 11, 2024

Here is the updated code below

 

 APPaymentEntry paymentGraph = PXGraph.CreateInstance<APPaymentEntry>();
 APInvoice fetchedDebitAdj = PXSelect<APInvoice,
     Where<APInvoice.docType, Equal<Required<APInvoice.docType>>,
     And<APInvoice.refNbr, Equal<Required<APInvoice.refNbr>>>>>
     .Select(Base, APDocType.DebitAdj, debitAdj.RefNbr); // Ensure using the correct type

 APPayment newPayment = paymentGraph.Document.Insert(new APPayment
 {
     VendorID = apinvoice.VendorID,
     DocType = APDocType.Check, 
     Hold = true // Set on hold initially
 });

 newPayment.RefNbr = fetchedDebitAdj.RefNbr; 
 paymentGraph.Document.Update(newPayment);

 APAdjust adjustment = new APAdjust
 {
     AdjgDocType = newPayment.DocType,
     AdjgRefNbr = newPayment.RefNbr,
     AdjdDocType = apinvoice.DocType,
     AdjdRefNbr = apinvoice.RefNbr,
     CuryAdjgAmt = fetchedDebitAdj.CuryOrigDocAmt,
     CuryAdjdAmt = fetchedDebitAdj.CuryOrigDocAmt,
     AdjAmt = fetchedDebitAdj.OrigDocAmt
 };

 paymentGraph.Adjustments.Insert(adjustment);
 newPayment.Hold = false; // Remove hold status
 paymentGraph.Actions.PressSave(); // Save the payment document
 PXLongOperation.WaitCompletion(paymentGraph.UID);

 

@Naveen Boga  Any thoughts.


jinin
Pro I
Forum|alt.badge.img+11
  • Pro I
  • November 11, 2024

Hi @Sagar Greytrix ,

Try the code sample below. 

Assign the correct value for each field.

APPaymentEntry docgraph = PXGraph.CreateInstance<APPaymentEntry>();

try
{
APInvoice fetchedDebitAdj = PXSelect<APInvoice, Where<APInvoice.docType, Equal<Required<APInvoice.docType>>,
And<APInvoice.refNbr, Equal<Required<APInvoice.refNbr>>>>>.Select(docgraph, APDocType.DebitAdj, debitAdj.RefNbr);

APPayment payment = new APPayment()
{
DocType = paymentType,
};

payment = docgraph.Document.Insert(payment);
payment.VendorID = invoice.VendorID;
payment = docgraph.Document.Update(payment);
payment.PaymentMethodID = PaymentMethodID;
payment.ExtRefNbr = "Assign the RefNbr";
payment.BranchID = invoice.BranchID;
payment.Hold = false;
payment.CashAccountID = CashAccountID;
payment.DocDesc = invoice.DocDesc;
string AdjDate = Convert.ToDateTime(invoice.DocDate).ToString("yyyy-MM-dd");
payment.AdjDate = Convert.ToDateTime(AdjDate);
payment.CuryOrigDocAmt = Paymentprice;
payment = docgraph.Document.Update(payment); // Update payment here

APAdjust adj = new APAdjust()
{
AdjdDocType = DocType,
AdjdRefNbr = RefNbr,
};

docgraph.Adjustments.Insert(adj);
docgraph.Persist();
docgraph.release.Press();
}
catch (Exception ex)
{
throw new PXException(ex.Message?.ToString());

}

 


Sagar Greytrix
Captain II
Forum|alt.badge.img+3
  • Author
  • Captain II
  • November 12, 2024

Hi jinin,

 

Thank you for looking into it.

 

I have modified some of the code and now it is not getting the reference number in the detail section.

APInvoice fetchedDebitAdj = PXSelect<APInvoice, Where<APInvoice.docType, Equal<Required<APInvoice.docType>>,
                            And<APInvoice.refNbr, Equal<Required<APInvoice.refNbr>>>>>.Select(docgraph, APDocType.DebitAdj, debitAdj.RefNbr);

APPayment payment = new APPayment()
{
    DocType = APDocType.DebitAdj,
};

payment = docgraph.Document.Insert(payment);
//payment.VendorID = apinvoice.VendorID;
//payment = docgraph.Document.Update(payment);
//payment.PaymentMethodID = 
payment.DocType = APDocType.DebitAdj;
payment.RefNbr = fetchedDebitAdj.RefNbr;
payment.BranchID = apinvoice.BranchID;
payment.Hold = false;
//payment.CashAccountID = CashAccountID;
payment.DocDesc = apinvoice.DocDesc;
string AdjDate = Convert.ToDateTime(apinvoice.DocDate).ToString("yyyy-MM-dd");
payment.AdjDate = Convert.ToDateTime(AdjDate);
payment.CuryOrigDocAmt = fetchedDebitAdj.CuryOrigDocAmt;
payment = docgraph.Document.Update(payment); // Update payment here

APAdjust adj = new APAdjust()
{
    AdjgDocType = payment.DocType,
    AdjgRefNbr = payment.RefNbr,
    AdjdDocType = apinvoice.DocType,
    AdjdRefNbr = apinvoice.RefNbr,
};

docgraph.Adjustments.Insert(adj);

docgraph.Persist();
docgraph.release.Press();

 

Regards,

Sagar Gadge

 


jinin
Pro I
Forum|alt.badge.img+11
  • Pro I
  • November 12, 2024

Hi jinin,

 

Thank you for looking into it.

 

I have modified some of the code and now it is not getting the reference number in the detail section.

APInvoice fetchedDebitAdj = PXSelect<APInvoice, Where<APInvoice.docType, Equal<Required<APInvoice.docType>>,
                            And<APInvoice.refNbr, Equal<Required<APInvoice.refNbr>>>>>.Select(docgraph, APDocType.DebitAdj, debitAdj.RefNbr);

APPayment payment = new APPayment()
{
    DocType = APDocType.DebitAdj,
};

payment = docgraph.Document.Insert(payment);
//payment.VendorID = apinvoice.VendorID;
//payment = docgraph.Document.Update(payment);
//payment.PaymentMethodID = 
payment.DocType = APDocType.DebitAdj;
payment.RefNbr = fetchedDebitAdj.RefNbr;
payment.BranchID = apinvoice.BranchID;
payment.Hold = false;
//payment.CashAccountID = CashAccountID;
payment.DocDesc = apinvoice.DocDesc;
string AdjDate = Convert.ToDateTime(apinvoice.DocDate).ToString("yyyy-MM-dd");
payment.AdjDate = Convert.ToDateTime(AdjDate);
payment.CuryOrigDocAmt = fetchedDebitAdj.CuryOrigDocAmt;
payment = docgraph.Document.Update(payment); // Update payment here

APAdjust adj = new APAdjust()
{
    AdjgDocType = payment.DocType,
    AdjgRefNbr = payment.RefNbr,
    AdjdDocType = apinvoice.DocType,
    AdjdRefNbr = apinvoice.RefNbr,
};

docgraph.Adjustments.Insert(adj);

docgraph.Persist();
docgraph.release.Press();

 

Regards,

Sagar Gadge

 


Is the payment number you are trying to add loading the screen? Did you check that?


Sagar Greytrix
Captain II
Forum|alt.badge.img+3
  • Author
  • Captain II
  • November 13, 2024

Yes, It is loading the screen.


Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • January 22, 2025

Hi ​@Sagar Greytrix were you able to find a solution? Thank you!