Tuesday, April 16, 2013

Hiding Content pane of AX 2009

The below code is to hide the content pane of Ax 2009
 
static void hideContentPaneWindow(Args _args)
{
#WinApi
HWND contentPane = WinApi::findWindowEx(WinAPI::findWindowEx(infolog.hWnd(), 0, ‘MDIClient’, ”),0,’ContentFrame’,” );
;
if (contentPane)
WinApi::ShowWindow(contentPane,#SW_HIDE); // To restore use #SW_RESTORE
}

Insert Values to Item Group Table from Excel


Hi Everyone,
Here I published the X++ code for Inserting Excel Data in to ItemGroup Table.
Condition: If the importing values are same as the values in the table then the values will not be uploaded, if the description of the item differs then the description only updated in the table.

Void Clicked()
{
SysExcelApplication application;
SysExcelWorkbooksworkbooks;
SysExcelWorkbookworkbook;
SysExcelWorksheetsworksheets;
SysExcelWorksheetworksheet;
SysExcelCellscells;
COMVariantType type;
System.DateTimeShlefDate;
FilenameOpenfilename;
dialogFielddialogFilename;
Dialogdialog;
InventItemGroupinventItemGroup1;
ItemGroupId itemGroupId;
Name itemGroupName,itemGroupName1;
introw;
#Excel

// convert into str from excel cell value
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("Upoad from Excel");
dialogFilename=dialog.addField(typeId(FilenameOpen));
dialog.filenameLookupFilter(["@SYS28576",#XLS,"@SYS28576",#XLSX]);
dialog.filenameLookupTitle("Upload from Excel");
dialog.caption("Upload from Excel");
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();

try
{
ttsbegin;
do
{
row++;
itemGroupId=COMVariant2Str(cells.item(row, 1).value());
itemGroupName=COMVariant2Str(cells.item(row,2).value());
if(row>1)
{
if(substr(itemGroupId,1,1) ==’6′)
{
itemGroupId = substr(itemGroupId,2,strlen(itemGroupId));
inventItemGroup = inventItemGroup::find(itemGroupId);
itemGroupName1= inventItemGroup::find(itemGroupId).Name;
if(inventItemGroup)
{

if(itemGroupName1!=itemGroupName)
{
select forupdate inventItemGroup1 where inventItemGroup1.ItemGroupId == itemGroupId;
inventItemGroup1.Name = itemGroupName;
inventItemGroup1.update();
info(strfmt("Item Description for Item GroupId (%1) Updated succesfully",ItemGroupId));
}
}
else
{
inventItemGroup1.ItemGroupId = itemGroupId;
inventItemGroup1.Name= itemGroupName;
inventItemGroup1.insert();
info(strfmt("Item GroupId (%1) uploaded succesfully",ItemGroupId));
}
}
}
type = cells.item(row+1, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
application.quit();
ttscommit;
}
catch
{
Error("Upload Failed");
application.quit();
}
}

 

Create LookUps Using X++ in Axapta

Hi All,
Here I created Lookups using X++ Code (without Ax Table or EDT relation). For that i override a String(Text) Control’ s lookup method.

public void lookup()
{
//super();
// Added by Ashok Koduri
// Declaration
Query LookupQuery = new Query();
QueryBuildDataSource LookupQueryBuildDataSource;
QueryBuildRange LookupQueryBuildRange;
SysTableLookup CustomSysTableLookup = SysTableLookup::newParameters(tableNum(InventTable), this);
;

// Add fields that you want in Lookups
CustomSysTableLookup.addLookupField(fieldNum(InventTable, ItemId));
CustomSysTableLookup.addLookupField(fieldNum(InventTable,ItemName));
CustomSysTableLookup.addLookupField(fieldNum(InventTable,ItemGroupId));
CustomSysTableLookup.addLookupField(fieldNum(InventTable,NameAlias));
CustomSysTableLookup.addLookupField(fieldNum(InventTable,ItemType));
CustomSysTableLookup.addLookupField(fieldNum(InventTable,DimGroupId));
// Add DataSource, Range and Value
LookupQueryBuildDataSource = LookupQuery.addDataSource(tableNum(InventTable));
LookupQueryBuildRange = LookupQueryBuildDataSource.addRange(fieldNum(InventTable,ItemVisibility));
LookupQueryBuildRange.value(queryValue(NOYESCOMBO::Yes));
// Execute the Query
CustomSysTableLookup.parmQuery(LookupQuery);
CustomSysTableLookup.performFormLookup();
// Added by Ashok Koduri
}

Using AOT Query Object in X++ Code

Hi,
Here i posted the code to use the AOT Query Object in our X++ code.
Steps:
1) Create a AOT Query Object as your requirement.
2) Create a Job and paste the below code to check the Query Object.

static void ExecuteAOTQuery(Args _args)
{
QueryRun queryRun;
Counter totalRecords;
;
queryRun = new QueryRun(queryStr(CreatedAOTQueryName));
if (queryRun.prompt())
{
while (queryRun.next())
{
totalRecords++;
}
}
info(strFmt(“Total Records : %1”, totalRecords));
}

Dynamic Query Using Aggregative Functions and Joins in Axapta 2009

Hi All,
Here i written the code to use the Aggregate functions and Joins inDynamic Query in spite of using normal Select statement in Ax 2009
Normal Select Statement:
while select sum(qty) from inventTrans where inventTrans.ItemId == “OL-2500” join  inventDimgroup by inventBatchId where inventDim.InventDimId == inventTrans.InventDimId
{
// Our Code Here
}
Dynamic Query:
static void Vasanth_Query_Eg1(Args _args)
{
Query query;
QueryBuildDataSource queryBuildDataSource;
QueryBuildRange queryBuildRange;
QueryRun queryRun;
Qty total;
InventTrans inventTrans;
;
query = new Query(); queryBuildDataSource = query.addDataSource(tableNum(InventTrans));
queryBuildDataSource.addSelectionField(fieldNum(InventTrans,Qty),
SelectionField::Sum);
queryBuildDataSource.orderMode(OrderMode::GroupBy);
queryBuildRange = queryBuildDataSource.addRange(fieldNum(InventTrans,ItemId));
queryBuildDataSource = queryBuildDataSource.addDataSource(tableNum(InventDim));
queryBuildDataSource.orderMode(OrderMode::GroupBy);
queryBuildDataSource.addSortField(fieldNum(InventDim,InventBatchId));
queryBuildDataSource.relations(true);
queryRun = new QueryRun(query);
if (queryRun.prompt())
{
while (queryRun.next())
{
inventTrans = queryRun.get(tableNum(InventTrans));
total = inventTrans.Qty;
}
}
info(strFmt(“Quantity: %1″, total));
}

RegularExpression in Axapta

Hi All,
Here i posted a small job , to explain how we use regular expressin in Axapta. Try it.
static void RegularExpression_Test()
{
int nameLength,myStringLength;
str myname = “Ashok”;
str myString = “1234″;
str formatString , formatString1;
System.Text.RegularExpressions.Regex regEx;
System.Text.RegularExpressions.Regex regEx1;
System.Text.RegularExpressions.Match regMatch;
InteropPermission permission = new InteropPermission(InteropKind::ClrInterop);
boolean retVal, retVal1;
;
nameLength = strLen(myname);
myStringLength = strLen(myString);
formatString = strfmt(@”^[0-9]{%1}”, myStringLength);
formatString1 = strfmt(@”^[a-zA-Z ]{%1}”, nameLength);
permission.assert();
//BP Deviation documented
regEx = new System.Text.RegularExpression.Regex(formatString);
regEx1 = new System.Text.RegularExpression.Regex(formatString1);
regMatch = regEx.Match(myString);
retVal = regMatch.get_Success();
print retVal; // Returns True;
regMatch = regEx1.Match(myname);
retVal1 = regMatch.get_Success();
print retVal1 // Returns True;
}

“Go To Main Table” Option in our Axapta

Hi All,
We all know about “Go To Main Table” Option in our Axapta Forms and Tables. Here I wrote one sample for how to get that functionality in our forms or table. This is done by three ways:
1) By EDT Relations
2) By using JumpRef method
3) By using FormRef property in Table
EDT Relations:
If you use an EDT in tables which have relation with some other table fileds, that time you can able to navigate the main table or main form.
FormRef Property:
Select the Table and go to properties and select the required form in the FormRef property.
JumpRef method:
If you are not having that option, simply write a override the JumpRef method in a field of DataSource or in a Form Control. Here i show you a sample jumpRef method code:
public void jumpRef()
{
Args args;
MenuFunction menuFunction;
;
args = new Args();
menuFunction = new MenuFunction(menuitemDisplayStr(“FormName”), MenuItemType::Display);
args = new Args(menuFunction.object());
args.caller(element);
args.record(“RecordName”); // to be a datasource which added in the current form
menuFunction.run(args);
}
In the form init() which we called here, we have to check the condition whether the form is called by any dataset or not.................!!!