Thursday, July 9, 2020

Updating Customer Master and Financial Dimension through Excel Upload

static void GH_CustDimension_SalesGroup_Updating(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;

    CustTable                         _custTable;
    DimensionAttribute                _dimAttribute;
    DimensionAttributeValueSet        _dimAttributeSet;
    DimensionAttributeValueSetItem    _dimAttributeSetItem;
    DimensionAttributeValue           _dimValue;
    DimensionAttributeValueSetStorage _dimStorage;
    Common                            _dimensionValueEntry;
    CustAccount                       _custAccount;

    ;

    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();

            if(strLRTrim(cells.item(row, 1).value().bStr())!= "")
                {
                     select forUpdate _custTable where _custTable.AccountNum == _custAccount;
                    {
                         ttsBegin;
                         _custTable.SalesGroup           = 'TES';
                         _custTable.MainContactWorker    = 5637156776;
                         _dimStorage                     = DimensionAttributeValueSetStorage::find(_custTable.DefaultDimension);
                         _dimAttribute                   = DimensionAttribute::findByName("GH01_Salesman");
                         _dimValue                       = DimensionAttributeValue::findByDimensionAttributeAndValue(_dimAttribute, 'TES', true, true);
                         _dimStorage.addItem(_dimValue);
                         _custTable.DefaultDimension     = _dimStorage.save();
                         _custTable.doUpdate();
                        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");
}


excel sheet - Header name - CustomerAccount 

No comments:

Post a Comment