Monday, 5 January 2015

How to Consume AX code in C# using Custom Service?

1. I will demonstrate now, how to expose AIF Web Service out of the AX.

2. In this, I will expose AX Data out of the application with a simple Example.

3. Create table named SampleAIFTbl with 3 fields (Name, AddressCity,
PhoneMobile), Class named SampleAIFServiceClass which have one
method (getSampleList()).

//code
[SysEntryPointAttribute(true),
AifCollectionTypeAttribute('return', Types::String)]
//return denotes the parameter type and String specifies
//the type of data the collection will hold

public List getSampleList()
{
SampleAIFTbl sampleAIFTbl;
List list = new List(Types::String);
//select all rows
while select * from sampleAIFTbl
{
//add name in the list
list.addEnd(sampleAIFTbl.Name);
//list.addEnd(" ");
}
return list;
}
4. getSampleList() method takes Name field data and insert into List object
of type String and return it C# by AIF Web Service through WSDL Port.

5. Create Service from AOT node and select the class created in previous
step in property node, select add operation node, add the method of
class SampleAIFServiceClass and select ok.

6. Create Service Group from AOT node, right click Service Group & select
“New Service Node Reference”, select auto deploy to yes and deploy the
service.
7. Open Company/System administration/Area page/Inbound Ports, select
the created service which is successfully deployed and copy the URI (wait).

8. Open visual studio, create project and select Console Application, once it
opens, open the solution explorer, right click on it & select Add project to
AOT
9. Once project added to AOT, Select Add Service Reference, paste URI and
click Go, give appropriate name so press ok.

10. We can use that service as per requirement, as I am just displaying those
data at console.

Note: Whenever you made modification in AX, deactivate the service, generate CIL
Generation & deploy the service again and in VS, update service reference to avoid errors.

C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;
using SampleAIFConsole.SampleAIFServiceReference;

namespace SampleAIFConsole
{
class Program
{
static void Main(string[] args)
{

SampleAIFServiceReference.SampleAIFServiceClient client;

try
{

SampleAIFServiceReference.CallContext cc = new
SampleAIFServiceReference.CallContext();
client = new SampleAIFServiceClient();
cc.Company = "ussi";
foreach (string s in client.getSampleList(cc))
{
Console.Out.WriteLine("the values are " + s);
}

Console.ReadLine();

}
catch (Exception e)
{

Console.Out.WriteLine(e.ToString());
Console.ReadLine();
}

}
}
}
You can code it, debug it, run it, see the output and modified it as you want.

Monday, 1 July 2013

Get Current Company in AX 2009

static void curExtExample(Args _arg)
{
    str curCompany;
    ;
 
    // Sets curCompany to the extension of the current company.
    curCompany= curExt();
    print "Current extension is " + curCompany;
    pause;
}

Todays date in ax 2009 report

To Get the todays date in the Ax 2009 report write the following code in display method of the report

display public str ABC_TodaysDate()
{
    return date2Str(today(), 123, DateDay::Digits2, DateSeparator::Slash, DateMonth::Digits2, DateSeparator::Slash, DateYear::Digits4);
}

Wednesday, 20 March 2013

Passing Args from Form to Class in Dynamics Ax 2012

In this post, i will demonstrate on how to pass Args from Form to Class in Dynamics Ax.


This post will helpful for those who are keen to learn about Args which is very much fruitful concept in Dynamics Ax.

The Scenario is when I select multiple grids in Form , those records will be passed to Class and once you received those records in Class and you can use it as per your Requirement.

Firstly, make one form from which you will pass the arguments and drag your table to the datasource, then drag the fields to the grid of design node, also drag one button adn override the clicked method.

write the following code in clicked method.
void clicked()
{
int recordsCount;
SampleTbl _SampleTbl;
container con;
Args args;
str multiSelectString;
;

args = new Args();
recordsCount = SampleTbl_ds.recordsMarked().lastIndex(); // gets the total records selected
_SampleTbl = SampleTbl_ds.getFirst(1);

while (_SampleTbl)
{
// storing recid of selected field in container
con = conIns(con,1,_SampleTbl.RecId);

// converting container to string with comma separated
multiSelectString = con2Str(con,',');

_SampleTbl = SampleTbl_ds.getNext(); // moves to next record
}
// passing string
args.parm(multiSelectString);
// calling menu item
new MenuFunction(identifierstr(SampleTest), MenuItemType::Action).run(args);


}

