How to update and save a record just after the record has been save for the first time?

喜夏-厌秋 提交于 2019-12-11 18:33:38

问题


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
}


来源:https://stackoverflow.com/questions/53539712/how-to-update-and-save-a-record-just-after-the-record-has-been-save-for-the-firs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!