Saturday, December 17, 2016

AX 2012 X++ code to import from EXCEL files COMVARIANT Vikas Mehta

static void readExcelFile(Args _args)
{
    SysExcelApplication         application;
    SysExcelWorkbooks           workbooks;
    SysExcelWorkbook            workbook;
    SysExcelWorksheets          worksheets;
    SysExcelWorksheet           worksheet;
    SysExcelCells               cells;
    Filename                    filename;
    COMVariantType              type;
   
int                         rowNo;
    SampleTable       SampleTable;
    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 "";
    }
rowNo   = 2;//If the excel file having header.
application = SysExcelApplication::construct();
workbooks = application.workbooks();
filename = @"C:\temp";//Excel file path.
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();
type = cells.item(rowNo, 1).value().variantType();
while (type != COMVariantType::VT_EMPTY)
    {
        type = cells.item(rowNo+1, 1).
value().variantType();
SampleTable.clear();
SampleTable.MainAccountId = COMVariant2Str(cells.item(rowNo, 1).value());

SampleTable.insert();
rowNo++;//To get next column number in the loop.
    }
info('fim');
application.quit();
}



Thursday, November 10, 2016

DIALOG AND GET VALUE FROM DIALOG BOX USING AXAPTA X++ DYNAMICS AX

DIALOG AND GET VALUE FROM DIALOG BOX USING AXAPTA
Below is a Code how  to add dialog and get value from dialog box in dynamics Axapta. This is very simple you just need to create one method which is overridden method is dialog. Dialog method used to initialized dialog box for your form or report but without using get from dialog method you can not get value of dialog control.

So both method is important dialog and getfromdialog.

 public Object dialog(Object _dialog)
 {
 DialogRunbase dialog = _dialog;
 DialogGroup dateGroup;
 ;
 dateGroup = dialog.addGroup("");
 dateGroup.frameType(1);
 dateGroup.columns(2);
 fromdate = LedgerPeriod::findOpeningDate(systemdateget());
 toDate= systemdateget();
 dialog.addGroup("@SYS4083",dateGroup);
 dialogFromDate = dialog.addFieldValue(typeid(FromDate),fromDate,'',"@SYS67");
 dialog.addGroup("@SYS8828",dateGroup) ;
 dialogToDate = dialog.addFieldValue(typeid(ToDate),toDate,'',"@SYS67");
 dialog.addGroup("@SYS24500");
 dialogPageChange = dialog.addFieldValue(typeid(NoYes),pageChange,"@SYS15349","@SYS24755");
 dialogResetPage = dialog.addFieldValue(typeid(NoYes),resetPage,"@SYS9358","@SYS24756");
 dialog.addGroup("@SYS12608");
 dialogDetailSummary = dialog.addFieldValue(typeid(DetailSummary),detailSummary,'',"@SYS24757");
 dialogNoneBeginTransEnd = dialog.addFieldValue(typeid(NoneBeginTransEnd),noneBeginTransEnd,
 "@SYS1046","@SYS69638");
 dialogIncludeOpening = dialog.addFieldValue(typeid(NoYes),includeOpening,"@SYS23250","@SYS54008");
 dialogIncludeClosing = dialog.addFieldValue(typeid(NoYes),includeClosing,"@SYS14844","@SYS82258");
 dialog.addGroup("Opening Balance Account");
 dialogOpeningBalance = dialog.addField(typeId(LedgerAccount)
 ,"Opening Balance Account");
 return dialog;
 }
 public boolean getFromDialog()
 {
 ;
 fromDate = dialogFromDate.value();
 toDate = dialogToDate.value();
 detailSummary = dialogDetailSummary.value();
 pageChange = dialogPageChange.value();
 resetPage = dialogResetPage.value();
 noneBeginTransEnd = dialogNoneBeginTransEnd.value();
 includeOpening = dialogIncludeOpening.value();
 includeClosing = dialogIncludeClosing.value();
 OpLedgerAccount = dialogOpeningBalance.value();
 return true;
 }  

