• Skip to main content
  • Skip to primary sidebar
  • Home
  • About
  • Recommended Readings
    • 2022 Book Reading
    • 2023 Recommended Readings
    • Book Reading 2024
    • Book Reading 2025
  • Supply Chain Management Guide
  • PKM
  • Microsoft Excel

Ali Raza Zaidi

A practitioner’s musings on Dynamics 365 Finance and Operations

Custom AIF Service in Dynamics Ax 2012 R3 from Scratch.

July 29, 2015 by alirazazaidi

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

2015-07-29_15-49-25

 

Filed Under: Dynamics AX 2012, Tips and tricks Tagged With: AIF, Dynamics Ax 2012

Primary Sidebar

About

I am Dynamics AX/365 Finance and Operations consultant with years of implementation experience. I has helped several businesses implement and succeed with Dynamics AX/365 Finance and Operations. The goal of this website is to share insights, tips, and tricks to help end users and IT professionals.

Legal

Content published on this website are opinions, insights, tips, and tricks we have gained from years of Dynamics consulting and may not represent the opinions or views of any current or past employer. Any changes to an ERP system should be thoroughly tested before implementation.

Categories

  • Accounts Payable (2)
  • Advance Warehouse (2)
  • Asset Management (3)
  • Azure Functions (1)
  • Books (6)
  • Certification Guide (3)
  • Customization Tips for D365 for Finance and Operations (62)
  • D365OF (59)
  • Data Management (1)
  • database restore (1)
  • Dynamics 365 (58)
  • Dynamics 365 for finance and operations (135)
  • Dynamics 365 for Operations (165)
  • Dynamics AX (AX 7) (134)
  • Dynamics AX 2012 (274)
  • Dynamics Ax 2012 Forms (13)
  • Dynamics Ax 2012 functional side (16)
  • Dynamics Ax 2012 Reporting SSRS Reports. (31)
  • Dynamics Ax 2012 Technical Side (52)
  • Dynamics Ax 7 (65)
  • Exam MB-330: Microsoft Dynamics 365 Supply Chain Management (7)
  • Excel Addin (1)
  • Favorites (12)
  • Financial Modules (6)
  • Functional (8)
  • Implementations (1)
  • Lifecycle Services (1)
  • Logseq (4)
  • Management Reporter (1)
  • Microsoft Excel (4)
  • MS Dynamics Ax 7 (64)
  • MVP summit (1)
  • MVP summit 2016 (1)
  • New Dynamics Ax (19)
  • Non Defined (9)
  • Note taking Apps (2)
  • Obsidian (3)
  • Personal Knowledge Management (2)
  • PKM (13)
  • Power Platform (6)
  • Procurement (5)
  • procurement and sourcing (5)
  • Product Information Management (4)
  • Product Management (6)
  • Production Control D365 for Finance and Operations (10)
  • Sale Order Process (10)
  • Sale Order Processing (9)
  • Sales and Distribution (5)
  • Soft Skill (1)
  • Supply Chain Management D365 F&O (3)
  • Tips and tricks (278)
  • Uncategorized (165)
  • Upgrade (1)
  • Web Cast (7)
  • White papers (4)
  • X++ (7)

Copyright © 2025 · Magazine Pro On Genesis Framework · WordPress · Log in