Thursday 27 April 2017

Import Vendor Postal address and Contact Info from Excel using X++

static void Na_ContactDetails(Args _args)
{
    Dialog                              dialog;
    Dialogfield                         dialogfield;
    VendTable                           vendTable;

    LogisticsElectronicAddress          logisticsElectronicAddress;
    LogisticsPostalAddress              logisticsPostalAddress;
    LogisticsLocation                   logisticsLocation,logisticsLocation1;
    LogisticsLocationRole               logisticsLocationRole;
    DirPartyTable                       dirPartyTable,dirPartyTable1;
    COMVariantType                      type;

    DirPartyLocation                    dirPartyLocation,dirPartyLocation1;
    DirPartyLocationRole                dirPartyLocationRole,dirPartyLocationRole1;
    LogisticsElectronicAddressRole      logisticsElectronicAddressRole;

    SysExcelApplication                 application;
    SysExcelWorkbooks                   workBooks;
    SysExcelWorkbook                    workBook;
    SysExcelWorksheets                  workSheets;
    SysExcelWorksheet                   workSheet;
    SysExcelCells                       cells;
    Filename                            fileName;
    int                                 row;

    AccountNum                          vendAccount;
    Name                                vendName;
    VendGroupId                         vendGroup;
    Description                         description;
    str 100                             Address,phonenum,namealiyas;
    CurrencyCode                        currency;


    str COMVariant2Str(COMVariant _cv, int _decimals = 0,int _characters = 0,int _separator1 = 0,int _separator2 = 0)
    {
        switch(_cv.variantType())
        {
            case (COMVariantType::VT_BSTR):
                return _cv.bStr();
            case (COMVariantType::VT_R4):
                return num2str(_cv.float(),_characters,_decimals, _separator1,_separator2);
            case (COMVariantType::VT_R8):
                return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
            case (COMVariantType::VT_DECIMAL):
                return num2str(_cv.decimal(),_characters,_decimals, _separator1, _separator2);
            case (COMVariantType::VT_DATE):
                return date2str(_cv.date(),123,2,1,2, 1,4);
            case (COMVariantType::VT_EMPTY):
                return "";
            default:
                throw error(strfmt("@SYS26908",_cv.variantType()));
        }
        return "";
    }

    application =   SysExcelApplication::construct();
    workBooks   =   application.workbooks();
    dialog      = new Dialog("FileOpen");
    dialogfield = dialog.addField(extendedTypeStr(Filenameopen), "File Name");
    dialog.run();

    if (dialog.run())
    {
        filename = (dialogfield.value());
    }
    try
    {
        workBooks.open(fileName);
    }
    catch (Exception::Error)
    {
        throw error("File Cannot be opened");
    }

    workBook    =   workBooks.item(1);
    workSheets  =   workBook.worksheets();
    workSheet   =   workSheets.itemFromNum(1);
    cells      =   workSheet.cells();
    row =1;
    do
    {
        row++;


        vendAccount = cells.item(row,1).value().bStr();
        vendName    = cells.item(row,2).value().bStr();
        vendGroup   = COMVariant2Str(cells.item(row,3).value());
        phonenum    = COMVariant2Str(cells.item(row,4).value());
        description = COMVariant2Str(cells.item(row,5).value());
        Address     = cells.item(row,6).value().bStr();
        currency    = cells.item(row,7).value().bStr();
        namealiyas  = "working1";

        logisticsLocation.clear();
        logisticsLocation.initValue();
        logisticsLocation.Description               = description;
        logisticsLocation.insert();

        logisticsLocation1.clear();
        logisticsLocation1.initValue();
        logisticsLocation1.Description                      = namealiyas;
        logisticsLocation1.insert();

        logisticsPostalAddress.clear();
        logisticsPostalAddress.initValue();
        logisticsPostalAddress.Address                      = Address;
        logisticsPostalAddress.Location                     = LogisticsLocation.RecId;
        logisticsPostalAddress.CountryRegionId              = "IND";
        logisticsPostalAddress.State                        = "Telangana";
        logisticsPostalAddress.Street                       = "Kukatpally";
        logisticsPostalAddress.insert();

        dirPartyTable.clear();
        dirPartyTable.initValue();
        dirPartyTable.Name                                  = vendName;
        dirPartyTable.PrimaryContactPhone                   = logisticsElectronicAddress.recId;
        dirPartyTable.PrimaryAddressLocation                = logisticsLocation.RecId;
        dirPartyTable.insert();

        logisticsElectronicAddress.clear();
        logisticsElectronicAddress.initValue();
        logisticsElectronicAddress.Type                     = LogisticsElectronicAddressMethodType::Phone;
        logisticsElectronicAddress.Locator                  = phonenum;
        logisticsElectronicAddress.Description              = description;
        logisticsElectronicAddress.IsPrimary                = NoYes::No;
        logisticsElectronicAddress.Location                 = logisticsLocation1.RecId;
        logisticsElectronicAddress.PrivateForParty          = dirPartyTable.RecId;
        logisticsElectronicAddress.IsMobilePhone            = NoYes::No;
        logisticsElectronicAddress.LocatorExtension         = "+91";
        logisticsElectronicAddress.insert();

        logisticsElectronicAddressRole.clear();
        logisticsElectronicAddressRole.initValue();
        logisticsElectronicAddressRole.ElectronicAddress    = logisticsElectronicAddress.RecId;
        logisticsElectronicAddressRole.LocationRole         = LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Home).RecId;
        logisticsElectronicAddressRole.insert();

        vendTable.clear();
        vendTable.initValue();
        vendTable.AccountNum                                =  vendAccount;
        vendTable.Currency                                  =  currency;
        vendTable.VendGroup                                 =  vendGroup;
        vendTable.Party                                     =  dirPartyTable.RecId;
        vendTable.insert();

        dirPartyLocation.clear();
        dirPartyLocation.initValue();
        dirPartyLocation.Party                              =  dirPartyTable.RecId;
        dirPartyLocation.Location                           =  logisticsLocation.RecId;
        dirPartyLocation.IsPostalAddress                    =  NoYes::Yes;
        dirPartyLocation.IsPrimary                          =  NoYes::Yes;
        dirPartyLocation.IsRoleDelivery                     =  NoYes::No;
        dirPartyLocation.insert();

        dirPartyLocation1.clear();
        dirPartyLocation1.initValue();
        dirPartyLocation1.Party                             =  dirPartyTable.RecId;
        dirPartyLocation1.Location                          =  logisticsLocation1.RecId;
        dirPartyLocation1.IsPostalAddress                   =  NoYes::No;
        dirPartyLocation1.IsPrimary                         =  NoYes::Yes;
        dirPartyLocation1.IsRoleDelivery                    =  NoYes::No;
        dirPartyLocation1.insert();

        dirPartyLocationRole.clear();
        dirPartyLocationRole.initValue();
        dirPartyLocationRole.LocationRole                   =  LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Home).RecId;
        dirPartyLocationRole.PartyLocation                  =  dirPartyLocation.RecId;
        dirPartyLocationRole.insert();

        dirPartyLocationRole1.clear();
        dirPartyLocationRole1.initValue();
        dirPartyLocationRole1.LocationRole                  =  LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Home).RecId;
        dirPartyLocationRole1.PartyLocation                 =  dirPartyLocation1.RecId;
        dirPartyLocationRole1.insert();

    }
    while (type != COMVariantType::VT_EMPTY);
    application.quit();
    workbooks.close();

    info("Done");

}

No comments:

Post a Comment