DYNAMICS AX CREATE CUSTOM NUMBER SEQUENCE X++ CODE SAMPLE

|| DYNAMICS AX NUMBER SEQUENCE ||

Suppose we want create number sequence for Test field on form in General  ledger module
Consideration: EDT-Test, Table-NumSeqTable and Form- NumSeqTable

Step1. Create new EDT with name Test
Step 2. Modify load module() method on  NumberSeqModuleLedger class

{
     datatype.parmDatatypeId(extendedTypeNum(Test));
     datatype.parmReferenceHelp(literalStr("Test"));
     datatype.parmWizardIsManual(NoYes::No);
     datatype.parmWizardIsChangeDownAllowed(NoYes::No);
     datatype.parmWizardIsChangeUpAllowed(NoYes::No);
     datatype.parmWizardHighest(999);
     datatype.parmSortField(30);
     datatype.addParameterType(NumberSeqParameterType::DataArea, truefalse);
     this.create(datatype);
}

Step 3.Create a method on LedgerParameters Table

     client server static NumberSequenceReference numRefTest()
{
     return NumberSeqReference::findReference(extendedTypeNum(Test));
}

Step 4.Write and run following job

static void NumberSeqLoadAll(Args _args)
{
         NumberSeqApplicationModule::loadAll();
}


Step 5.Then run the wizard

Organization Administration -> CommonForms -> Numbersequences->Numbersequences-> Generate -> run the wizard.

Step 6.Now we have to check the number sequence  is correctly working  for that write a job:

static void NumSeq(Args _args)
{
    NumberSeq  numberSeq;
    Test num;
    ;
    numberSeq = NumberSeq::newGetNum(ProjParameters:: numRefTest ());
    num = numberSeq.num();
    info(num);
}


Step 7.Now we want that Number Sequence in form level(Test Table):


 Write below code in class declaration  
public class FormRun extends ObjectRun
{
    NumberSeqFormHandler numberSeqFormHandler;

}

 Step 8.Write the NumberSeqFormHandler() in form methods node.

NumberSeqFormHandler numberSeqFormHandler()
{
    if (!numberSeqFormHandler)
    {
       numberSeqFormHandler = NumberSeqFormHandler::newForm(LedgerParameters:: numRefTest ().NumberSequenceId,
                                                             element,
                                                             NumSeqTable_DS,
                                                             fieldNum(NumSeqTable, Test)
                                                            );
    }
    return numberSeqFormHandler;
}


Step 9.Write the  Create(),Delete(),Write() , Validate Write(),Link Active() on the Data source methods node.
 Create() Method
void create(boolean append = false,
            boolean extern = false
{
    element.numberSeqFormHandler().formMethodDataSourceCreatePre();

    super(append);

    if (!extern)
    {
        element.numberSeqFormHandler().formMethodDataSourceCreate(true);
    }
}

Delete() Method

public void delete()
{
    element.numberSeqFormHandler().formMethodDataSourceDelete();
    super();
}

Write()Method

public void write()
{
    super();
    element.numberSeqFormHandler().formMethodDataSourceWrite();
}



Validate Write() Method

public boolean validateWrite()
{
    boolean         ret;
    ret = super();
    ret = element.numberSeqFormHandler().formMethodDataSourceValidateWrite(ret) && ret;
    if (ret)
    {
        NumSeqTable.validateWrite();
    }
    return ret;
}

Link Active() Method

public void linkActive()
{
    ;
    element.numberSeqFormHandler().formMethodDataSourceLinkActive();
    super();
}
Step 10.Finally add Close() method on form 
void close()
{
    if (numberSeqFormHandler)
    {
        numberSeqFormHandler.formMethodClose();
    }
    super();
}


Related Posts Plugin for WordPress, Blogger...