Function - AR Invoice Update

Any help to help me with this one?

I have this code

// Declare variables
int invNum = this.InvoiceNum;
bool bSmoothed = this.Smoothed;
DateTime? dSmoothDate = this.SmoothDate;
string sLegalNum = this.SmoothLegalNum;
int iPeriod = this.SmoothPeriod;
int iYear = this.SmoothYear;
bool success = false; // Variable to track if the invoice update was successful
string errorMessage = string.Empty; // Variable to store the detailed error message
string grpID = "";

try {
    Erp.Tablesets.ARInvoiceTableset invTS = null;
    
    this.CallService<Erp.Contracts.ARInvoiceSvcContract>(bo => 
    {
        // Get Invoice By ID
        invTS = bo.GetByID(invNum);
        
        // Create buffer
        var origRow = invTS.InvcHead.NewRow();
        BufferCopy.Copy(invTS.InvcHead[0], origRow);
        invTS.InvcHead.Add(origRow);
        
        
        // Loop data  
        foreach (var dtRows in invTS.InvcHead)
        {
            dtRows.SetUDField("IncomeSmoothed_c", bSmoothed);
            dtRows.SetUDField("SmoothPeriod_c", iPeriod);
            dtRows.SetUDField("SmoothLegalNum_c", sLegalNum);
            dtRows.SetUDField("SmoothYear_c", iYear);
            dtRows.SetUDField("SmoothDate_c", dSmoothDate);
            dtRows.RowMod = "U";
            dtRows.GroupID = grpID;
        }
        
        decimal grpTotalInvAmt = 0;
        string opGenMessage = "";
        string opLtrCrdMsg = "";
        bool lUpdateRan = false;
        bool genAmortSched = false;
        
        bo.UpdateMaster(ref invTS, grpID, "InvcHead", false, false, ref genAmortSched, false, invNum, 1, "", false, invTS.InvcHead[0].InvoiceAmt, false, out grpTotalInvAmt, out opGenMessage, out opLtrCrdMsg, out lUpdateRan);
        
        success = true;
    });
 /*         
    this.CallService<Erp.Contracts.ARInvoiceSvcContract>(bo => { 
        bo.Update(ref invTS);
        success = true; // Invoice update was successful
    });*/
    
} catch (Exception ex) {
    success = false;
    errorMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
}

if (success) {
    InfoMsg = "Invoice updated successfully.";
} else {
    InfoMsg = "Failed to update the invoice. Error: " + errorMessage;
}

It’s currently a function the data being passed in.

{
  "InvoiceNum": 200041,
  "Smoothed": true,
  "SmoothDate": "2023-06-28T10:20:52.599Z",
  "SmoothLegalNum": "2024-GJ-00000001",
  "SmoothPeriod": 2,
  "SmoothYear": 2024,
  "grpID": "JY2006"
}

I keep getting

{
  "InfoMsg": "Failed to update the invoice. Error: Group is required."
}

I can’t seem to find what it means by Group is required…

I think you can only edit invoices when they exist within a group before they are posted.

3 Likes

A uBAQ performs an update perfectly… :confused:

1 Like

Change the code a bit…

No luck :poop:

// Declare variables
int invNum = this.InvoiceNum;
bool bSmoothed = this.Smoothed;
DateTime? dSmoothDate = this.SmoothDate;
string sLegalNum = this.SmoothLegalNum;
int iPeriod = this.SmoothPeriod;
int iYear = this.SmoothYear;
bool success = false; // Variable to track if the invoice update was successful
string errorMessage = string.Empty; // Variable to store the detailed error message
string grpID = "";
bool continueProcessingOnError = true;
bool rollbackParentOnChildError = false;
string opGenMessage = "";
string opLtrCrdMsg = "";
bool lUpdateRan = false;
bool genAmortSched = false;
bool errorsOccurred = false;


try {
    Erp.Tablesets.UpdExtARInvoiceTableset invTS = null;
    
    this.CallService<Erp.Contracts.ARInvoiceSvcContract>(bo => 
    {
        // Get Invoice By ID
        //invTS = bo.GetByID(invNum);
        
        // Create buffer
        var origRow = invTS.InvcHead.NewRow();
        BufferCopy.Copy(invTS.InvcHead[0], origRow);
        invTS.InvcHead.Add(origRow);
        
        
            invTS.InvcHead[0].SetUDField("IncomeSmoothed_c", bSmoothed);
            invTS.InvcHead[0].SetUDField("SmoothPeriod_c", iPeriod);
            invTS.InvcHead[0].SetUDField("SmoothLegalNum_c", sLegalNum);
            invTS.InvcHead[0].SetUDField("SmoothYear_c", iYear);
            invTS.InvcHead[0].SetUDField("SmoothDate_c", dSmoothDate);
            invTS.InvcHead[0].InvoiceNum = invNum;
            
           
           invTS.InvcHead[0].RowMod = "U";
          
          bo.UpdateExt(ref invTS, continueProcessingOnError, rollbackParentOnChildError, out errorsOccurred);
        
        success = true;
    });
 /*         
    this.CallService<Erp.Contracts.ARInvoiceSvcContract>(bo => { 
        bo.Update(ref invTS);
        success = true; // Invoice update was successful
    });*/
    
} catch (Exception ex) {
    success = false;
    errorMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
}

if (success) {
    InfoMsg = "Invoice updated successfully.";
} else {
    InfoMsg = "Failed to update the invoice. Error: " + errorMessage;
}


{
  "InfoMsg": "Failed to update the invoice. Error: Group is required."
}

Removal of grpID as kevin said this was stupid using his …

For anyone confused, he was emptying his grpID he passed in as an argument.
Nothing had a grpID :rofl:

Have you been able to do the update? I’m in a similar situation.

I used a uBAQ in the end…

Finally, this is working for me.

// Declaro variables
int invNum = this.InvoiceNum;
bool isSent = false;
bool success = false; // Variable que indica si la actualización fue correcta
string errorMessage = string.Empty; // Variable que almacena el mesaje de error
bool continueProcessingOnError = true;
bool rollbackParentOnChildError = false;
string opGenMessage = "";
string opLtrCrdMsg = "";
string InfoMsg = string.Empty;
bool lUpdateRan = false;
bool genAmortSched = false;
bool errorsOccurred = false;
decimal grpTotalInvAmt = 0;

try {
    
    this.CallService<Erp.Contracts.ARInvoiceSvcContract>(bo => 
    {
        
        // Obtenigo la factura por el ID
        var dsInvoice = bo.GetByID(invNum);
        var originalRow = dsInvoice.InvcHead.NewRow();
        BufferCopy.Copy(dsInvoice.InvcHead[0], originalRow);
        
        dsInvoice.InvcHead.Add(originalRow);
        dsInvoice.InvcHead[0].SetUDField("isSent_c", true);
        dsInvoice.InvcHead[0].RowMod = "U";    
          
bo.UpdateMaster(ref dsInvoice, dsInvoice.InvcHead[0].GroupID, "InvcHead", false, false, ref genAmortSched, false, dsInvoice.InvcHead[0].InvoiceNum, 1, "", false, dsInvoice.InvcHead[0].InvoiceAmt, false, out grpTotalInvAmt, out opGenMessage, out opLtrCrdMsg, out lUpdateRan);
        success = true;
    });
    
} catch (Exception ex) {
    success = false;
    errorMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
}

if (success) {
    InfoMsg = "Factura actualizada exitosamente.";
} else {
    InfoMsg = "Fallo al actualizar la factura. Error: " + errorMessage;
}
5 Likes

Well done man

Bien hecho