Showing posts with label AIF. Show all posts
Showing posts with label AIF. Show all posts

Friday 12 February 2016

Custom AIF Service in Dynamics Ax 2012 R3 from Scratch.

Today I decide to experiment with custom service. So I decided to create custom table with Name MyColorTable.

This table contains only one field Name. and made it unique with no allow duplicate on Index.
MyColorTable

If we consider custom service in Dynamics Ax 2012, it contains following objects

  • X++ Data contract class
  • X++ Service contract class
  • Service Node
  • Service Group node.

Data Contract class
So first we create Data contract class
Suppose Our data contract class name is  ColorDc


[DataContractAttribute]

class ColorDC

{

Name ColorName;

}
Now add a new method with Name ParmColorName and set its as


[ DataMemberAttribute('ColorName')]

public Name parmColorName(Name _ColorName=ColorName)

{

ColorName=_ColorName;

return ColorName;

}


Service contract class:
Now we write a Service class which contains Three method Purpose of these method to explore the require attribute for getting parameter in service method and return list from service method.
InsertColor. (This method contains single color)
InsertColorList (This method take list of colorDC as parameter).
GetColorList ( This method return list of colorDC).

[SysEntryPointAttribute(true),

AifCollectionTypeAttribute('Colorobj', Types::Class)]

public void InsertColor(ColorDC Colorobj)

{

MyColorTable _Color;

Name _Name;

 

_Name = Colorobj.parmColorName();

 

select * from _Color where _Color.Name== _Name;

 

if (_Color==null)

{

try

{

ttsBegin;

_Color.Name = _Name;

_Color.insert();

 

ttsCommit;

}

catch

{

ttsAbort;

}

 

}

}

In above method we add some attribute which allow this method to act as web method and attribute help us define the expected parameter for this method call.

Now we call create another method, here attributes also described method as webmethod and also method expect what type of parameter.
[DataMemberAttribute("InsertColorList"),

AifCollectionTypeAttribute("ColorList",Types::Class, classStr(ColorDC))

]

public Void InsertColorList(List ColorList )

{

ListIterator  iterator;

ListEnumerator  enumerator;

ListIterator   literator;

ColorDC       _color;

 

 

enumerator = ColorList.getEnumerator();

 

while(enumerator.moveNext())

{

_color= enumerator.current();

if (_color !=null)

{

this.InsertColor(_color);

}

}

 

}



Now get method which return all color in Ax.
[SysEntryPointAttribute(true),

AifCollectionTypeAttribute('return', Types::Class, classStr(ColorDC))]

public list GetColorList()

{

ColorDC Colorobj;

List _ColorList = new List(Types::Class);

MyColorTable  Colorbuf;

while select * from Colorbuf

{

 

Colorobj = new ColorDC();

Colorobj.parmColorName(Colorbuf.Name);

_ColorList.addEnd(Colorobj);

}

return _ColorList;

}

Now compile it now
Service Object:
create a service object and set Service contract class there
New Service
Expand ColorService object and Right click on Operations and click on add method, this way we can restrict which method need to expose in service or which method need not.
AddOperation
2015-07-28_6-40-49
Click on all check boxes and enabled them
Service Group:
Create a new service group and drag and drop service object under it.
2015-07-28_6-41-15



Right click on ColorServiceGroup and deploy the service
Deploy

Wait and let It deploy Info box shows the message that service is deployed successfully
2015-07-28_12-38-21


No service is successfully deployed, Now open Ax client , from Administration section. Setup=> AIF => Inbond port.
AIF
Copy WSDL URI.

Open Visual studio and create a new Console application for testing the code.
Right click on references and ad service reference
ssss

2015-07-28_13-01-06




First we call insert the records in service.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

ColorServiceGroup.ColorDC cd1 = new ColorServiceGroup.ColorDC();

cd1.ColorName = "Red";

ColorServiceGroup.ColorDC cd2 = new ColorServiceGroup.ColorDC();

cd2.ColorName = "Blue";

ColorServiceGroup.ColorDC cd3 = new ColorServiceGroup.ColorDC();

cd3.ColorName = "Yellow";

 

ColorServiceGroup.ColorDC[] DcList = new ColorServiceGroup.ColorDC[] { cd1, cd2, cd3 };

ColorServiceGroup.ColorServiceClient _Client = new ColorServiceGroup.ColorServiceClient();

 

