Description:
I am working on a requirement where I need to insert insurance details during the invoice posting of a sales order in Dynamics 365 Finance and Operations. Specifically, I need to retrieve certain details from the CustInvoiceTrans
table, such as the LineAmount
and InvoiceDate
, in order to insert insurance-related information.
Current Approach:
I attempted to implement the logic by extending the SalesInvoiceJournalPost
class and calling my logic after the EndPost
method as well as the PostJournalPost
method. However, I am encountering an issue where I am not able to retrieve the desired data from the CustInvoiceTrans
table. While the corresponding records are being created in the CustInvoiceJour
table, I am unable to find any records in the CustInvoiceTrans
table from which I can fetch the LineAmount
and InvoiceDate
.
Questions:
- Is there a more appropriate place to insert the logic in order to ensure that I can retrieve the required details from the
CustInvoiceTrans
table? - Would extending the
SalesFormLetter_Invoice
and calling my logic after theCreatePayment()
method be a correct approach, or is there a better method to achieve this goal? - should I create eventhandler for post ? if yes what i have to include in that
- What is the correct sequence of actions or place where I should be inserting the logic to ensure the data is available for retrieval and insertion of insurance details?
CUSTINVOICETRANS protected void InsertRetailInsuranceDetails() { SalesTable salesTable1; CustInvoiceJour custInvoiceJour = this.custInvoiceJour; CustInvoiceTrans _custInvoiceTrans = custInvoiceTrans; real totalLineAmount = 0; real totalPcs = 0; SalesLine _salesLine; NewSalesLine newSalesLine; RetailInvoiceTable insuranceTable; RetailInsuranceTable retailInsuranceTable; CustInvoiceId invoiceno =custInvoiceJour.InvoiceId; definedParameter _definedparameter = definedParameter::find(); while select LineAmount, SalesId from _custInvoiceTrans where _custInvoiceTrans.InvoiceId == custInvoiceJour.InvoiceId { totalLineAmount += _custInvoiceTrans.LineAmount; } select firstOnly salesTable1 where salesTable1.SalesId == _custInvoiceTrans.SalesId; if (totalLineAmount > _definedparameter.MinInsuranceValue) { select sum(Pcs) from newSalesLine where newSalesLine.SalesId == salesTable1.SalesId; totalPcs = newSalesLine.Pcs; select firstOnly retailInsuranceTable where retailInsuranceTable.Store == salesTable.InventSiteId; insuranceTable.clear(); insuranceTable.TransactionID = salesTable1.SalesId; insuranceTable.InsuranceAmount = totalLineAmount; insuranceTable.RetailInsuranceNo = salesTable1.SalesId; insuranceTable.InsuranceDate = _custInvoiceTrans.InvoiceDate; insuranceTable.PolicyToDate = _custInvoiceTrans.InvoiceDate + retailInsuranceTable.PeriodInsuranceDays; insuranceTable.MasterPolicyNo = retailInsuranceTable.MasterPolicyNo; insuranceTable.PeriodInsuranceDays = retailInsuranceTable.PeriodInsuranceDays; insuranceTable.PCS = totalPcs; ttsbegin; try { insuranceTable.insert(); } catch { throw error("Insurance insertion failed."); } ttscommit; } }
I shared you basic snippet logic what i want to insert while posting .
Thanks,
Ayushaman