first
make one class and write the following code in main method of it.
public static void main(Args args)
{
SampleTbl _SampleTbl;
container con;
int i;
str multipleRecords;
;
multipleRecords = args.parm();
info(strFmt('%1',multipleRecords));
// i am just here an infolog of recids but you can do as per requirement once you got multiple records
}


thanks and your feedback will be appreciated

Passing Multiple Records from one form to another through Args in Dynamics Ax 2012


In my last post, i posted on how to pass Record from one Form to another Form. Now i will pass multiple Record from one Form to another through Args.


The Scenario is when I select multiple grids in Form A, those records will be passed to FormB and there FormB will received the all those records and filter it and will show.

Step 1: Make one SampleTable which has three fields named (SIno, name, AddressCity).
Step 2: Make FormA with datasource as SampleTable and drag the datasource fields into the design Grid.
Step 3: Drag one button and override the click method and write the following code.
step 4: Make one Display menuitem of FormB.


void clicked()
{

int recordsCount;
SampleTable _sampleTable;
container con;
Args args;
str multiSelectString;
;


args = new Args();
recordsCount = SampleTable_ds.recordsMarked().lastIndex(); // gets the total records selected
_sampleTable = SampleTable_ds.getFirst(1);

while (_sampleTable)
{
// storing recid of selected field in container
con = conIns(con,1,_sampleTable.RecId);

// converting container to string with comma separated
multiSelectString = con2Str(con,',');

_sampleTable = SampleTable_ds.getNext(); // moves to next record
}
// passing string
args.parm(multiSelectString);
// calling menu item
new MenuFunction(menuitemDisplayStr(FormBMenuItem), MenuItemType::Display).run(args);
}


first

step 5: Make FormB with datasource as same table for which you take for Form A i.e., SampleTable and drag the fields in to the
Grid of design part.
step 7: write the following code in to the init method of FormB.


public void init()
{
container con;
int i;
str multipleRecords;
super();
// getting string value from caller
multipleRecords = element.args().parm();

// string to container
con = str2con(multipleRecords,",");

// for sorting
for(i = 1;i<= conLen(con) ;i++)
{
SampleTbl_ds.query().dataSourceTable(Tablenum(SampleTbl)).addRange(fieldNum(SampleTbl,RecId)).value(SysQuery::value(conPeek(con,i)));
}

}


thanks and your feedback will be appreciated.

Tuesday, 19 March 2013

Passing Args from one Form to another Form in dynamics Ax 2012

Args concept in dynamics ax is awesome. one can easily pass arguments from one Form to another Form with very less effort. Here, i am going to demonstrate how to pass Arguments from Form to Form. The Scenario is when I select one grid in Form A, that record will be passed to Form B and there Form B will received the whole record and filter it and will show.

Step 1: Make one SampleTable which has three fields named (SIno, name, AddressCity).
Step 2: Make FormA with datasource as SampleTable and drag the datasource fields into the design Grid.
Step 3: Drag one button and override the click method and write the following code.

void clicked()
{
Args args;
FormRun formRun;
;
super();
args = new args(formstr(FormB)); // sending Args(record) to FormB
args.record(SampleTable);
formrun = classfactory.formrunclass(args);
formrun.init();
formrun.run();
formrun.wait();
formrun.detach();

}



first

step 4: Make FormB with datasource as same table for which you take for Form A i.e., SampleTable and drag the fields in to the
Grid of design part.

step 5: write the following code in to the init method of FormB.

public void init()
{
SampleTable _sampleTable;
super();
_sampleTable = element.args().record();
SampleTable_ds.query().dataSourceTable(Tablenum(SampleTable)).addRange(fieldNum(SampleTbl,RecId)).
value(SysQuery::value(_sampleTable.Recid));
}

Note: You can pass only one Record with this Code, if you want to pass multiple grids,i will cover in next post.


