-->

How to update and save a record just after the rec

2019-08-27 17:51发布

问题:

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.

回答1:

Put your logic in RowPersisting and let the system save by itself. Calling Persist from Persist leads to many problems that are easily avoided if you simply modify the record before its persisted.

void CROpportunity_RowPersisting(PXCache sender, PXRowPersistingEventArgs e, PXRowPersisting del)
{
   // Don't call save action or persist
   // Just modify data and let the system save
}


标签: acumatica