Import Customer through X++


static void UploadCustomer(Args _args)
{
 CommaTextIO csvFile;
 container readCon;
 counter icount,inserted;
 Dialog dialog;
 DialogField dfFileName;


 FileName fileName;
 Name name;

 DirPartyContactInfoView contactView;
 CustTable CustTable;
 DirParty dirParty;
 DirPartyRecId partyRecId;

 LogisticsPostalAddress address;
 DirPartyPostalAddressView addressView;

 inserted =0;

 #File


 dialog = new Dialog("Pick the file");
 dfFileName = dialog.addField(extendedTypeStr("FilenameOpen"));
 dialog.filenameLookupFilter(["All files", #AllFiles]);


 if (dialog.run())
 {
 csvFile = new CommaTextIo(dfFileName.value(), 'r');
 csvFile.inFieldDelimiter(',');
 readCon = csvFile.read();
 ttsBegin;
 while(csvFile.status() == IO_Status::OK)
 {
 readCon = csvFile.read();

 if(readCon)
 {
 icount++;

 name = conPeek(readCon,2);
 partyRecId = DirPartyTable::createNew( DirPartyType::Organization, name).RecId;

 custTable.clear();
 custTable.initValue();
 custTable.Party = partyRecId;
 custTable.AccountNum = conPeek(readCon,1);
 custTable.CustGroup = conPeek(readCon,3);
 custTable.Currency = conPeek(readCon,7);
 custTable.DlvMode = conPeek(readCon,8);
 custTable.PaymTermId = conPeek(readCon,9);
 custTable.insert();

 custTable = Custtable::find(conPeek(readCon,1));
 partyRecId = custTable.Party;

 DirParty = DirParty::constructFromPartyRecId(partyRecId );
 address.clear();
 //address.PostBox = strLRTrim(conPeek(readCon,13));
 address.CountryRegionId = strLRTrim(conPeek(readCon,5));
 if( address.CountryRegionId != "")
 {
 address.State = strLRTrim(conPeek(readCon,15));
 address.ZipCode = strLRTrim(conPeek(readCon,15));
 address.Street = strLRTrim(conPeek(readCon,4));
 address.City = strLRTrim(conPeek(readCon,18));
 
 //address.Address = conPeek(readCon,4);
 
 addressView.LocationName = "Address";
 addressView.IsPrimary = NoYes::Yes;
 addressView.Party = partyRecId;
 addressview.initFromPostalAddress(address);


 DirParty = DirParty::constructFromPartyRecId(addressView.Party );
 DirParty.createOrUpdatePostalAddress(addressView);
 }

 contactView.clear();

 if(conPeek(readCon,5) != "")
 {
 contactView.LocationName = "Phone Number";
 contactView.Locator = strLRTrim(conPeek(readCon,5));
 contactView.Type = LogisticsElectronicAddressMethodType::Phone;
 contactView.Party = partyRecId;
 contactView.IsPrimary = NoYes::Yes;
 dirParty.createOrUpdateContactInfo(contactView);
 }

 if(conPeek(readCon,3) != "")
 {
 contactView.LocationName = "Fax Number";
 contactView.Locator = strLRTrim(conPeek(readCon,3));
 contactView.Type = LogisticsElectronicAddressMethodType::Fax;
 contactView.Party = partyRecId;
 contactView.IsPrimary = NoYes::Yes;
 dirParty.createOrUpdateContactInfo(contactView);
 }

 if(conPeek(readCon,4) != "")
 {
 contactView.LocationName = "Website";
 contactView.Locator = strLRTrim(conPeek(readCon,4));
 contactView.Type = LogisticsElectronicAddressMethodType::URL;
 contactView.Party = partyRecId;
 contactView.IsPrimary = NoYes::Yes;
 dirParty.createOrUpdateContactInfo(contactView);
 }

 if(conPeek(readCon,6) != "")
 {
 contactView.LocationName = "Email";
 contactView.Locator = strLRTrim(conPeek(readCon,6));
 contactView.Type = LogisticsElectronicAddressMethodType::Email;
 contactView.Party = partyRecId;
 contactView.IsPrimary = NoYes::Yes;
 dirParty.createOrUpdateContactInfo(contactView);
 }

 }

 }
 ttsCommit;
 }

}
Advertisements

One thought on “Import Customer through X++

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