thanks and your feedback will be appreciated.

Monday, 25 February 2013

Using Tree in Form

1. Firstly, create three tables with the following requirements.

1.1 Create a table (parent table) name: Medicine

Fields type Edt
MedName str
MedDesc str

1.2. Create a table (reference table) name: MedReferencehrr

Fields type Edt
MedRef int64 Medref = reference table: Medicines
DtOwn int64

Relation:
1. ownrelation : MedReferencehrr i.e, DtOwn == recid
2. Foreign key rel : Medicines MedRef = MedReferencehrr .recid



1.3 Create a table( child table ) name : MedDetails

Fields type Edt

MedRef int64 Medref = reference table: Medicines
Name1 str
Desc1 str

Relation: Normal: MedDetails .MedRef = Medicines .recid

1.4 Create a new Form named SampleTree2 and drag 3 tables in the form DS. The following code
must written as it is.


Medicines no relation
MedDetails join source: Medicines type: active
MedReferencehrr
In design: add group in group add a Tree name it as: SampleTree2

Public class FormRun extends ObjectRun
{

boolean isCreatedNew;
boolean isModified;
TreeItemIdx newItemIdx;
TreeItemIdx lastRecordIdx;
int64 lastRecordRecId;
String10 billingGroupRecid;


//
String10 billingeGroupHierarchyRecid;
int64 billingGroupHierarchyParentRecid;


//
QueryBuildRange qbrMedecines;

QueryBuildRange qbrMedReferencehrr;

//
List delRecList;
//

ImageListAppl_HISServices imageAppl;
#define.Root(3)
}
1. Init method :

