首次保存记录后,如何更新和保存记录? [英] How to update and save a record just after the record has been save for the first time?

查看:79
本文介绍了首次保存记录后,如何更新和保存记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OpportunityID字段,它是一个编号序列,当第一次保存时,我希望UsrEBDR字段更新为契机ID值。

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.

因此,在插入数据库时​​,我想将OpportunityID字段复制到UsrEBDR字段。

So on INSERTING into the database, I want to copy the OpportunityID field to the UsrEBDR Field.

OpportunityID字段是一个自动编号序列,因此在RowPersistingEvent中它尚未由框架计算。

The OpportunityID field is an Autonumbering Sequence so in the RowPersistingEvent it has not yet been calculated by the framework.

所以我尝试覆盖RowPersisted事件,但在MoveNext上遇到异常,它没有保存我的记录。

so I tried overriding the RowPersisted event but I get an exception on MoveNext and it doesnt save my record.

您有想法吗?

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();
            }
        }

编辑:
到目前为止,我知道了通过执行以下操作:

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.

推荐答案

放入您在 RowPersisting 中的逻辑,并让系统自行保存。如果只在记录持久之前修改记录,则从Persist调用Persist会导致很多问题,这些问题很容易避免。

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
}

这篇关于首次保存记录后,如何更新和保存记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