Thursday, 27 October 2016

X++ code to import Project contracts form excel in Ax 2012

static void DIPL_projcontract(Args _args)
{
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    //Name name;
    FileName filename;
    ProjInvoiceTable projInvoiceTable;
    ProjFundingSource projFundingSource;
    CustTable custTable;
    //DirPartyTable  dirPartyTable;

    int row = 1 ;
    str dataAreaId;
    int64 FundingSourceCustomerId;
    str FundingSourceName, ProjcontractName;
    str Description;
    str CurrencyId;
    str TaxGroupId;
    str ProjInvoiceProjId;
    ProjForecastModelType ProjForecastModelType;
    str ForecastInvoiceFrequency;
    CustAccount     cusAcc;
   // ProjInvoiceProjId projInvoiceProjId;
    NumberSeq num;
    ;

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    //specify the file path that you want to read
    filename = "D:\\projectcontract.xlsx";
    try
    {
        workbooks.open(filename);

    }
    catch (Exception::Error)
    {
    throw error("File cannot be opened.");
    }


        workbook = workbooks.item(1);
        worksheets = workbook.worksheets();
        worksheet = worksheets.itemFromNum(1);//.itemFromName("Sales target");
        cells = worksheet.cells();

    do
    {
        row++;
        ttsBegin;
           // ProjInvoiceProjId = cells.item(row, 2).value().bStr();
            ProjcontractName= cells.item(row, 3).value().bStr();
            cusAcc = cells.item(row, 4).value().bStr();
            CurrencyId = cells.item(row, 6).value().bStr();
            ForecastInvoiceFrequency =cells.item(row,7).value().bStr();
            TaxGroupId = cells.item(row, 8).value().bStr();

            projInvoiceTable.clear();
            num = NumberSeq::newGetNum(Projparameters::numRefProjInvoiceProjId());
            ProjInvoiceProjId =num.num() ;
            projInvoiceTable.ProjInvoiceProjId = ProjInvoiceProjId;
            projInvoiceTable.CurrencyId = CurrencyId;
            projInvoiceTable.TaxGroupId = TaxGroupId;
            projInvoiceTable.Description =custTable::find(cusAcc).name();
            projInvoiceTable.ForecastInvoiceFrequency=str2enum(ProjForecastModelType,ForecastInvoiceFrequency);
            projInvoiceTable.insert();
            custTable = custTable::find(cusAcc);
            projFundingSource.CustAccount = custTable.AccountNum;

            projFundingSource.initFromCustTable(custtable);
            projfundingsource.Party = custTable.Party;
            projFundingSource.FundingSourceId   = custTable.name();
            projFundingSource.ContractId    =  projInvoiceTable.ProjInvoiceProjId;
            if(projFundingSource.validateWrite())
                projFundingSource.insert();
                info(strFmt("%1 Project contract created",ProjInvoiceProjId));
        ttsCommit;
        type = cells.item(row+1, 1).value().variantType();
    }

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

}


No comments:

Post a Comment