ColorServiceGroup.CallContext _CallContext = new ColorServiceGroup.CallContext();

_CallContext.Company = "USMF";

_Client.InsertColorList(_CallContext, DcList);

 

 

 

 

 

}

}

}

When I run the above code in Visual studio  records are successfully inserted in original table.
TableBrowser


Now we call list of colors exist in Dynamics Ax 2012.
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

 

 

 

ColorServiceGroup.ColorServiceClient _Client = new ColorServiceGroup.ColorServiceClient();

 

ColorServiceGroup.CallContext _CallContext = new ColorServiceGroup.CallContext();

_CallContext.Company = "USMF";

ColorServiceGroup.ColorDC[] _DcList=   _Client.GetColorList(_CallContext);

//    ColorServiceGroup.ColorDC _Dc ;

foreach (ColorServiceGroup.ColorDC _Dc in _DcList)

{

Console.WriteLine(_Dc.ColorName);

 

}

 

Console.ReadKey();

 

 

 

}

}

}



When I run the above code, all records exists in Ax display on c# console

Creating and consuming Custom Document Service in Dynamics Ax 2012 R3

For one of Presentation on AIF, I build a custom Document Service based on custom table. To avoid any validation constraints I made a very simple custom table “StudentInfoTable”.
FieldNamedataType
Roll Numberstring
FistNamestring
LastNamestring
Addressstring
DateOfBirthdate.

Create a Unique Index on FirstName field, so Read and find method of custom Document service works fine.


In AOT under Query Node with Name AxdStudentInfo, create a new DataSource and add StudentInfoTable. Right click on field and set Dynamic Property to Yes

Query

Now right click on “StudentInfoTable” Node and set its Update properties to true, so Update methods will created against this query object

UpdateQuery

Now in Development environment from top menu click on AIF frame and then select on Create Document Service and click on it. For this result in running a Wizard
Menu


Select following options from Wizard, select the query which we create above mentioned drop down. Name the Document Name

9-14-2014 10-04-05 AM
Press Next button, from the next button check all the checkbox as per following screen shot.
9-14-2014 10-05-12 AM
Press on Next Button
9-14-2014 10-08-52 AM
Press on Generate button.
9-14-2014 10-12-39 AM
Press On Finished button.
There is possibility of errors in generated code, You can find complete code generated with wizard in private project in Dynamics Ax.
Private Project for Document Services
Right click on AxdStudentInfo and compile again.
You have delete two methods  cacheObject and cacheRecordRecord in AxStudentInfoTable and compile the whole project.

Now right click on StudentInfoService and set namespace
Service reference
Now create a right on Service group Node and create a new service group.

Service Group
Drag and drop service node under Student InfoService group. So  Student Service will be available for in side service group.

Now generate Inc CIL or right click on StudentInfoServicegroup and click deploy

Deploy Service Group






After successful deployment you may find following info box

Service InfoBox




Now open a new work space and go at System Administration module and open in bound port under Application integration framework
In Bound Port



And Open Application Inbound framework

Address
Copy WSDL URL In my case it is


now open Visual studio Project and create a new Console project I am more comfortable with C#.
Console



From Solution explore right click and add Service reference
ServiceReference2


Now we can use following code to create, Read, Update and delete methods

Create method


  StudentInfoRef.AxdEntity_StudentInfoTable _StudentInfo = new StudentInfoRef.AxdEntity_StudentInfoTable();

StudentInfoRef.AxdStudentInfo _Student = new StudentInfoRef.AxdStudentInfo();

_Student.StudentInfoTable = new StudentInfoRef.AxdEntity_StudentInfoTable[1] { _StudentInfo };

 

_StudentInfo.FirstName = "Ali";

_StudentInfo.LastName = " Raza";

_StudentInfo.Address = " Lahore";

_StudentInfo.DateOfBirth = new DateTime(1979, 4, 6);

StudentInfoRef.StudentInfoServiceClient _Client = new StudentInfoRef.StudentInfoServiceClient();

StudentInfoRef.CallContext _callContext = new StudentInfoRef.CallContext();

_callContext.Company = "USSI";

StudentInfoRef.EntityKey[] entityKeys = _Client.create(_callContext, _Student);

You can see create function works fine.
TableExploreCreate
In above mentioned code, Create method take two parameter, first is Call context, In Call context variable we can define legal entity where data will be inserted.


