Tuesday, June 5, 2012

Create Vendor Postal Address using x++ code

In Microsoft Dynamics AX. I came accross a requirement in which I had to create LogisticsPostalAddress records (i-e postal addressed for party (vendors/customers)) using x++ code. I tried to use excel add-in but it didn't work for me in case I have multiple postal addresses for a single vendor.

Following is the code that reads a csv file and then creates postal addresses for a vendor.

static void PostalAddressCreate(Args _args)

{

VendTable vendTable;

DirParty dirParty;

DirPartyPostalAddressView PostalAddress;

CommaTextIo file;

container record;

str countyId, zipcode;

;

file =
new CommaTextIo("C:\\VendorPostalAddress.csv",'r');

file.inFieldDelimiter(',');

while (file.status() == IO_Status::Ok)

{

record = file.read();

vendTable = VendTable::find(
conPeek(record,1));

if (vendTable.RecId)

{

dirParty = DirParty::constructFromCommon(vendTable);

PostalAddress.Street =
conPeek(record,2);

PostalAddress.BuildingCompliment = conPeek(record,3);

PostalAddress.City = conPeek(record,4);

PostalAddress.CountryCurrencyCode = conPeek(record,5);

PostalAddress.CountryRegionId = conPeek(record,6);

countyId = conPeek(record,7);

if (Global::strStartsWith(countyId,'~'))

{

countyId =
strDel(countyId,1,1);

}

PostalAddress.County = countyId;

PostalAddress.IsPrimary =
conPeek(record,12);

PostalAddress.LocationName = conPeek(record,16);

PostalAddress.State = conPeek(record,24);

zipcode = conPeek(record,30);



if (Global::strStartsWith(zipcode,'~'))

{

zipcode =
strDel(zipcode,1,1);

}

PostalAddress.ZipCode = zipcode;

PostalAddress.ValidFrom = datetobeginUtcDateTime(
1\1\2012, DateTimeUtil::getUserPreferredTimeZone()) ;

PostalAddress.ValidTo = datetobeginUtcDateTime(1\1\2154, DateTimeUtil::getUserPreferredTimeZone()) ;

PostalAddress.Party = vendTable.Party;

 

dirParty.createOrUpdatePostalAddress(PostalAddress);

}

}

}