Tuesday, October 1, 2013

Vendor Master import from CSV file using X++ programming

public class VendorMasterImport extends RunBase
{
    CommaIo                     csvFile;
    Filename                    filename;
    DialogField                 dialogFilename;
    #define.CurrentVersion(2)
    #localmacro.CurrentList
    filename,
    insertIncorrectRecords,
    #endmacro
    #localmacro.ListVersion1
    filename,
    insertIncorrectRecords
    #endmacro
    #File
}
public Object dialog()
{
    DialogRunbase       dialog = super();
    ;
    dialogFilename  = dialog.addField(extendedTypeStr(FilenameOpen));
    dialogFilename.value(filename);
    dialog.filenameLookupFilter(["All files", #AllFiles]);
    return dialog;
}

public boolean getFromDialog()
{
    filename                = dialogFilename.value();
    return true;
}
void run()
{
    //CommaTextIO                             csvFile;
    container                               readCon;
    counter                                 icount,inserted;
    Dialog                                  dialog;
    DialogField                             dfFileName;
    DirPartyRecId                           partyRecId,contactPartyRecid;
    Name                                    name,contactName;
    VendTable                               vendtable;
    str                                     contactperson;
    DirPartyPostalAddressView               addressView;
    DirPartyContactInfoView                 contactView;
    ContactPerson                           contactpersonTable;
    LogisticsElectronicAddressMethodType    enumType;
    DirContactPersonsService                dirContactPersonsService;
    DirContactPersons                       dirContactPersons;
    DirContactPersons_ContactPerson         dirContactPersons_ContactPerson;
    DirContactPersons_Person                dirContactPersons_Person;
    DirContactPersons_PersonName            dirContactPersons_PersonName;
    AifEntityKeyList                        aifEntityKeyList, aifEntityKeyList_DirContactPerson;
    str                                     fName, mName, lName;
    VendAccount                             vendorAccount;
    DirParty                                dirParty;
    LogisticsPostalAddress                  address;
    LogisticsElectronicAddress              logisticsElectronicAddress;
    BinData                                 binData;
    str                                     stringImage;
    LogisticsAddressStateID                 stateId;
    str                                     accountnum,accountName,vendgroup,currency,dlvmode,paymtermid,countryid,street,city,mobile,fax,email,zipcode,pobox,phone;

    ;

    csvFile = new CommaIO(filename, 'r');
    try
    {
        if (csvFile)
        {
            ttsbegin;
            readCon = csvFile.read();
            while (csvFile.status() == IO_Status::OK)
            {
                readCon = csvFile.read();
                if(readCon)
                {
                icount++;
                accountnum = conPeek(readCon,1);
                accountName = conPeek(readCon,2);
                phone = conPeek(readCon,3);
                fax = conPeek(readCon,4);
                vendgroup = conPeek(readCon,5);
                currency = conPeek(readCon,6);
                dlvmode = conPeek(readCon,7);
                paymtermid = conPeek(readCon,8);
                email = conPeek(readCon,9);
                mobile = conPeek(readCon,10);
                contactperson = conPeek(readCon,11);
                pobox = conPeek(readCon,12);
                countryid = conPeek(readCon,13);
                zipcode = conPeek(readCon,14);
                stateId = conPeek(readCon,15);
                city = conPeek(readCon,16);
                street = conPeek(readCon,17);


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

                vendtable.clear();
                vendtable.initValue();
                vendtable.Party = partyRecId;
                vendtable.AccountNum = accountnum;
                vendtable.VendGroup  = vendgroup;
                vendtable.Currency   = currency;
                //vendtable.Blocked    =
                vendtable.DlvMode    = dlvmode;
                vendtable.PaymTermId   = paymtermid;
                if(contactperson != '')
                {
                    contactname = ContactPerson;
                    ContactPerson::findOrCreateNameParty(partyRecId,contactname);
                }
                vendtable.insert();
                ttsCommit;

                stateId = subStr(stateId,1,25);

                address.PostBox = strLRTrim(PoBox);
                address.CountryRegionId = strLRTrim(Countryid);
                address.State = stateId;
                address.ZipCode = strLRTrim(ZipCode);
                address.Street  = strLRTrim(Street);
                //address.county = strLRTrim(conPeek(readCon,17));
                address.City    = strLRTrim(City);
                addressView.LocationName = name;
                addressView.IsPrimary = NoYes::Yes;
                addressView.Party = partyRecId;
                addressview.initFromPostalAddress(address);

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

                contactView.LocationName = "Email";
                contactView.Locator      = strLRTrim(email);
                contactView.Type         = LogisticsElectronicAddressMethodType::Email;
                contactView.Party        = partyRecId;
                contactView.IsPrimary    = NoYes::Yes;
                dirParty.createOrUpdateContactInfo(contactView);

                contactView.LocationName = "Mobile";
                contactView.Locator      = strLRTrim(mobile);
                contactView.Type         = LogisticsElectronicAddressMethodType::Phone;
                contactView.Party        = partyRecId;
                contactView.IsPrimary    = NoYes::Yes;
                dirParty.createOrUpdateContactInfo(contactView);

                contactView.LocationName = "Phone";
                contactView.Locator      = strLRTrim(Phone);
                contactView.Type         = LogisticsElectronicAddressMethodType::Phone;
                contactView.Party        = partyRecId;
                contactView.IsPrimary    = NoYes::Yes;
                dirParty.createOrUpdateContactInfo(contactView);

                contactView.LocationName = "Fax";
                contactView.Locator      = strLRTrim(Fax);
                contactView.Type         = LogisticsElectronicAddressMethodType::Fax;
                contactView.Party        = partyRecId;
                contactView.IsPrimary    = NoYes::Yes;
                dirParty.createOrUpdateContactInfo(contactView);
                inserted++;
                }
            }
            ttsCommit;
        }
        icount--;//Remove header recount from total record count
    }
    catch(Exception::Error)
    {
        info(strFmt("%1 %2",Exception::Error,icount));
    }
}

static void main(Args  args)
{
    VendorMasterImport        VendorMasterImport;
    ;
    VendorMasterImport =  new VendorMasterImport();
    if(VendorMasterImport.prompt())
    {
        VendorMasterImport.run();
    }
}

No comments:

Post a Comment