Read Method

Read method use EntityKey for searching criteria for example we want to search Student with First Name Ali following code will works for Us.
Read method works on keyField value. It is recommend that you will search records on the bases of Unique indexed based field.



StudentInfoRef.KeyField keyField = new StudentInfoRef.KeyField() { Field = "FirstName", Value = "Ali2" };

 

// Create an entity key instance and put in the key field data

StudentInfoRef.EntityKey entityKey = new   StudentInfoRef.EntityKey();

 

entityKey.KeyData   = new   StudentInfoRef.KeyField[1] { keyField };

// Create an array of entity keys and put in the previously

 

StudentInfoRef.EntityKey[] entityKeys = new StudentInfoRef.EntityKey[1] { entityKey };

 

 

StudentInfoRef.StudentInfoServiceClient _Client = new StudentInfoRef.StudentInfoServiceClient();

StudentInfoRef.CallContext _callContext = new StudentInfoRef.CallContext();

_callContext.Company = "USSI";

 

StudentInfoRef.AxdStudentInfo _StudentInfo = _Client.read(_callContext, entityKeys);

 

 

StudentInfoRef.AxdEntity_StudentInfoTable _StudentInfoTable = _StudentInfo.StudentInfoTable[0];

 

Console.WriteLine("First Name :" + _StudentInfoTable.FirstName);

Console.WriteLine("Last Name :" + _StudentInfoTable.LastName);

Console.WriteLine("Address :" + _StudentInfoTable.Address);

 

Console.ReadLine();






Update Method
Update in Document is two step process, first step to read the record based on field and second step to update the record which is return as result.


For example I want to update Address and Last Name of student. Following code works for me.



StudentInfoRef.KeyField keyField = new StudentInfoRef.KeyField() { Field = "FirstName", Value = "Ali2" };

 

// Create an entity key instance and put in the key field data

StudentInfoRef.EntityKey entityKey = new StudentInfoRef.EntityKey();

 

entityKey.KeyData = new StudentInfoRef.KeyField[1] { keyField };

// Create an array of entity keys and put in the previously

 

StudentInfoRef.EntityKey[] entityKeys = new StudentInfoRef.EntityKey[1] { entityKey };

 

 

StudentInfoRef.StudentInfoServiceClient _Client = new StudentInfoRef.StudentInfoServiceClient();

StudentInfoRef.CallContext _callContext = new StudentInfoRef.CallContext();

_callContext.Company = "USSI";

 

StudentInfoRef.AxdStudentInfo _StudentInfo = _Client.read(_callContext, entityKeys);

 

 

StudentInfoRef.AxdEntity_StudentInfoTable _StudentInfoTable = _StudentInfo.StudentInfoTable.First();

 

_StudentInfoTable.Address = "Lahore";

_StudentInfoTable.LastName = "Zaidi";

_StudentInfoTable.action = StudentInfoRef.AxdEnum_AxdEntityAction.update;

_StudentInfoTable.actionSpecified = true;

_Client.update(_callContext, entityKeys, _StudentInfo);





Delete Method

Delete Method works for same way, Send entity keys and call for Delete method.

StudentInfoRef.KeyField keyField = new StudentInfoRef.KeyField() { Field = "FirstName", Value = "Ali2" };

 

// Create an entity key instance and put in the key field data

StudentInfoRef.EntityKey entityKey = new StudentInfoRef.EntityKey();

 

entityKey.KeyData = new StudentInfoRef.KeyField[1] { keyField };

// Create an array of entity keys and put in the previously

 

StudentInfoRef.EntityKey[] entityKeys = new StudentInfoRef.EntityKey[1] { entityKey };

 

 

StudentInfoRef.StudentInfoServiceClient _Client = new StudentInfoRef.StudentInfoServiceClient();

StudentInfoRef.CallContext _callContext = new StudentInfoRef.CallContext();

_callContext.Company = "USSI";

_Client.delete(_callContext, entityKeys);

Exploring The SalesSalesOrderservice AIF Document Service In Dynamics Ax 2012 R3

For my session for Integration with Dynamics Ax 2012. I have to consume SalesSalesOrderSerivce. In this article I am configure and consume Sales Order service and share c# code for calling a Sales service and consume.

Enable the methods and configure the inbound port. For this you have to go at following link and open AIf Inbound
Aif Link

