使第一个电子地址成为主要地址

Make a first Electronic Address Primary

我想将联系人中插入的第一个电子地址(例如电子邮件地址)设为主要地址,插入地址时第二个地址可能不会自动成为主要地址。

因此我想修改table上的insert方法:

LogisticsElectronicAddress

public void insert()
{

    ttsbegin;
    if (!this.Location)
    {
        this.Location = LogisticsLocation::create('', false).RecId;
    }

    super();
    this.updatePrimary(this.IsPrimary);

/* This is the modifications I made to check for an other primary address

    if (!this.otherPrimaryExists())
        {
         this.IsPrimary = true;   
        }

*/

    ttscommit;

    this.invalidatePresenceInfo();
}

方法otherPrimaryExists()包含:

   /// <summary>
/// Checks whether primary record exists for the location.
/// </summary>
/// <returns>
/// true if another electronic address record is primary for the location; otherwise, false.
/// </returns>
public boolean otherPrimaryExists()
{
    LogisticsElectronicAddress logisticsElectronicAddress;

    select firstonly RecId from logisticsElectronicAddress
        where logisticsElectronicAddress.Location == this.Location &&
            logisticsElectronicAddress.Type == this.Type &&
            logisticsElectronicAddress.IsPrimary == true &&
            logisticsElectronicAddress.RecId != this.RecId;

    return logisticsElectronicAddress.RecId != 0;
}

问题是,所有电子地址都变成了主要地址,当我关闭表格时,所有主要标记都被删除了。如何解决这个问题?

这是因为您设置 isPrimary = true 之后您已经执行了插入并且没有调用后续更新。

简单的解决方案,只需将您的代码移到 super 调用之上。

LogisticsElectronicAddress

public void insert()
{

    ttsbegin;
    if (!this.Location)
    {
        this.Location = LogisticsLocation::create('', false).RecId;
    }

/* This is the modifications I made to check for an other primary address
// Move this here
    if (!this.otherPrimaryExists())
        {
         this.IsPrimary = true;   
        }

*/


    super();
    this.updatePrimary(this.IsPrimary);

    ttscommit;

    this.invalidatePresenceInfo();
}

修改insert方法如下:

public void insert()
{
    ;

    ttsbegin;

    if (!this.Location)
    {
        this.Location = LogisticsLocation::create('', false).RecId;
    }

    if (!this.otherPrimaryExists())
    {
        this.IsPrimary = true;
    }

    super();

    this.updatePrimary(this.IsPrimary);

    ttscommit;

    this.invalidatePresenceInfo();
}

如果在调用super后设置字段值,该值将不会存储在数据库中。