I've got the OpportunityID field which is a numbering sequence, and when it's saved for the first time I want the UsrEBDR Field to be updated with the opportunityID value.
So on INSERTING into the database, I want to copy the OpportunityID field to the UsrEBDR Field.
The OpportunityID field is an Autonumbering Sequence so in the RowPersistingEvent it has not yet been calculated by the framework.
so I tried overriding the RowPersisted event but I get an exception on MoveNext and it doesnt save my record.
Do you have an idea?
protected virtual void CROpportunity_RowPersisted(PXCache sender, PXRowPersistedEventArgs e, PXRowPersisted del)
{
if (del != null) del(sender, e);
CROpportunity row = e.Row as CROpportunity;
if(row == null) return;
if(e.Operation == PXDBOperation.Insert && string.IsNullOrWhiteSpace(row.GetExtension<CROpportunityExt>().UsrEBDR) && e.TranStatus == PXTranStatus.Open)
{
Base.Opportunity.Current.GetExtension<CROpportunityExt>().UsrEBDR = row.OpportunityID;
Base.Persist();
}
}
Edit: Thus far I got it working by doing this :
protected virtual void CROpportunity_RowPersisted(PXCache sender, PXRowPersistedEventArgs e, PXRowPersisted del)
{
if (del != null) del(sender, e);
CROpportunity row = e.Row as CROpportunity;
if(row == null) return;
if(e.Operation == PXDBOperation.Insert && string.IsNullOrWhiteSpace(row.GetExtension<CROpportunityExt>().UsrEBDR) && e.TranStatus == PXTranStatus.Completed)
{
row.GetExtension<CROpportunityExt>().UsrEBDR = row.OpportunityID;
using(PXTransactionScope ts = new PXTransactionScope())
{
var restrictOpportunityId = new PXDataFieldRestrict<CROpportunity.opportunityID>(row.OpportunityID);
var assignEBDR = new PXDataFieldAssign<CROpportunityExt.usrEBDR>(row.OpportunityID);
PXDatabase.Update<CROpportunity>(assignEBDR, restrictOpportunityId);
ts.Complete();
}
}
}
Yet I feel it's an improper use of the framework, and it's not really "clean", if someone has any idea how to make it clean.