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

No comments:

Post a Comment

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)