Friday, May 26, 2017

Lookup through X++ Code

Lookup through code based on selected control

public void lookup()
{
    Query query = new Query();
    QueryBuildDataSource qbds;
 

    // Instantiate sysTableLookup object using table which will provide the visible fields
    SysTableLookup sysTableLookup = sysTableLookup::newParameters(tableNum(BrandTable), this);
    ;

    // Create the query.
    qbds= query.addDataSource(tableNum(BrandTable));
   //Add range
  //Product Control set auto declaration YES
    qbds.addRange(fieldNum(BrandTable,Product)).value(Product.valueStr());

    // Set the query to be used by the lookup form
    sysTableLookup.parmQuery(query);

    // Specify the fields to show in the form.
    sysTableLookup.addLookupfield(fieldNum(BrandTable, Product));
    sysTableLookup.addLookupfield(fieldNum(BrandTable, Brand),true);
    sysTableLookup.addLookupfield(fieldNum(BrandTable, Description));

    // Perform the lookup
    sysTableLookup.performFormLookup();
}

Import data from Excel to AX through X++

static void ImportExcelData(Args _args)
{
    SysExcelApplication             application;
    SysExcelWorkbooks               workbooks;
    SysExcelWorkbook                workbook;
    SysExcelWorksheets              worksheets;
    SysExcelWorksheet               worksheet;
    SysExcelCells                   cells;
    COMVariantType                  type;
    System.DateTime                 ShlefDate;
    FilenameOpen                    filename;

    dialogField                     dialogFilename;
    Dialog                          dialog;
    #AviFiles
    // Progress Bar
    SysOperationProgress progress = new SysOperationProgress(1, NoYes::Yes); 

    StyleTable                      styleTable;  // Table name
    str                             style;
    int                             row = 0;
    #Excel

    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 "";
    }
    ;

    dialog = new Dialog("ExcelUpload");
    dialogFilename      =   dialog.addFieldValue(extendedTypeStr(FilenameOpen),filename);
    dialog.filenameLookupFilter(["@SYS28576",#XLS,"@SYS28576",#XLSX]);
    dialog.filenameLookupTitle("Upload from Excel");
    dialog.caption("Excel Upload");
    dialogFilename.value(filename);

    if(!dialog.run())
        return;

    filename            =   dialogFilename.value();
    application         =   SysExcelApplication::construct();
    workbooks           =   application.workbooks();

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

    // Progress Caption & Animation
    progress.setCaption("Copying..");
    progress.setAnimation(#AviUpdate);

    do
    {
        try
        {
            ttsbegin;
            row++;
            // Getting EquipmentId value form Excel Row wise and assigning as str
            style = COMVariant2Str(cells.item(row,1).value());         

        // While Revecing datas from Excel Sheet If invalid value presents it wil not allowe to insert and the
            //   last  record also deleted
            if (!equipId)
            {
                ttsbegin;

                _TestXls.delete();

                ttscommit;

                box::warning(strfmt("Check the value in Excel Sheet row %1", row));

                return;

            }

            else
            {
                if (row > 1)
                {
                    // Progress bar Text and Total
                    progress.setText(strfmt("Importing to Ax Table : %1", row));
                    progress.setTotal(row, 1);

                    _TestXls.clear();
                    _TestXls.Style    =  equipId;
                    _TestXls.insert();

                }
            }

            ttscommit;
        }

        catch
        {
            Error(strfmt("Upload Failed in row %1", row));
        }

        type = cells.item(row + 1, 1).value().variantType();

    } while (type!= COMVariantType::VT_EMPTY);

    info(strfmt(" Details Uploaded Successfully"));

    application.quit(); 

}

Enable/Disable form control based on multiple rows select

 class PurchTableFormEventHandler {      [FormDataSourceEventHandler(formDataSourceStr(PurchTable, PurchTable), FormDataSourceEventType::Act...