[AX2009] Adding custromer's address form code

Hi,

I wan’t to add address (public) to customer which already exists.

Can I add it directly to Address table filling AddrRecId and AddrTableId fileds or should I use dedicated class/method to do this (which class?)?

I was looking for but have not found the exact answer to the forum.

Regards,

arnam

…can I do following after inserting or updating address:

_DirPartyTable = DirParty::createPartyFromCommon(_CustTable);

[…]

_Address.AddrRecId = _DirPartyTable.RecId;

_Address.AddrTableId = tableNum(DirPartyTable);

if (_ForInsert)

{

_Address.insert();

DirParty::createPartyFromCommon(_Address);

}

else

{

_Address.update();

DirParty::updatePartyFromAddress(_Address);

}

…it doesn’t work. There is a problem while generating NumSeq.

Whether anyone know different solution?

Hi,

static void testCustAddress(Args _args)
{

CustTable custTable;

;

custTable = custTable::find(‘10000’);

custTable.Street = ‘Queen Street’;
CustTable.City = ‘London’;

dirparty::updatePartyFromCommonInsert(custTable.PartyId, custTable);

}

static void testCustAddress(Args _args)
{

CustTable custTable;
;

custTable = custTable::find(‘10000’);
custTable.Street = ‘Queen Street’;
CustTable.City = ‘London’;

dirparty::updatePartyFromCommonInsert(custTable.PartyId, custTable);

}

Hi,

Refer to the above job. That job updates addresses for an existing customer. Hope this helps.

Hi,

Thank you for the answer.

As far I can see this code only insterts/updates primiary adres. What if I want to insert/update additional address?

Regards,

arnam

Hi,

static void testCustAddress(Args _args)
{

CustTable custTable;
address address;
dirpartyTable dirPartyTable;

;

custTable = custTable::find(‘10000’);
custTable.Street = ‘King Street’;
CustTable.City = ‘London’;
custTable.CountryRegionId = ‘UK’;
custtable.ZipCode = ‘25126’;

dirparty::updatePartyFromCommonInsert(custTable.PartyId, custTable, false);

dirPartyTable = dirPartyTable::find(custTable.PartyId);

address = address::find(dirPartyTable.TableId, dirPartyTable.RecId, addresstype::None);

if (address.RecId != 0)
{
DirPartyAddress::changePrimaryToAltAddress(address);

}

}

Hi,

Just a follow up really to see whether the above code was of help to you. If it did, please can you mark it as solution.

Hi,

Not yet tested. I’ll let you know as soon as test your code.

Regards,

arnam

Hi,

Finally I’ve written something based on your code.

I had to consider following cases:

  • insert new primary address;

  • insert new additional address;

  • update existning primary address;

  • update existing additional address;

  • change of existing primary address to additional address;

  • change of additional primary address to existing address;

My method:

// bar0016_AccountsReceivable 20/04/2011 by WP@arnam (Piotr PB. Bartkowiak)

static container CustomerAddresIU(AccountNum _CustAccount,

RefRecID _AddrRecId, //if != 0 then update existing address

boolean _PrimaryAddress, //if true disable current primary address and update data on CustTable

AddressType _AddressType,

AddressStreet _Street,

AddressCity _City,

AddressZipCodeID _ZipCode,

AddressCountryRegionId _CountryRegionID,

WPExternalID _ExternalID

)

{

CustTable _CustTable;

Address _Address, _AddressDisable;

AddressMap _AddressMap;

boolean _NewAddress = true;

container _AddressRecIds;

RecId _AddressRecId;

DirPartyTable _DirPartyTable;

;

//--------------------------------------------------------------------------------------------------------------------

_CustTable = CustTable::find(_CustAccount, true);

if (!_CustTable)

return connull();

_Address = Address::findRecId(_AddrRecId, true);

if (_Address)

_NewAddress = false;

//--------------------------------------------------------------------------------------------------------------------

//get Customer’s DirPartyTable

if (!_CustTable.PartyId)

{

_DirPartyTable = DirParty::createPartyFromCommon(_CustTable);

_CustTable.PartyId = _DirPartyTable.PartyId;

_CustTable.update();

}

else

{

_DirPartyTable = DirPartyTable::find(_CustTable.PartyId);

}

//--------------------------------------------------------------------------------------------------------------------

if (_PrimaryAddress) //if inserting or updating address which is primary

{

//disable current primary address

_AddressDisable = WPARDataCreator::getPrimaryAddress(_DirPartyTable.PartyId);

if (_AddressDisable)

if (

(_NewAddress) || //when adding new address

((!_NewAddress) && (_AddressDisable.RecId != _Address.RecId)) //or address which is being updated is not primary

)

{

DirPartyAddress::changePrimaryToAltAddress(_AddressDisable);

}

//set new primary address data on CustTable

_CustTable.Street = _Street;

_CustTable.City = _City;

_CustTable.ZipCode = _ZipCode;

_CustTable.CountryRegionId = _CountryRegionID;

_CustTable.Address = Address::formatAddress(_CustTable.Street,

_CustTable.ZipCode,

_CustTable.City,

_CustTable.CountryRegionId,

“”,

“”);

if (_NewAddress) //create new address based on CustTable data

dirparty::updatePartyFromCommonInsert(_CustTable.PartyId, _CustTable, false);

else // or update existing address

{

DirPartyAddress::changeAltAddressToPrimary(_Address);

dirparty::updatePartyFromCommon(_CustTable.PartyId, _CustTable);

}

_CustTable.update();

if (_NewAddress) //find newly added address

_Address = address::find(_DirPartyTable.TableId, _DirPartyTable.RecId, AddressType::None, true);

else

_Address.reread(); //or reread address data updated from CustTable

if (_Address.RecId != 0) //update AddressType

{

_Address.type = _AddressType;

_Address.update();

}

}

//--------------------------------------------------------------------------------------------------------------------

else //if updating or inserting address which is not primary

{

if (_NewAddress) //init address data

{

_Address.clear();

_Address.initValue();

_Address.AddrRecId = _DirPartyTable.RecId;

_Address.AddrTableId = tableNum(DirPartyTable);

}

_Address.type = _AddressType;

_Address.Street = _Street;

_Address.City = _City;

_Address.ZipCode = _ZipCode;

_Address.CountryRegionId = _CountryRegionID;

_Address.Address = Address::formatAddress(_Address.Street,

_Address.ZipCode,

_Address.City,

_Address.CountryRegionId,

“”,

“”);

if (_NewAddress)

_Address.insert();

else

_Address.update();

}

return [_Address.RecId];

}

What do you think about it?