Importing Customers

Below is an example of how to import Customers (Organisations) with AX++ code.

CustTable CustTable;

DirOrganization dirOrganization;
LogisticsLocation logisticsLocation;
LogisticsPostalAddress logisticsPostalAddress;
LogisticsElectronicAddress logisticsElectronicAddress;

str sDummy;
;

try
{
ttsbegin;

setprefix(strFmt(“New Customer %1”, ));

//Organization
dirOrganization.clear();
dirOrganization.initValue();
dirOrganization.Name = <customer name> ;
dirOrganization.OrgNumber = <organisation number>;

if (dirOrganization.validateWrite())
{
dirOrganization.insert();

if (dirOrganization)
{
//Location
logsticsLocation = LogisticsLocation::create(‘Primary’, NoYes::Yes);

//Party location
DirParty::addLocation(dirOrganization.RecId, logisticsLocation.RecId, true, true, false, [LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Business).RecId]);

//Postal address
logisticsPostalAddress.clear();
logisticsPostalAddress.initValue();
logisticsPostalAddress.Location = logisticsLocation.RecId;
LogisticsPostalAddress.Street = strLRTrim(<address line 1>);
sDummy =strLRTrim(

);

if(strlen(sDummy) > 0){
LogisticsPostalAddress.Street += “\n” + sDummy;
}
sDummy =strLRTrim(

);

if(strlen(sDummy) > 0){
LogisticsPostalAddress.Street += “\n” + sDummy;
}

logisticsPostalAddress.City = <city>;
logisticsPostalAddress.County = <county>;
logisticsPostalAddress.ZipCode = <zip code>;
logisticsPostalAddress.CountryRegionId = <country>;
logisticsPostalAddress.Address = LogisticsPostalAddress::formatAddress(
logisticsPostalAddress.Street,
logisticsPostalAddress.ZipCode,
logisticsPostalAddress.City,
logisticsPostalAddress.CountryRegionId,
logisticsPostalAddress.State,
logisticsPostalAddress.County);

if((LogisticsPostalAddress.Street || logisticsPostalAddress.City || logisticsPostalAddress.ZipCode) && !logisticsPostalAddress.CountryRegionId)
{
throw error(“Country not been specified for address”);
}
else
{
if (logisticsPostalAddress.validateWrite())
{
logisticsPostalAddress.insert();
}
else
throw error(“Postal address error”);
}

//Location
logisticsLocation = LogisticsLocation::create(‘Electronic address’, NoYes::No);

//Party location
DirParty::addLocation(dirOrganization.RecId, logisticsLocation.RecId, false, true, false);

//phone address
logisticsElectronicAddress.clear();
logisticsElectronicAddress.initValue();
logisticsElectronicAddress.Location = logisticsLocation.RecId;
logisticsElectronicAddress.Type = LogisticsElectronicAddressMethodType::Phone;
logisticsElectronicAddress.Locator = strLRTrim(<telephone number>);
logisticsElectronicAddress.IsPrimary = NoYes::Yes;

if(strLen(logisticsElectronicAddress.Locator) > 0)
{
if (logisticsElectronicAddress.validateWrite())
{
logisticsElectronicAddress.insert();
}
else
throw error(“Phone number error”);
}

//email address
logisticsElectronicAddress.clear();
logisticsElectronicAddress.initValue();
logisticsElectronicAddress.Location = logisticsLocation.RecId;
logisticsElectronicAddress.Type = LogisticsElectronicAddressMethodType::Email;
logisticsElectronicAddress.Locator = strLRTrim(<email address>);
logisticsElectronicAddress.IsPrimary = NoYes::Yes;

if(strLen(logisticsElectronicAddress.Locator) > 0)
{
if (logisticsElectronicAddress.validateWrite())
{
logisticsElectronicAddress.insert();
}
else
throw error(“Email address error”);
}

//Customer
custTable.clear();
custTable.initValue();
CustTable.AccountNum = <account number>;
custTable.Party = dirOrganization.RecId;
CustTable.CustGroup = ;
CustTable.initFromCustGroup(CustGroup::find(CustTable.CustGroup));
CustTable.Currency = <currency>;
CustTable.CreditMax = <credit limit>;
CustTable.VATNum = <VAT number>;
CustTable.Blocked = <blocked>;
CustTable.SegmentId = <segment>;
CustTable.SalesGroup = <sales group>;
CustTable.DlvMode = <delivery mode>;
CustTable.TaxGroup = <tax group>;
CustTable.MandatoryCreditLimit = NoYes::Yes;

custTable.DefaultDimension = AxdDimensionUtil::getDimensionAttributeValueSetId(
[3, “<dimension 1>”, “<dimension 1 value>”, “<dimension 2>”, “<dimension 2 value>”, “<dimension 3>”, “<dimension 3 vlaue>”]);

if (custTable.validateWrite())
{
custTable.insert();
}
else
throw error(“Customer record error”);
}
}
else
throw error(“Organization error”);

ttscommit;
}
catch
{
error(“Aborted”);
ttsAbort;
}

As with all data import scripts, adjustments need to be made to meet your data import requirements, and should be tested thoroughly before used in a production environment.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s