Search code examples
axaptadynamics-ax-2012x++

Make a first Electronic Address Primary


I want to make the first electronic address that is inserted on a contact (e.g. E-mail address) a primary address, the 2nd Address may not become primary automatic when inserting the address.

Herefore I want to modify the insert method on the table:

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

The method otherPrimaryExists() contains:

   /// <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;
}

The problem is, all Electronic address are becoming primary, and when I close the form, all primary marks are removed. How to solve this problem?


Solution

  • It's because you're setting isPrimary = true after you've already performed the insert and there are no later updates being called.

    Simple solution, just move your code above the super call.

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