更新客户的默认联系人添加新记录

Updating default contact on customer adds new record

我有一个使用 CustomerMaint 图添加或更新客户的方法。

我的方法与正在发生的一件奇怪的事情不同。

插入客户后。如果我通过更新联系人的方法,则会在联系人 table 上创建第二条联系人记录。如果我再次更新它会正常运行并且不会创建新的联系人记录并且会更新默认联系人记录。

这是我的方法

    private PX.Objects.AR.Customer UpdateContact(ContactRead rexContact, PX.Objects.AR.Customer m, bool insert = true)
    {

        PX.Objects.CR.Contact defContact = null;

        PX.Objects.AR.CustomerMaint graph = PXGraph.CreateInstance<PX.Objects.AR.CustomerMaint>();

        graph.Clear(PXClearOption.ClearAll);

        //Add Customer and BAccount Records
        m.AcctCD = "V" + rexContact._id;
        m.AcctName = rexContact.system_search_key;
        m.Type = "CU";

        if (insert) {
            m = graph.CurrentCustomer.Insert(m);
            defContact = graph.DefContact.Current;
        }  
        else {
            defContact = PXSelect<PX.Objects.CR.Contact, Where<PX.Objects.CR.Contact.contactID, Equal<Required<PX.Objects.CR.Contact.contactID>>>>.Select(this, m.DefContactID);
        }

        //Update Default Contact Record
        defContact.ContactType = "AP";
        defContact.FullName = rexContact.system_search_key;

        if (rexContact._related.contact_emails != null)
        {
            if (rexContact._related.contact_emails.Length > 0) defContact.EMail = rexContact._related.contact_emails[0].email_address;
        }

        if (rexContact._related.contact_phones != null)
        {
            if (rexContact._related.contact_phones.Length > 0) defContact.Phone1 = rexContact._related.contact_phones[0].phone_number;
        }

        graph.DefContact.Update(defContact);

        //Change customer class to vendor
        m.CustomerClassID = "VENDOR";
        graph.CurrentCustomer.Update(m);

        graph.Actions.PressSave();

        return m;

    }

您似乎是在与主视图不同的视图中插入客户。在 graph.CurrentCustomer 中插入内容不会正确设置当前记录,这可能会导致在您尝试访问 graph.DefContact.Current 时出现不可预知的行为。您应该将联系人插入 graph.BAccount,这是客户维护图表的主要视图。

此外,当您更新现有客户的联系人(而不是插入新客户)时,您仍应将 graph.BAccount.Current 值设置为您使用 LocatePXSelect,并使用 graph.DefContact.Select() 检索默认联系人,而不是使用完整的 PXSelect