Right click on AIF menu and create a New
set the Name there “ for example I create the port with Name “MySalesOrderService”.
10-10-2014 7-58-10 PM
Click on Service Operations add following one
Selection
Click on arrow button and add these method to service
Selected
Now click on Close button to close it.
Now click to check box to enable data policy button document. Click on it.
enable the Data policy

New screen open for all possible fields, Click on Element to sort the list, Enable following fields which are minimum required for Sales Order creation.
Mendatory Selection



Now save it. From port screen enable logging and Click on activate, Wait it will take time to deploy the service on port.
Activate
Info log appear to tell that port is successfully active.
InfoLog

Now copy the WSDL url
Wsdl


Create a new Console application in Visual studio, I am more familiar with C# so I created a console application in C#.
Add new Service reference and set its name “SalesOrderService” and get methods from WSDL.
SalesOrder Service Reference

Now Click on Ok button.


Calling the Create Method.

 

var line = new SalesOrderService.AxdEntity_SalesLine()
{
ItemId = “D0001”,
SalesQty = 42,
SalesUnit = “ea”
};

var order = new SalesOrderService.AxdEntity_SalesTable()
{
CustAccount = “US-003”,
PurchOrderFormNum = “ffg”,
ReceiptDateRequested = DateTime.Now.Date,
SalesLine = new SalesOrderService.AxdEntity_SalesLine[] { line }
};

//   SalesOrderService. _salesOrder= new SalesOrderService.AxdSalesOrder();


var orderList = new SalesOrderService.AxdEntity_SalesTable[] { order };
var callContext = new SalesOrderService.CallContext() { Company = “USMF” };
SalesOrderService.SalesOrderServiceClient client = new SalesOrderService.SalesOrderServiceClient();


//   _salesOrder.SalesTable=orderList;
try
{
client.create(callContext, orderList);


//  client.create(callContext, orderList);
client.Close();
}
catch
{
client.Abort();
throw;
}

Read Method Code

try
{
SalesOrderService.KeyField keyField = new SalesOrderService.KeyField() { Field = “SalesId”, Value = “000767” };



// Create an entity key instance and put in the key field data

SalesOrderService.EntityKey entityKey = new SalesOrderService.EntityKey();



entityKey.KeyData = new SalesOrderService.KeyField[1] { keyField };

// Create an array of entity keys and put in the previously



SalesOrderService.EntityKey[] entityKeys = new SalesOrderService.EntityKey[1] { entityKey };





SalesOrderService.SalesOrderServiceClient _Client = new SalesOrderService.SalesOrderServiceClient();

SalesOrderService.CallContext _callContext = new SalesOrderService.CallContext();

_callContext.Company = “USMF”;


SalesOrderService.AxdEntity_SalesTable[] _SalesOrder= _Client.read(_callContext, entityKeys);


SalesOrderService.AxdEntity_SalesTable _SalesTable = _SalesOrder[0];
SalesOrderService.AxdEntity_SalesLine _SalesLineTable = _SalesTable.SalesLine[0];
Console.WriteLine(“Account Num : ” + _SalesTable.CustAccount.ToString());
Console.WriteLine(“Order date  : ” + _SalesTable.ReceiptDateRequested.ToString());
Console.WriteLine(“ItemId  : ” + _SalesLineTable.ItemId.ToString());
Console.WriteLine(“QTy  : ” + _SalesLineTable.SalesQty.ToString());
Console.WriteLine(“Sales Unit  : ” + _SalesLineTable.SalesUnit.ToString());
_Client.Close();
Console.ReadKey();
}
catch
{
//     client.Abort();
throw;
}


Update Method Call code

 

SalesOrderService.KeyField keyField = new SalesOrderService.KeyField() { Field = "SalesId", Value = "000756" };



// Create an entity key instance and put in the key field data

SalesOrderService.EntityKey entityKey = new SalesOrderService.EntityKey();



entityKey.KeyData = new SalesOrderService.KeyField[1] { keyField };

// Create an array of entity keys and put in the previously



SalesOrderService.EntityKey[] entityKeys = new SalesOrderService.EntityKey[1] { entityKey };





SalesOrderService.SalesOrderServiceClient _Client = new SalesOrderService.SalesOrderServiceClient();

SalesOrderService.CallContext _callContext = new SalesOrderService.CallContext();

_callContext.Company = “USMF”;



