In this example, we explore a complex custom services. Which perform a insert , update, delete and return all the records form table. If you interested in basics of custom services you have to visit my pervious post on this topic
http://tech.alirazazaidi.com/explore-the-custom-service-in-dynamics-ax-2012/.
For this example, I create a very simple custom table . This table contains following fields.
FieldName | dataType |
Roll Number | string |
FistName | string |
LastName | string |
Address | string |
DateOfBirth | date. |
For Data contract we have to create a new class. On class declaration method you have to put the attribute with name like as [DataContractAttribute]
[DataContractAttribute]
class StudnetDC
{
str rollNumber;
str address;
date dateOfBirth;
str firstName;
str lastName;
}
For each field I have to declare a separate param method with are equivalent to C# or Vb.net property method like as We have to use again a attribute on each method [ DataMemberAttribute(‘Parameter Name’)]
[ DataMemberAttribute('Address')]
public str paramAddress(str _address=address)
{
address=_address;
return address;
}
[ DataMemberAttribute('Date of Birth')]
public date paramDateOfBirth(date _dateOfBirth=dateOfBirth)
{
dateOfBirth=_dateOfBirth;
return dateOfBirth;
}
[ DataMemberAttribute('First Name')]
public str paramFirstName(str _firstName=firstName)
{
firstName= _firstName;
return firstName;
}
[ DataMemberAttribute('Last Name')]
public str paramLastName(str _lastName=lastName )
{
lastName= _lastName;
return lastName;
}
[ DataMemberAttribute('RollNumber')]
public str paramRollNumber(str _rollNumber= rollNumber)
{
rollNumber= _rollNumber;
return _rollNumber;
}
Then I create a service class. On each method I have to use attributes for passing parameter to method in my case, insert method will take parameter of contract class as follow.
[SysEntryPointAttribute(true),
AifCollectionTypeAttribute('Studentobj', Types::Class)]
public void InsertStudent(StudnetDC Studentobj)
{
Student studentbuf;
studentbuf.RollNumber = Studentobj.paramRollNumber();
studentbuf.FirstName = Studentobj.paramFirstName();
studentbuf.LastName = Studentobj.paramLastName();
studentbuf.DateOfBirth = Studentobj.paramDateOfBirth();
studentbuf.Address = Studentobj.paramAddress();
studentbuf.insert();
}
[SysEntryPointAttribute(true),
AifCollectionTypeAttribute('Studentobj', Types::Class)]
public void UpdateStudent(StudnetDC Studentobj)
{
Student studentbuf;
while select forUpdate studentbuf
where studentbuf.RollNumber== Studentobj.paramRollNumber()
{
studentbuf.RollNumber = Studentobj.paramRollNumber();
studentbuf.FirstName = Studentobj.paramFirstName();
studentbuf.LastName = Studentobj.paramLastName();
studentbuf.DateOfBirth = Studentobj.paramDateOfBirth();
studentbuf.Address = Studentobj.paramAddress();
}
}
[SysEntryPointAttribute(true),
AifCollectionTypeAttribute('Studentobj', Types::Class)]
public void DeleteStudent(StudnetDC Studentobj)
{
Student studentbuf;
while select forUpdate studentbuf
where studentbuf.RollNumber== Studentobj.paramRollNumber()
{
studentbuf.delete();
}
}
To return a list form Class method we have to use following attribute on method.
[SysEntryPointAttribute(true),
AifCollectionTypeAttribute(‘return’, Types::Class, classStr(MyParam))]
[SysEntryPointAttribute(true),
AifCollectionTypeAttribute('return', Types::Class, classStr(StudnetDC))]
public list SelectStudent()
{
StudnetDC studentobj;
List _StudentList = new List(Types::Class);
Student studentbuf;
while select * from studentbuf
{
studentobj = new StudnetDC();
studentobj.paramFirstName( studentbuf.FirstName);
studentobj.paramLastName(studentbuf.LastName);
studentobj.paramRollNumber(studentbuf.RollNumber);
studentobj.paramAddress(studentbuf.Address);
studentobj.paramDateOfBirth(studentbuf.DateOfBirth);
_StudentList.addEnd(studentobj);
}
return _StudentList;
}
Now compile the both classes. Service node.