Importing Contacts

Below is an example of how to import Contacts with AX++ code for customers.

DirPerson DirPerson;
DirPersonName DirPersonName;
LogisticsLocation logisticsLocation;
LogisticsElectronicAddress logisticsElectronicAddress;
ContactPerson ContactPerson;
;

try
{
ttsbegin;

DirPerson.clear();
DirPerson.initValue();
DirPerson.PersonalTitle = PersonTitleTable::find().RecId;

DirPersonName.clear();
DirPersonName.FirstName = <First Name>;
DirPersonName.MiddleName = <Middle Name>;
DirPersonName.LastName = <Last Name>;

DirPerson.ProfessionalTitle = <Professional Title>;

DirPerson.Name = <Contact Name>;
if (DirPerson.validateWrite())
{
DirPerson.insert();
if (DirPerson)
{
DirPersonName.Person = DirPerson.RecId;
DirPersonName.ValidFrom = DateTimeUtil::newDateTime(systemDateGet(),str2time (’00:00:00′),DateTimeUtil::getUserPreferredTimeZone());
DirPersonName.ValidTo = DateTimeUtil::maxValue();
DirPersonName.insert();

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

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

//Telephone
logisticsElectronicAddress.clear();
logisticsElectronicAddress.initValue();
logisticsElectronicAddress.Location = logisticsLocation.RecId;
logisticsElectronicAddress.Type = LogisticsElectronicAddressMethodType::Phone;
logisticsElectronicAddress.Locator = <Telephone Number>;
logisticsElectronicAddress.LocatorExtension = <Extension>;
logisticsElectronicAddress.IsPrimary = NoYes::Yes;

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

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

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

//Contact
ContactPerson.clear();
ContactPerson.initvalue();
ContactPerson.ContactPersonId =             NumberSeq::newGetNum(CompanyInfo::numRefContactPersonId()).num();
if(CustTable::exist())
ContactPerson.ContactForParty = CustTable::find(CustomerAccount).Party;
ContactPerson.ContactForParty = PartyRecId;
ContactPerson.Department = <Department>;
ContactPerson.DocuIntro1 = <Salutation>;

if(ContactPerson.validateWrite())
ContactPerson.insert();
else
throw error(“Contact person failed to create”);
}
}
else
throw error(“Person 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