Tuesday, October 1, 2019

Import excel file with email purpose type

static void GH_excel_CustomerEmailUpload(Args _args)
{
    SysExcelApplication                 application;
    SysExcelWorkbooks                   workbooks;
    SysExcelWorkbook                    workbook;
    SysExcelWorksheets                  worksheets;
    SysExcelWorksheet                   worksheet;
    SysExcelCells                       cells;
    COMVariantType                      type;
    Filename                            filename;
    str                                 filePath, fileNameOnly;
    Dialog                              dialog;
    DialogField                         dialogFileName;
    int                                 row;
    container                           record, roles;
    GOD_VacationBalances                vacationBalances;
    HcmPersonnelNumberId                personnelNumber;
    real                                vacationBalance;
    TransDate                           upToDate;
    DirPartyContactInfoView             contactView;
    CustTable                           CustTable;
    DirParty                            dirParty;
    DirPartyRecId                       partyRecId;
    CustAccount                         _custAccount;
    LogisticsPostalAddress              address;
    DirPartyPostalAddressView           addressView;
    ;

    dialog = new Dialog();
    dialog.caption("Pick excel file");
    dialogFileName = dialog.addField(extendedTypeStr(FilenameOpen), "Enter excel file", "Browse excel file");

    if(!dialog.run())
        return;

    filename = dialogFileName.value();
    [filePath,fileNameOnly] = fileNameSplit(filename);
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();

    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error(strFmt("Filename %1 cannot be blank.",filename));
    }

    workbook   = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet  = worksheets.itemFromNum(1);
    cells      = worksheet.cells();

    row = 1;
    do
    {
        try
        {
            row++;
            _custAccount            =   cells.item(row, 1).value().bStr();

            partyRecId              = CustTable::find(_custAccount ).Party;

            DirParty                = DirParty::constructFromPartyRecId(partyRecId );

              contactView.clear();

                if(strLRTrim(cells.item(row, 1).value().bStr())!= "")
                {
                    contactView.LocationName = "Email-SOA";
                    contactView.Locator      = strLRTrim(cells.item(row, 2).value().bStr());
                    contactView.Type         = LogisticsElectronicAddressMethodType::Email;
                    contactView.Party        = partyRecId;
                    contactView.IsPrimary    = NoYes::No;
                                   
                    DirParty = DirParty::constructFromPartyRecId(CustTable.Party);

                    roles = [LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Home).RecId];
                   
                    ttsBegin;
                   
                    dirParty.createOrUpdateContactInfo(contactView,roles);
               
                    ttsCommit;
                }

            else
            {
                warning(strFmt("Row %1 not updated.", row));
            }
        }
        catch
        {
            error(strfmt("Row %1 not updated.", row));
        }
        type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);
    workbook.saved(true);
    application.visible(false);
    application.quit();

    info("Excel uploaded successfully");
}