Copy Sales release Tax to Invoice Line tax

Hey Guys,
Trying to figure out a BPM, on how to copy the sales release Tax to Invoice Line tax. The Method i am trying to create on is GetShipments.

Any insights would be appreciated.

Thanks,
Dishant

try
{
    // Fetch the Invoice Number from the result set (or input parameters)
    int invoiceNum = result.InvcHead.FirstOrDefault()?.InvoiceNum ?? 0; // Adjust based on your logic

    if (invoiceNum > 0)
    {
        // Fetch the Invoice Header from the database
        var invhd = Db.InvcHead.FirstOrDefault(i => i.InvoiceNum == invoiceNum);
        
        if (invhd != null)
        {
            // Continue with your logic to fetch invoice details
            var invdtList = Db.InvcDtl.Where(t => t.Company == invhd.Company && t.InvoiceNum == invhd.InvoiceNum).ToList();
            
            foreach (var invd in invdtList)
            {
                // Fetch InvcDtl and process the Tax Code logic
                int invoiceLine = invd.InvoiceLine;

                // Check if the tax record already exists
                var existingTax = Db.InvcTax.FirstOrDefault(t => 
                    t.Company == invd.Company &&
                    t.InvoiceNum == invoiceNum && 
                    t.InvoiceLine == invoiceLine);
                
                // Fetch the related OrderRelTax for details
                var orderRelTax = Db.OrderRelTax.FirstOrDefault(t =>
                                  t.Company == invd.Company &&           // Match company
                                  t.OrderNum == invd.OrderNum &&         // Match order number (assuming invd has OrderNum property)
                                  t.OrderLine == invd.OrderLine &&       // Match order line (assuming invd has OrderLine property)
                                  t.OrderRelNum == invd.OrderRelNum);
                
                // Initialize variables
                string taxCode = string.Empty;  // Initialize taxCode
                string rateCode = string.Empty;  // Initialize rateCode
                bool manualSelect = false;       // Initialize manualSelect
                decimal percent = 0;
                decimal reportable = 0;
                decimal TaxableAmt = 0;
                decimal TaxAmt = 0;

                if (orderRelTax != null)
                {
                    // Assign values from OrderRelTax
                    taxCode = orderRelTax.TaxCode;     
                    rateCode = orderRelTax.RateCode;   
                    manualSelect = orderRelTax.Manual;
                    percent = orderRelTax.Percent;
                    reportable = orderRelTax.ReportableAmt;
                    TaxableAmt = orderRelTax.TaxableAmt;
                    TaxAmt = orderRelTax.TaxAmt;
                }

                // Create or update the InvcTax record
                using (var invtxsvc = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.ARInvoiceSvcContract>(Db))
                {
                    Erp.Tablesets.ARInvoiceTableset arData = new Erp.Tablesets.ARInvoiceTableset();
                    
                    // If existingTax is not found, get a new tax record
                    if (existingTax == null)
                    {
                        // Call GetNewInvcTax with the appropriate parameters
                        invtxsvc.GetNewInvcTax(ref arData, invhd.InvoiceNum, invoiceLine, taxCode, rateCode);
                        var invt = arData.InvcTax.FirstOrDefault();
                        if (invt != null)
                        {
                            // Assign values and update
                            invt.Company = invhd.Company;
                            invt.InvoiceNum = invhd.InvoiceNum;
                            invt.InvoiceLine = invoiceLine;
                            invt.TaxCode = taxCode;
                            invt.RateCode = rateCode;
                            invt.Percent = percent;
                            invt.Manual = manualSelect;

                            // Only update Tax amounts if manualSelect is true
                            if (manualSelect)
                            {
                                invt.DocTaxAmt = TaxAmt; // Assuming TaxAmt is assigned correctly
                                invt.DocTaxableAmt = TaxableAmt; // Assuming TaxableAmt is assigned correctly
                                invt.DocReportableAmt = reportable; // Assuming reportable is assigned correctly
                            }

                            // Update the InvcTax record
                            invtxsvc.Update(ref arData);
                            this.Log(BusinessObjectMessageType.Information, 
                                $"Created new InvcTax for InvoiceNum:{invhd.InvoiceNum}, InvoiceLine:{invoiceLine}, TaxCode: {taxCode}");
                        }
                        else
                        {
                            throw new Exception("No InvcTax record was created after calling GetNewInvcTax.");
                        }
                    }
                    else
                    {
                        // If the tax record exists, update the existing record
                        this.Log(BusinessObjectMessageType.Information, 
                            $"Updating existing InvcTax for InvoiceNum: {invoiceNum}, InvoiceLine: {invoiceLine}, TaxCode: {taxCode}");
                        
                        existingTax.TaxCode = taxCode;
                        existingTax.RateCode = rateCode; // Update the RateCode
                        existingTax.Manual = manualSelect; // Update the Manual flag
                        existingTax.Percent = percent;

                        // Only update Tax amounts if manualSelect is true
                        if (manualSelect)
                        {
                            existingTax.DocTaxAmt = TaxAmt; // Update TaxAmt
                            existingTax.DocTaxableAmt = TaxableAmt; // Update TaxableAmt
                            existingTax.DocReportableAmt = reportable; // Update ReportableAmt
                        }
                        
                        // Prepare the update
                        //arData.InvcTax.Add(existingTax); // Mark the existing record for update
                        invtxsvc.Update(ref arData); // Perform the update
                    }
                }
            }
        }
        else
        {
            throw new Exception("No InvcHead record found for the specified InvoiceNum.");
        }
    }
    else
    {
        throw new Exception("Invalid or missing InvoiceNum.");
    }
}
catch (Exception ex)
{
    throw new Exception("Error during the tax code setup process.", ex);
}

I did come up with an idea, and this above code seems to work, instead of GetShipments i tried on GetByID, but, the data won’t show or update if Hit refresh is not pressed after the GetById BO call when the getshipments is done…any inights to update the code or to move it on another BO??

So, after some trial and error i found its best to place this code in data directive standard with some tweeks…it works there…