public void init()
{
super();
imageAppl = new ImageListAppl_HISServices();
imageAppl.addImage(resourceStr(SampleTree),#Root);
SampleTree.setImagelist(imageAppl.imageList());
}

2. Run method :
public void run()
{
super();
if( Medecines.RecId == 0)
{
AddNewService.enabled(true);
}
element.loadTree();

}
3. Loadtree method:

public void loadTree()
{
TreeItemIdx parentItemIdx;

SampleTree2.deleteAll();

parentItemIdx = this.createRootNode();

this.createChildNodes(parentItemIdx,0);

SampleTree2.select(SampleTree2.getRoot());
SampleTree2.expand(SampleTree2.getRoot());
}

4. createRootNode method :

public TreeItemIdx createRootNode()
{
FormTreeItem parentItem;

parentItem = new FormTreeItem("Medicines");
parentItem.data(0);

//parentitem.image(imageAppl.image(#Root));
// parentitem.selectedImage(imageAppl.image(#Root));

returnSampleTree2.addItem(FormTreeAdd::Root,FormTreeAdd::First,parentItem);
}

5. createChildNodes method :

public void createChildNodes(TreeItemIdx _parentIdx, int64 _serviceGroupHierarchyParentRecid)
{
MedReferencehrr MedReferencehrrLocal;
MedReferencehrr MedReferencehrrChild;
Medecines medecines1;
FormTreeItem parentItem;
TreeItemIdx newParentIdx;

while select * from MedReferencehrrLocal
join * from medecines1
where MedReferencehrrLocal.DtOwn == _serviceGroupHierarchyParentRecid
&& MedReferencehrrLocal.MedRef == medecines1.RecId
{

select count(recId) from MedReferencehrrChild
where MedReferencehrrChild.DtOwn == MedReferencehrrLocal.RecId;

if(MedReferencehrrChild.RecId > 0)
{
parentItem = new FormTreeItem();
parentItem.data(MedReferencehrrLocal.RecId);
parentItem.text(medecines1.MedDesc);


newParentIdx = SampleTree2.addItem(_parentIdx,FormTreeAdd::Sort,parentItem);
if(MedReferencehrrLocal.RecId == lastRecordRecId)
{
lastRecordIdx = newParentIdx;
}
element.createChildNodes(newParentIdx,MedReferencehrrLocal.RecId);
}
else
{
parentItem = new FormTreeItem();
parentItem.data(MedReferencehrrLocal.RecId);
parentItem.text(medecines1.MedDesc);


newParentIdx = SampleTree2.addItem(_parentIdx,FormTreeAdd::Sort,parentItem);
if(MedReferencehrrLocal.RecId == lastRecordRecId)
{
lastRecordIdx = newParentIdx;
}
}

}

}



Now add action panel to this tree group . in button group add two buttons type : button name :AddBelow and delete .
In AddBelow button : clicked method :
void clicked()
{
super();
element.addNode(SampleTree2.getSelection());
}
In form Methods : add the method:
addNode method :

public void addNode(TreeItemIdx _parentIdx)
{
FormTreeItem newItem;
FormTreeItem parentItem;

newItem = new FormTreeItem("");
newItem.data(0);
//newItem.image(imageAppl.image(#NormalGroup));
//newItem.selectedImage(imageAppl.image(#NormalGroup));

newItemIdx = SampleTree2.addItem(_parentIdx, FormTreeAdd::Sort,newItem);

SampleTree2.select(newItemIdx);

Medecines_ds.create();

parentItem = SampleTree2.getItem(_parentIdx);
billingGroupHierarchyParentRecid = parentItem.data();
isCreatedNew = true;

AddBelow.enabled(false);
}

In Delete button : clicked method :
void clicked()
{
//super();
FormTreeItem treeItem;

treeItem = BillingTree.getItem(BillingTree.getSelection());

if(element.deleteBillingGroup(treeItem.data()))
{
element.loadTree();
}

}


In form Methods : add the method:
deleteBillingGroup method :
ignore error and cont
public boolean deleteBillingGroup(recId _recId)
{
Medecines medecines2;
MedReferencehrr medReferencehrrlocal;

DialogButton dialogButton;
ListEnumerator enumer;
boolean allDeleted = true;

if(_recId)
{
medReferencehrrlocal = MedReferencehrr::find(_recId);


dialogButton = Box::yesNo('Selected billing group and its subsidary group will be deleted', DialogButton::No);
if( dialogButton == DialogButton::Yes )
{
try
{
delRecList = new List(Types::Int64);

delRecList.addStart(medReferencehrrlocal.MedRef);

element.deleteGroupCode(_recId);

enumer = delRecList.getEnumerator();

ttsBegin;

while(enumer.moveNext())
{
select forupdate medecines2
where medecines2.RecId == enumer.current();

if(medecines2.validateDelete())
{
medecines2.delete();
}
else
{
allDeleted = false;
break;
}
}

if(allDeleted)
{
ttsCommit;
return true;
}
else
{
ttsAbort;
return false;
}

}
catch
{
return false;
}
}
else
{
return false;
}
}


else
{
return false;
}
}
--
In Table MedReferencehrr(In Table not in Form) Create a new method ,write the following code :

public static MedReferencehrr find(
RecId _recid,
boolean _forUpdate = false,
ConcurrencyModel _concurrencyModel = ConcurrencyModel::Auto)
{
MedReferencehrr medReferencehrr ;
medReferencehrr.selectForUpdate(_forUpdate);

if(_forUpdate && _concurrencyModel != ConcurrencyModel::Auto)
{
medReferencehrr.concurrencyModel(_concurrencyModel);
}

select firstonly * from medReferencehrr where medReferencehrr.RecId == _recid;

return medReferencehrr;
}


------
Come Back to Form and Create a New Method In form name : deleteGroupCode


public void deleteGroupCode(recId _parentrecId)
{
MedReferencehrr medReferencehrrChild;
MedReferencehrr medReferencehrrTraverse;

while select * from medReferencehrrTraverse
where medReferencehrrTraverse.DtOwn == _parentrecId

{

select count(recId) from medReferencehrrChild
where medReferencehrrChild.DtOwn == medReferencehrrTraverse.RecId;

if(medReferencehrrChild.RecId > 0)
{
delRecList.addEnd(medReferencehrrTraverse.MedRef);
element.deleteGroupCode(medReferencehrrTraverse.RecId);

}
else
{
delRecList.addEnd(medReferencehrrTraverse.MedRef);
}

}
}


In Form DesignDesigngroupTree SampleTree2 --right click – override method – SelectionChanged :

public void selectionChanged(FormTreeItem _OldItem, FormTreeItem _NewItem, FormTreeSelect _how)
{
super(_OldItem, _NewItem, _how);

if(SampleTree2.getRoot() == SampleTree2.getSelection())
{
AddNewService.enabled(false);
}
else
{
AddNewService.enabled(true);
}

if(isCreatedNew)
{
if(isModified)
{
if(SampleTree2.getSelection() != newItemIdx)
{
//if(HMServiceCatalog_ds.validateWrite())
//{
//HMServiceCatalog_ds.write();
//}
//else
//{
SampleTree2.select(newItemIdx);
//}
}

}
else
{
SampleTree2.delete(newItemIdx);
newItemIdx = 0;
if(Medecines_ds.validateWrite())
{
Medecines_ds.write();
}
isCreatedNew = false;
isModified = false;
AddBelow.enabled(true);
billingeGroupHierarchyRecid = element.getCurrentSelectedItemRecid();
MedReferencehrr_ds.executeQuery();
billingGroupRecid = int642str(MedReferencehrr.MedRef);
Medecines_ds.executeQuery();

}
}
else
{
billingeGroupHierarchyRecid = element.getCurrentSelectedItemRecid();
MedReferencehrr_ds.executeQuery();
billingGroupRecid = int642str(MedReferencehrr.MedRef);
Medecines_ds.executeQuery();

}
}



Now design the form. Take another Group , take action panel with two method,
Name Prop
AddNewService command : New
DeleteNewService command : DeleteRecord

Select a new grid and drag some fields from the Table : MedDetails

In Form DataSource :
1. Medecines

public void init()
{
super();

qbrMedecines = this.query().dataSourceTable(tableNum(Medecines)).addRange(fieldNum(Medecines,recId));
}
public void write()
{
MedReferencehrr MedReferencehrrLocal;

super();

if(isCreatedNew == true)
{
MedReferencehrrLocal.MedRef = Medecines.RecId;
MedReferencehrrLocal.DtOwn = billingGroupHierarchyParentRecid;
MedReferencehrrLocal.insert();

newItemIdx = 0;
billingGroupHierarchyParentRecid = 0;
isCreatedNew = false;
isModified = false;
AddBelow.enabled(true);

element.refresh(MedReferencehrrLocal.RecId);
}
else
{
element.refresh(MedReferencehrr.RecId);
}
}
public void executeQuery()
{
qbrMedecines.value(billingGroupRecid);

super();
}

2. MedReferencehrr

public void init()
{
super();

qbrMedReferencehrr = this.query().dataSourceTable(tableNum(MedReferencehrr)).addRange(fieldNum(MedReferencehrr,recId));
}

public void executeQuery()
{
qbrMedReferencehrr.value(billingeGroupHierarchyRecid);

super();
}






Lastly Create a new method in Form Method :

public void refresh(recId _lastRecid)
{
lastRecordRecId = _lastRecid;

element.loadTree();

SampleTree2.select(lastRecordIdx);
}






Now : In order to Sort the Tree Based on the following upper grid then :

1. Create a new Table MainTableMed
Add few fields
In Table Medecines :
Add relation  new relation give table : MainTableMed;
Right click foreign key PrimaryKey
A field will be generated .

2. In the Form Drag Table : MainTableMed into Datasource ..
InDatasourceTable: Medecines Properities
Join Source = MainTableMed
Link Type = active

In Form DataSource Table MainTableMed  override methods :

public void create(boolean _append = false)
{
super(_append);

SampleTree2.deleteAll();
AddBelow.enabled(false);
}

public void delete()
{
super();

if(MainTableMed.RecId == 0)
{
SampleTree2.deleteAll();
AddBelow.enabled(false);
}

}
public int active()
{
int ret;

ret = super();

if(ret)
{
if(MainTableMed.RecId)
{
element.loadTree();
AddBelow.enabled(true);
}
else
{
SampleTree2.deleteAll();
AddBelow.enabled(false);
}
}

return ret;
}
2

To work this Moddify the form method : CreateChildNodes


Now design the Form  take Group , add action panel contains New and Delete record Methods and a Grid : pro -datasource : MainTableMed , add few fields
1