static void OutlookAppointment(Args _args)
{
COM sysOutlookCollection;
COM receipiants;
COM collection;
COMVariant comStartDate = new COMVariant();
COMVariant comEndDate = new
COMVariant();
COM c;
#SysOutLookCOMDEF
#define.mapi("MAPI")
#define.outlook("Outlook.Application")
COM sysOutlook;
COM sysOutlookNameSpace;
COM sysOutlookMAPIFolder;
sysOutlook = new COM(#outlook);
sysOutlookNameSpace = sysOutlook.getNamespace(#mapi);
sysOutlookNameSpace.logon();
sysOutlookMAPIFolder = sysOutlookNameSpace.getDefaultFolder(#OlDefaultFolders_olFolderTasks);
collection = sysOutlookMAPIFolder.items();
c = collection.add();
comStartDate.date(today());
comStartDate.time(str2Time( "12:00:00"));
comEndDate.date(today());
comEndDate.time(str2Time( "12:45:00"));
c.subject("Meeting regd Microsoft Dynamics AX 2012");
c.body("Lets discuss on whats new in DAX 2012");
c.save();
if (c)
{
receipiants = c.Recipients();
receipiants.add("test@microsoft.com");
receipiants.ResolveAll();
c.assign();
//c.display();
c.send();
info("The action is created in Microsoft Outlook");
}
else
throw error("@SYS31969");
sysOutlookNameSpace.logoff();
}
Friday, March 29, 2013
Dynamics AX 2012: Job to create and sent outlook Tasks
How add financial dimension on forms inside Ax2012
How add financial dimension on forms inside Ax2012
1. Perform
a) Open
AOT>>Data Dictionary>>Extended Data Types type/select
DimensionDefault and drag it in table which will be used further as a
datasource in form where you have to show the Dimensions. Do Remember
that you have to drag it in table not at DataSource.
b) Open
Table in the Data, Dictionary which will be used as a Datasource, and
create a realtion with table DimensionAttributeValueSet .
c) Right
Click the Relations. Select ‘New Realation’. Select properties. Set
name as DimensionAttributeValueSet, Table as DimensionAttributeValueSet.
d) Right Click the this newly created Relation DimensionAttributeValueSet, select New>>Normal.
e) Set the properties of Normal Realtion as: Field=TheFieldwhichwillsaveDimensionNumberInYourTable
Source EDT= DimensionDefault
Related Field=RecId
2. Verify that the table that will hold the foreign key to the DimensionAttributeValueSet table is a
data source on the form(the one on which you have to show dimensions).
3. Create a tab that will contain the financial dimensions control. This control is often the only
data shown on the tab because the number of financial dimensions can be large.
4. set properties of Tab as under
a) Set the Name metadata of the tab to TabFinancialDimensions.
b) Set the AutoDeclaration metadata of the tab to Yes.
c) Set the Caption metadata of the tab to @SYS101181 (Financial dimensions).
d) Set the NeedPermission metadata of the tab to Manual.
e) Set the HideIfEmpty metadata of the tab to No.
5. Override the pageActivated method on the new tab
public void pageActivated()
{
dimDefaultingController.pageActivated();
super();
}
6. Override the following methods on the form.
class declaration
public class FormRun extends ObjectRun
{
DimensionDefaultingController dimDefaultingController;
}
init (for the form):
public void init()
{
super();
dimDefaultingController=DimensionDefaultingController::constructInTabWithValues(
true,
true,
true,
0,
this,
tabFinancialDimensions,
"@SYS138487");
dimDefaultingController.parmAttributeValueSetDataSource(myTable_ds,
fieldstr(myTable, DefaultingDimension));
}
7. Override the following methods on the form data source
public int active()
{
int ret;
ret = super();
dimDefaultingController.activated();
return ret;
}
public void write()
{
dimDefaultingController.writing();
super();
}
public void delete()
{
super();
dimDefaultingController.deleted();
}
Thursday, March 28, 2013
Dynamics Ax 2012 - Default Financial Dimension for Customer
static void setDefaultDimensionToCustomer(Args _args)
{
CustTable custTable;
Struct struct = new Struct();
container ledgerDimension;
DimensionDefault DimensionDefault;
;
struct.add('Abteilung', '02');
struct.add('Kostenstellen', '00200');
ledgerDimension += struct.fields();
ledgerDimension += struct.fieldName(1);
ledgerDimension += struct.valueIndex(1);
ledgerDimension += struct.fieldName(2);
ledgerDimension += struct.valueIndex(2);
ttsBegin;
DimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(ledgerDimension);
custTable = CustTable::find("22027", true);
custTable.DefaultDimension = DimensionDefault;
custTable.update();
ttsCommit;
}
{
CustTable custTable;
Struct struct = new Struct();
container ledgerDimension;
DimensionDefault DimensionDefault;
;
struct.add('Abteilung', '02');
struct.add('Kostenstellen', '00200');
ledgerDimension += struct.fields();
ledgerDimension += struct.fieldName(1);
ledgerDimension += struct.valueIndex(1);
ledgerDimension += struct.fieldName(2);
ledgerDimension += struct.valueIndex(2);
ttsBegin;
DimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(ledgerDimension);
custTable = CustTable::find("22027", true);
custTable.DefaultDimension = DimensionDefault;
custTable.update();
ttsCommit;
}
Dynamics AX 2012 - Run Base Batch
AX 2012 – Business Operation Framework - Create Batch without using RunBaseBatch Framework - Part I
Introduction:
You can develop components / write business logic and later can be hosted as services on AOS with help of Windows communication foundation. These services later can be used by or integrated with the third party applications in order to communicate with Dynamics AX.
BOF Artifacts:
Data Contract Class:
By name only you can identify that this class is going to hold input parameters and query. In simple terms these are fields/controls which we add on ‘Dialog’ to get input from user like FromDate, query criteria’s through select button.
To identify class as Data Contract, we need to use [DataContractAttribute] as below,
And for parm methods, [DataMemberAttribute]
Note:Thumb rule, if you have 10 fields on ‘Dialog’ then you should have 10 parm method.
Service Operation Class:
This class contains main business logic for which you designed/create ‘Data Contract’ class.
UI Builder Class:
BOF automatically generates ‘Dialog UI’ based upon Data Contract class, but if we wanted to access dialog fields at run time for validations like ‘enabled/allowEdit/Lookup’ then we need to create separate class for user interface.
Let us follow below steps in order to create batch class without using ‘RunBaseBatch’ framework, instead we make use of new BOF framework.
Step1:
Create Data Contract class as below,
- [DataContractAttribute]
- class Tutorial_TestDataContract
- {
- TransDate transDate;
- CustAccount custAccount;
- }
- [DataMemberAttribute]
- public TransDate parmTransDate(TransDate _transDate = transDate)
- {
- transDate = _transDate;
- return transDate;
- }
- [DataMemberAttribute]
- public CustAccount parmCustAccount(CustAccount _custAccount = custAccount)
- {
- custAccount = _custAccount;
- return custAccount;
- }
Once you done with data contract class, create class to write business logic which will make use of above input parameters. It’s like ‘run’ method of any batch class where we write actual business logic.
- class Tutorial_TestBatchServiceOperation extends SysOperationServiceController
- {
- }
- public void new(identifierName _className = "", identifierName _methodName = "", SysOperationExecutionMode _executionMode = 0)
- {
- super();
- this.parmClassName(_className);
- this.parmMethodName(_methodName);
- this.parmExecutionMode(_executionMode);
- }
- public static Tutorial_TestBatchServiceOperation construct()
- {
- ClassName className;
- MethodName runMethodName;
- SysOperationExecutionMode execMode = SysOperationExecutionMode::Synchronous;
- Tutorial_TestBatchServiceOperation testBatchServiceOp;
- className = classStr(Tutorial_TestBatchServiceOperation);
- runMethodName = methodStr(Tutorial_TestBatchServiceOperation, runMyLogic);
- testBatchServiceOp = new Tutorial_TestBatchServiceOperation(className,
- runmethodName,
- execMode);
- testBatchServiceOp.parmDialogCaption("Tutorial SysOperation Batch");
- return testBatchServiceOp;
- }
//You can compare this method to 'run' method of any class
- public void runMyLogic(Tutorial_TestDataContract _data)
- {
- ;
- info(strFmt("You entered %1, %2", _data.parmCustAccount(), _data.parmTransDate()));
- }
- public static void main(Args args)
- {
- Tutorial_TestBatchServiceOperation testBatch;
- testBatch = Tutorial_TestBatchServiceOperation::construct();
- testBatch.startOperation();
- }
It’s very important to run ‘Incremental CIL Generation’ since above class will get executed on AOS.
Step4:
Run above class ‘Tutorial_TestBatchServiceOperation ‘ and you can see similar/familiar user interface for batch class
Click ‘Ok’ and you will get output as below
In next part will try to address how to access fields on dialog at run-time to control validations like ‘allowEdit/enabled’ etc….
Untill then ‘Happy Batching :)
Tuesday, February 12, 2013
SSRS report using Ax-Query and Ranges
http://community.dynamics.com/product/ax/axtechnical/b/axsantoshkumar/archive/2010/01/08/dynamics-ax-simple-ssrs-report-example-by-using-ax-query-and-ranges.aspx
Dynamics Ax - Simple SSRS report example by using Ax-Query and Ranges
------------------------------------------------------------
Dynamics Ax - Simple SSRS report example by using Ax-Query and Ranges
------------------------------------------------------------
In this report I am
going to use the Ax-Query in the SSRS business logic and report
will be generated by using SSRS Business logic.
For this
report…
1)
Define the sample Ax
Query in Dynamics Ax as follows...
2)
In the above
SampleCustomers query I had defined the ranges for the Name
Field.
3)
Now go to VS-2008
Create New Project for Dynamics Ax SSRS
reports.
4)
Open the report from the
Solution explorer and Add the new DataMethod to the report as
follows
5) Rename the DataMethod and do the code as follows
[DataMethod(), AxSessionPermission(SecurityAction.Assert)]
public static DataTable
CustomerDetails(String
custAccName)
{
DataTable custDataTable = new DataTable();
IDictionary<string, object>
ranges = new Dictionary<string, object>();
DataRow customersRow;
DataTable dtASCAllRows = new DataTable("Customers");
DataView dvASC = new DataView();
//Defining ranges
object[] rangeName = new object[] { "CustTable.Name" };
object[] rangeValue = new object[] {
custAccName };
ranges.Add(rangeName[0].ToString(), rangeValue[0]);
// execute query - "SampleCustomers" is our Ax
Query
dtASCAllRows = AxQuery.ExecuteQuery((String.Format("Select
* from {0}", "SampleCustomers"))
,
ranges);
dvASC.Table = dtASCAllRows;
custDataTable.Columns.Add("AccountNum", typeof(string));
custDataTable.Columns.Add("AccountName", typeof(string));
custDataTable.Columns.Add("CustGroup", typeof(string));
// Loop for fetching data for every
record
for (int
intLoopCounter = 0;
intLoopCounter < dvASC.Count; intLoopCounter++)
{
customersRow = custDataTable.NewRow();
customersRow["AccountNum"] = Convert.ToString(dvASC[intLoopCounter]["AccountNum"]);
customersRow["AccountName"] = Convert.ToString(dvASC[intLoopCounter]["Name"]);
customersRow["CustGroup"] = Convert.ToString(dvASC[intLoopCounter]["CustGroup"]);
custDataTable.Rows.Add(customersRow);
}
return custDataTable;
}
6)
Define the new Dataset for
the report.
7) Assign the properties of the dataset as follows and
Map our DataMethod (CustomerDetails) to the Query Property of the
DataSet.
8)
Save the solution. Drag and drop
the Dataset to the Designs node of the report.
9)
Now we can see the preview of the
report by using the Customer name as filter condition.
Assign secuirty key for unassigned AOT objects
AOT Object Assign New SecKey
static void EXU_AOTObject_AssignNewSecKey_BUS(Args _args)
{
#AOT
#Properties
TreeNode path,treeNode,node;
EXU_Tree exuTree;
TreeNodeIterator iterator;
Map map= new Map(Types::Integer,Types::String);
MapEnumerator mapEnumerator;
UtilElementType elementType;
str name,newKeyName,newObjectName;
boolean ret,newObjectRet,checkSecurityKeyExist;
TreeNode newSecuirtyTreenode, setOnObject;
#define.DEL('DEL_')
#define.TMP('TMP')
#define.EXU('EXU_')
#define.SecurityKey("SecurityKey")
#define.DisplayMenuItem("DisplayMenuItem")
#define.ActionMenuItem("ActionMenuItem")
#define.OutputMenuItem("OutputMenuItem")
#define.Menu("Menu")
#define.View("View")
#define.Table("Table")
;
map.insert(1,#DisplayMenuItem);
map.insert(2,#ActionMenuItem);
map.insert(3,#OutputMenuItem);
map.insert(4,#Menu);
map.insert(5,#View);
map.insert(6,#Table);
mapEnumerator = map.getEnumerator();
while(mapEnumerator.moveNext())
{
iterator = null;
treeNode = null;
info(mapEnumerator.currentValue());
switch(any2int(mapEnumerator.currentKey()))
{
case 1:
path = treenode::findNode(#MenuItemsDisplayPath);
break;
case 2:
path = treenode::findNode(#MenuItemsActionPath);
break;
case 3:
path = treenode::findNode(#MenuItemsOutputPath);
break;
case 4:
path = treenode::findNode(#MenusPath);
break;
case 5:
path = treenode::findNode(#ViewsPath);
break;
case 6:
path = treenode::findNode(#TablesPath);
break;
default:
continue;
}
iterator = path.AOTiterator();
treeNode = iterator.next();
while(treeNode)
{
name = SysTreeNode::applObjectName(treeNode.treeNodePath()); // Return the current Object Name
if(strScan(name,#DEL,1,strLen(name))|| strScan(name,#TMP,1,strLen(name))) // Check Object Name start with 'DEL_' or 'TMP'
{
ret = true;
}
if(SysTreeNode::isNodeInLayer(treeNode, UtilEntryLevel::bus) && //Check Object exist in BUS layer or not
treeNode.AOTgetProperty(#PropertySecuritykey) == '' && // Check Object Property i.e.(Security Key is blank)
!ret)
{
newKeyName = #EXU + name ; // A new unique security key name
newObjectName = treeNode.newObjectName(newKeyName); // Suggest a new name for the object if exist else return the same name
if(newKeyName != newObjectName) // Check new Object Name exist in AOT or not
{
checkSecurityKeyExist = true; // If name already exist then checkSecuirtyKeyExist = true;
}
if(newKeyName)
{
/*if(!checkSecurityKeyExist)
{
newSecuirtyTreenode = TreeNode::findNode(#SecurityKeysPath); //Find the security key node in AOT
newSecuirtyTreenode.AoTadd(newKeyName); // Create and add a new Secruity Key to AOT
sqlDataDictionary::Synchronize();
}
setOnObject = TreeNode::findNode(treeNode.treeNodePath()); //Find the current Object in the AOT
setOnObject.AOTsetProperty(#SecurityKey,newKeyName); //Assign a newly created Security Key to Objects Properties
setOnObject.AOTsave();*/
info(strfmt("Object Name : %1 ,Security Key Name : %2 , Path : %3, Object type: %4, Layer : %5, Configuration Key : %6",
name,
newKeyName,
treeNode.treeNodePath(),
any2int(mapEnumerator.currentKey()) > 3 ? mapEnumerator.currentValue() : treeNode.AOTgetProperty(#PropertyObjectType),
'bus',
treenode.AOTgetProperty(#PropertyConfigurationkey)
));
}
}
treeNode = treenode.AOTnextSibling();
checkSecurityKeyExist = false;
ret = false;
}
}
}
static void EXU_AOTObject_AssignNewSecKey_BUS(Args _args)
{
#AOT
#Properties
TreeNode path,treeNode,node;
EXU_Tree exuTree;
TreeNodeIterator iterator;
Map map= new Map(Types::Integer,Types::String);
MapEnumerator mapEnumerator;
UtilElementType elementType;
str name,newKeyName,newObjectName;
boolean ret,newObjectRet,checkSecurityKeyExist;
TreeNode newSecuirtyTreenode, setOnObject;
#define.DEL('DEL_')
#define.TMP('TMP')
#define.EXU('EXU_')
#define.SecurityKey("SecurityKey")
#define.DisplayMenuItem("DisplayMenuItem")
#define.ActionMenuItem("ActionMenuItem")
#define.OutputMenuItem("OutputMenuItem")
#define.Menu("Menu")
#define.View("View")
#define.Table("Table")
;
map.insert(1,#DisplayMenuItem);
map.insert(2,#ActionMenuItem);
map.insert(3,#OutputMenuItem);
map.insert(4,#Menu);
map.insert(5,#View);
map.insert(6,#Table);
mapEnumerator = map.getEnumerator();
while(mapEnumerator.moveNext())
{
iterator = null;
treeNode = null;
info(mapEnumerator.currentValue());
switch(any2int(mapEnumerator.currentKey()))
{
case 1:
path = treenode::findNode(#MenuItemsDisplayPath);
break;
case 2:
path = treenode::findNode(#MenuItemsActionPath);
break;
case 3:
path = treenode::findNode(#MenuItemsOutputPath);
break;
case 4:
path = treenode::findNode(#MenusPath);
break;
case 5:
path = treenode::findNode(#ViewsPath);
break;
case 6:
path = treenode::findNode(#TablesPath);
break;
default:
continue;
}
iterator = path.AOTiterator();
treeNode = iterator.next();
while(treeNode)
{
name = SysTreeNode::applObjectName(treeNode.treeNodePath()); // Return the current Object Name
if(strScan(name,#DEL,1,strLen(name))|| strScan(name,#TMP,1,strLen(name))) // Check Object Name start with 'DEL_' or 'TMP'
{
ret = true;
}
if(SysTreeNode::isNodeInLayer(treeNode, UtilEntryLevel::bus) && //Check Object exist in BUS layer or not
treeNode.AOTgetProperty(#PropertySecuritykey) == '' && // Check Object Property i.e.(Security Key is blank)
!ret)
{
newKeyName = #EXU + name ; // A new unique security key name
newObjectName = treeNode.newObjectName(newKeyName); // Suggest a new name for the object if exist else return the same name
if(newKeyName != newObjectName) // Check new Object Name exist in AOT or not
{
checkSecurityKeyExist = true; // If name already exist then checkSecuirtyKeyExist = true;
}
if(newKeyName)
{
/*if(!checkSecurityKeyExist)
{
newSecuirtyTreenode = TreeNode::findNode(#SecurityKeysPath); //Find the security key node in AOT
newSecuirtyTreenode.AoTadd(newKeyName); // Create and add a new Secruity Key to AOT
sqlDataDictionary::Synchronize();
}
setOnObject = TreeNode::findNode(treeNode.treeNodePath()); //Find the current Object in the AOT
setOnObject.AOTsetProperty(#SecurityKey,newKeyName); //Assign a newly created Security Key to Objects Properties
setOnObject.AOTsave();*/
info(strfmt("Object Name : %1 ,Security Key Name : %2 , Path : %3, Object type: %4, Layer : %5, Configuration Key : %6",
name,
newKeyName,
treeNode.treeNodePath(),
any2int(mapEnumerator.currentKey()) > 3 ? mapEnumerator.currentValue() : treeNode.AOTgetProperty(#PropertyObjectType),
'bus',
treenode.AOTgetProperty(#PropertyConfigurationkey)
));
}
}
treeNode = treenode.AOTnextSibling();
checkSecurityKeyExist = false;
ret = false;
}
}
}
AOT Objects which are not having Security Key
Finding AOT objects which are not having Security key
static void EXU_AOTObject_NoSecKey_BUS(Args _args)
{
#AOT
#Properties
TreeNode path,treeNode,node;
EXU_Tree exuTree;
TreeNodeIterator iterator;
Map map= new Map(Types::Integer,Types::String);
MapEnumerator mapEnumerator;
;
map.insert(1,"DisplayMenuItem");
map.insert(2,"ActionMenuItem");
map.insert(3,"OutputMenuItem");
map.insert(4,"Menu");
map.insert(5,"View");
map.insert(6,"Table");
mapEnumerator = map.getEnumerator();
while(mapEnumerator.moveNext())
{
iterator = null;
treeNode = null;
info(mapEnumerator.currentValue());
switch(any2int(mapEnumerator.currentKey()))
{
case 1:
path = treenode::findNode(#MenuItemsDisplayPath);
break;
case 2:
path = treenode::findNode(#MenuItemsActionPath);
break;
case 3:
path = treenode::findNode(#MenuItemsOutputPath);
break;
case 4:
path = treenode::findNode(#MenusPath);
break;
case 5:
path = treenode::findNode(#ViewsPath);
break;
case 6:
path = treenode::findNode(#TablesPath);
break;
default:
continue;
}
iterator = path.AOTiterator();
treeNode = iterator.next();
while(treeNode)
{
if(SysTreeNode::isNodeInLayer(treeNode, UtilEntryLevel::bus))
{
if(treeNode.AOTgetProperty(#PropertySecuritykey) =='')
{
info(strfmt("Path : %1, Object type: %2, Layer : %3, Configuration Key : %4",
treeNode.treeNodePath(),
any2int(mapEnumerator.currentKey()) > 3 ? mapEnumerator.currentValue() : treeNode.AOTgetProperty(#PropertyObjectType),
'bus',
treenode.AOTgetProperty(#PropertyConfigurationkey)
));
}
}
treeNode = treenode.AOTnextSibling();
}
}
}
static void EXU_AOTObject_NoSecKey_BUS(Args _args)
{
#AOT
#Properties
TreeNode path,treeNode,node;
EXU_Tree exuTree;
TreeNodeIterator iterator;
Map map= new Map(Types::Integer,Types::String);
MapEnumerator mapEnumerator;
;
map.insert(1,"DisplayMenuItem");
map.insert(2,"ActionMenuItem");
map.insert(3,"OutputMenuItem");
map.insert(4,"Menu");
map.insert(5,"View");
map.insert(6,"Table");
mapEnumerator = map.getEnumerator();
while(mapEnumerator.moveNext())
{
iterator = null;
treeNode = null;
info(mapEnumerator.currentValue());
switch(any2int(mapEnumerator.currentKey()))
{
case 1:
path = treenode::findNode(#MenuItemsDisplayPath);
break;
case 2:
path = treenode::findNode(#MenuItemsActionPath);
break;
case 3:
path = treenode::findNode(#MenuItemsOutputPath);
break;
case 4:
path = treenode::findNode(#MenusPath);
break;
case 5:
path = treenode::findNode(#ViewsPath);
break;
case 6:
path = treenode::findNode(#TablesPath);
break;
default:
continue;
}
iterator = path.AOTiterator();
treeNode = iterator.next();
while(treeNode)
{
if(SysTreeNode::isNodeInLayer(treeNode, UtilEntryLevel::bus))
{
if(treeNode.AOTgetProperty(#PropertySecuritykey) =='')
{
info(strfmt("Path : %1, Object type: %2, Layer : %3, Configuration Key : %4",
treeNode.treeNodePath(),
any2int(mapEnumerator.currentKey()) > 3 ? mapEnumerator.currentValue() : treeNode.AOTgetProperty(#PropertyObjectType),
'bus',
treenode.AOTgetProperty(#PropertyConfigurationkey)
));
}
}
treeNode = treenode.AOTnextSibling();
}
}
}
Query by code in AX
Query Using Dyna Link
http://community.dynamics.com/product/ax/axtechnical/b/axaptavsme/archive/2012/07/17/building-a-query-object.aspx
static void CustTableSales(Args _args)
{
Query query;
QueryBuildDataSource qbds1;
QueryBuildDataSource qbds2;
QueryBuildRange qbr1;
QueryBuildRange qbr2;
QueryRun queryRun;
CustTable custTable;
;
query = new Query();
qbds1 = query.addDataSource(tablenum(CustTable));
qbds1.addSortField(
fieldnum(CustTable, Name),
SortOrder::Ascending);
qbr1 = qbds1.addRange(fieldnum(CustTable,Blocked));
qbr1.value(queryvalue(CustVendorBlocked::No));
qbr2 = qbds1.addRange(fieldnum(CustTable,CustGroup));
qbr2.value(queryvalue(’10′));
qbds2 = qbds1.addDataSource(tablenum(SalesTable));
qbds2.relations(false);
qbds2.joinMode(JoinMode::ExistsJoin);
qbds2.addLink(
fieldnum(CustTable,AccountNum),
fieldnum(SalesTable,CustAccount));
queryRun = new QueryRun(query);
while (queryRun.next())
{
custTable = queryRun.get(tablenum(CustTable));
info(strfmt(
“%1 – %2″,
custTable.Name,
custTable.AccountNum));
}
}
Query Using without Dyna Link
http://community.dynamics.com/product/ax/axtechnical/b/axaptavsme/archive/2012/07/17/building-a-query-object.aspx
static void CustTableSales(Args _args)
{
Query query;
QueryBuildDataSource qbds1;
QueryBuildDataSource qbds2;
QueryBuildRange qbr1;
QueryBuildRange qbr2;
QueryRun queryRun;
CustTable custTable;
;
query = new Query();
qbds1 = query.addDataSource(tablenum(CustTable));
qbds1.addSortField(
fieldnum(CustTable, Name),
SortOrder::Ascending);
qbr1 = qbds1.addRange(fieldnum(CustTable,Blocked));
qbr1.value(queryvalue(CustVendorBlocked::No));
qbr2 = qbds1.addRange(fieldnum(CustTable,CustGroup));
qbr2.value(queryvalue(’10′));
qbds2 = qbds1.addDataSource(tablenum(SalesTable));
qbds2.relations(false);
qbds2.joinMode(JoinMode::ExistsJoin);
qbds2.addLink(
fieldnum(CustTable,AccountNum),
fieldnum(SalesTable,CustAccount));
queryRun = new QueryRun(query);
while (queryRun.next())
{
custTable = queryRun.get(tablenum(CustTable));
info(strfmt(
“%1 – %2″,
custTable.Name,
custTable.AccountNum));
}
}
Query Using without Dyna Link
static void Query_Example(Args _args)
{
Query q;
Queryrun qr;
QueryBuildRange qbr;
QueryBuildDataSource qbds;
InventTrans iv;
Real Total;
str range;
/* The following query produces the same results as:
while select sum(qty) from inventTrans
where (inventtrans.ItemId == "OL-1500") || inventtrans.ItemId == "OL-1000"
join inventDim
group by inventBatchId
where inventDim.InventDimId == inventTrans.InventDimId */
// Instantiate the query class.
q = new query("Inventory_Transactions");
// Create a data source by using the InventTrans table.
qbds = q.addDataSource(tablenum(InventTrans));
// Select only the Qty field, and then sum the Qty field.
qbds.addSelectionField(fieldnum(InventTrans,Qty),selectionfield::Sum);
// Set the range to the ItemId field.
qbr = qbds.addRange(fieldnum(InventTrans,ItemId));
// The range for the where statement specifies an 'or' statement.
qbr.value(strfmt('((%1 == "%2") || (%1 == "%3"))',fieldstr(inventtrans,ItemId),'OL-1500','OL-1000'));
// The following is the alternative way to enter the range.
// This also limits the selection on the ItemId values.
range = strfmt('((ItemId == "%1")||(ItemID =="%2"))',queryvalue('OL-1500'),queryvalue('OL-1000'));
qbr.value(range);
// Create the join to the InventDim table.
qbds = qbds.addDataSource(tablenum(InventDim));
// Specify the table relationship.
qbds.relations(true);
// Indicate the order mode as the grouping.
qbds.orderMode(ordermode::GroupBy);
// Specify the grouping on the InventBatchId field.
qbds.addSortField(fieldnum(InventDim,InventBatchId));
// Instantiate the QueryRun class for the form.
qr = new QueryRun(q);
// If the user clicks OK, continue.
if (qr.prompt())
{
// While there are records in the query, continue.
while (qr.next())
{
// Set the value of the query to the iv table.
bufferiv = qr.get(tablenum(InventTrans));
// Create the value of the total field.
total = iv.Qty;
}
}
// Specify the quantity for the item.
info(strfmt("Quantity: %1",total));
// Indicate the SQL string that is used for the query.
info (qr.query().dataSourceNo(1).toString());
}
Subscribe to:
Comments (Atom)



