Friday, September 4, 2020

HOW TO GET ERROR MESSAGE IN TRY CATCH

 System.Exception exception;

try
{

}
catch(Exception::CLRError)
{
exception = CLRInterop::getLastException();
while(exception)
{
error( exception.get_Message() );
exception = exception.get_InnerException();
}
throw Exception::Error;
}

Exception error in info


D365 exception message 

public  str getErrorStr()
    {
        SysInfologEnumerator enumerator;
        SysInfologMessageStruct msgStruct;
        Exception exception;
        str error;
        enumerator = SysInfologEnumerator::newData(infolog.cut());
        while (enumerator.moveNext())
        {
            msgStruct = new SysInfologMessageStruct(enumerator.currentMessage());
            exception = enumerator.currentException();
            error = strfmt("%1 %2", error, msgStruct.message());
        }
        return error;
    }

how to get original record of a table in Dynamics D365 ,ax 2012

   public void update()

    {
        InventTable     this_Orig           = this.orig();
      
        ttsbegin;

       super();

      
        if (this_Orig.SalesModel                       != this.SalesModel              ||
            this_Orig.SalesPriceModelBasic      != this.SalesPriceModelBasic    ||
this_Orig.SalesContributionRatio    != this.SalesContributionRatio  ||
            this_Orig.SalesPercentMarkup        != this.SalesPercentMarkup)
        {
            InventTable::updateAutoSalesPrice(this.ItemId);
        }
        ttscommit;
    }

HOW TO SELECT MULTIPLE VALUES IN ENUMS IN D365 CODE

 Below code is used to select multiple enum values in a while loop via a container..


SalesTable  salesTable;
container   con = [SalesType::Sales, SalesType::ReturnItem];
while select SalesId from salesTable
where salesTable.SalesType in con
{
Info(salesTable.SalesId);
}

Import Data in Excel

 Make a new project with the following class.

class SDExcelExport{}
public static void main(Args args)
{

CustTable custTable;
SysExcelApplication application;
SysExcelWorkBooks workbooks;
SysExcelWorkBook workbook;
SysExcelWorksheets worksheets;
sysExcelWorksheet worksheet;
SysExcelCells cells;
SysExcelCell cell;
int row;

;

application = SysExcelApplication::construct();
workbooks = application.workbooks(); //gets the workbook object
workbook = workbooks.add(); // creates a new workbook
worksheets = workbook.worksheets(); //gets the worksheets object
//The following selects the first worksheet in the workbook to insert data
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
// numberFormat ‘@’ is to insert data as Text

cells.range('A:A').numberFormat('@');
while select custTable
//The following loop will provide the data to be populated in each column
{
row++;
cell = cells.item(row,1);
cell.value(custTable.AccountNum);
cell = cells.item(row,2);
cell.value(custTable.Name);
cell = cells.item(row,3);
cell.value(CustTable.CustGroup);
cell = cells.item(row,4);
cell.value(CustTable.Currency);
cell = cells.item(row,5);
cell.value(CustTable.CreditMax);
cell = cells.item(row,6);
cell.value(CustTable.CreditRating);
}
application.visible(true); '// opens the excel worksheet
}

how to add range the query of the contract class in D365 code.



 private void initQuery(Contractclass _contract)
    {
        query   = _contract.getQuery();
        wmsQBDS = query.dataSourceTable(tableNum(inventtable));

        QueryBuildRange      errorRange = wmsQBDS.addRange(fieldNum(inventtable, Isopen));

        errorRange.value(queryValue(NoYes::No));
        errorRange.status(RangeStatus::Locked);

        QueryBuildDataSource QBDS =

       QBDS.addDataSource(tableNum(NewTable));
        wmsHeaderQBDS.relations(true);
        wmsHeaderQBDS.joinMode(JoinMode::ExistsJoin);
        SysQuery::findOrCreateRange(wmsHeaderQBDS, fieldNum(NewTable, Newfield)).value('newrange');
     
    }

HOW TO CREATE A LINK TO ITEM FORM ON A FIELD.

 public void jumpRef()

{
Args args;
MenuFunction menuFunction;
args = new args();
args.record(InventTable::find(this.valueStr()));
menuFunction = new MenuFunction(menuitemDisplayStr(EcoResProductDetailsExtended), MenuItemType::Display);
menuFunction.run(args);
}