SalesOrderService.AxdEntity_SalesTable[] _SalesOrderList = _Client.read(_callContext, entityKeys);


SalesOrderService.AxdEntity_SalesTable _SalesOrderTable = _SalesOrderList.First();
SalesOrderService.AxdEntity_SalesLine SalesLine = _SalesOrderTable.SalesLine.First();
decimal salesQty = 50;
SalesLine.SalesQty = salesQty;






_Client.update(_callContext, entityKeys, _SalesOrderList);


Delete method code

 

SalesOrderService.KeyField keyField = new SalesOrderService.KeyField() { Field = "SalesId", Value = "000757" };



// Create an entity key instance and put in the key field data

SalesOrderService.EntityKey entityKey = new SalesOrderService.EntityKey();



entityKey.KeyData = new SalesOrderService.KeyField[1] { keyField };

// Create an array of entity keys and put in the previously



SalesOrderService.EntityKey[] entityKeys = new SalesOrderService.EntityKey[1] { entityKey };





SalesOrderService.SalesOrderServiceClient _Client = new SalesOrderService.SalesOrderServiceClient();

SalesOrderService.CallContext _callContext = new SalesOrderService.CallContext();

_callContext.Company = “USMF”;

_Client.delete(_callContext, entityKeys);


Trouble shooting AIF service.
There are many possible failure of sales service call, You can trouble shoot it from  System Administration section as
Exceptions
During writing code and I got many failures, All exceptions recorded here.

ExceptionsForm

Labels

#veryusefulcode (1) AIF (8) AOT Maps (1) Args (1) Ax 2009 Reports (2) AX 2012 navigation (1) Ax 2012 Interview Questions (1) AX 7 (2) AX Architecture (1) Ax Backup (1) AX Workflow (2) AX2012 (1) AX2012 R2 (1) Ax2012R3 (1) AX2012R3 Dynamics Connector Step by Step Installation and Configuration (1) AX2012R3 EP Step by Step Installation and Configuration EP R3 (1) AX2012R3 HelpServer Step by Step Installation and Configuration (1) AX2012R3 Rapid Start Connector Step by Step Installation and Configuration (1) AX2012R3 Report Server and Analysis Server Step by Step Installation and Configuration (1) AX7 (1) Best practices (1) Blocking user to enter (1) Collection Classes (1) Container (1) D365FO (3) Data Migration Frame Work ax 2012R3 (1) Deleting duplicate Record from Table – Ax2012 (1) Delivery due date notification workflow in Ax 2012 (1) Development Steps EP (1) Dimensions (1) DIXF (1) DMF in Ax 2012 R3 (1) Dynamics Ax 2012 Interview Questions (1) DYNAMICS AX 2012 INTERVIEW QUESTIONS PART 2 (1) DYNAMICS AX 7 (1) EDT relation Migration Tool (1) EP AX 2012 (1) Ep Lookup (1) Error (1) Event Handler (1) F5 (1) File Handling (4) Filter on AX2012 Listpage (1) filtering (2) financial dimensions in AX 2012 (3) form (1) images (1) Installation and Configration (4) Installation and Configuration (11) Installation of Management Reporter 2012 for AX 2012 (1) Interaction class in ax 2012 (1) Interview Question (1) Interview Questions For Ax 2012 (1) Invent DIm (1) Jobs (2) license (1) List page and form menuitem enable code (1) Methods (1) microsoft Dynamics AX 365FO (1) Number Sequence Generation – AX 2012 (5) Number Sequence2012 (1) OLTP-OLAP (1) Passing Args (1) Passing form caller and menu item caller in ax 2012 (1) Passing Multiple Records Args (1) Posting in Ax 2012 (1) POSTING PURCHASE ORDER (1) Query (1) Query Filter Form (2) Query Ranges in X++ (1) Question and Answer (1) Report (1) Reports Controller class (1) RLS in ax 2009 (1) SALES ORDER IMPORT/EXPORT FRAMEWORK BY DMF (1) Security (1) security roles (1) Security Sysqueryrangeutil (1) Sharepoint 2016 (1) SQL SERVER (1) SSRS (2) SSRS Reports Controller class (2) Table collections & Virtual company (1) Time (1) TIPS AND TRICKS (1) Web service AIF (3) Web Services on IIS (AIF) Step by Step Installation and Configuration (1) workflow ax2012 (1) Workflow installation (1) Workflow Method (3) X++ (1)