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

Tuesday, 8 November 2016

Query Service AIF AX2012


 AIF query service can be used to fetch ad hoc data from AX2012 without creating a new service.
Its a common service which can be used to retrieve the data from AX database. There are three types of queries that you can run through a query service but I am discussing only the first two options here:


In the below code I will demonstrate the use of query service for fetching all the records from the SalesTable through the user defined query. In order to run this snippet, please make sure that AIF services are deployed and you are able to access the query service.  URI for the query service WSDL is:

http://<AOS_HOST>:8101/DynamicsAx/Services/QueryService


C# code for fetching the records

QueryServiceClient client = new QueryServiceClient();
QueryMetadata queryMetaData = new QueryMetadata();
queryMetaData.DataSources = new QueryDataSourceMetadata[] {
new QueryDataSourceMetadata() { DynamicFieldList=true, Table=”StudentTable”,  Name=”StudentTable”,  Enabled=true}
};

Paging paging = null;

DataSet ds = client.ExecuteQuery(queryMetaData, ref paging);

foreach (DataRow dr in ds.Tables[0].Rows)
{
Console.WriteLine(dr[“PersonnelNumber”].ToString());
}

Console.ReadLine();


Sunday, 6 November 2016

How to debug AIF AX2012

In this post I will demonstrate the debugging of AX2012 AIF services, which makes the lives of developers easy :).

We can simply debug the AIF service, triggered from C# code which was written to consume the AIF services.

I have created a document service and written some custom code in service classes. Now I want to debug and see the impact.

Follow below steps:

Prerequisite: Your environment should be full CIL compiled.

Step 1: Create a console application in visual studio C#, to consume the AIF service. This was demonstrated in my earlier post. For more details visit below link.


Step 2: Open a new visual studio application. Right click visual studio and Run as admin.

Step 3: Open Application explorer in visual studio and find your service class.




Step 4: Now go to Debug option and attach the process to server.
 Step 5: Make sure that the symbol loading is completed and the debugger symbol is completely turned Red.


Step 5: Now the service is ready to be debugged. Switch to the first visual studio application where you have written the C# code as per the Step 1. Make sure the two VS applications are running simultaneously.

Step 6:  Run the test code, the debugger will hit the line of code on another VS application where you have attached the server processes.


Note: Debugging is possible in VS only, this will not open AX debugger, also the client side code will not be executed in debugger.

Happy daxing :)


Monday, 24 October 2016

AIF AX2012 good to know concepts.

In this post I am trying to consolidate the concepts which are good to know and can help in making the right decision for deciding the approach of service creation and customization of existing ones.

1. Axd class methods.

  • getSurrogateForeignKeyValue() : This method is used for getting the surrogate key values for related table fields. Below is the example code snippet.

public RecId getSurrogateForeignKeyValue(AxdBaseProperty _axdBaseProperty, Struct _sfkReplacementValue)
{
RecId ret;
ret = super(_axdBaseProperty, _sfkReplacementValue);
switch (_axdBaseProperty.parmImplementingTableId())
{
case tableNum(DirpartyTable) :
switch (_axdBaseProperty.parmImplementingFieldId())
{
case fieldNum(DirpartyTable, Name):
ret = DirpartyTable::find(_sfkReplacementValue.value(fieldStr(DirPartyTable,Name))).RecId;
spTypeId = ret;
break;
}
}
return ret;
}
.
This method will be overridden in scenarios where the document service has a table which has multiple foreign key relations. As we know that we cannot pass the recId values directly, but certainly we may need to populate the foreign key values. Hence we can override this method and finds the related recId of the value passed for certain field.
sfkReplacementValue.value denotes the replacement key value mentioned on the related table.
  • UpdateNow(): This method is the last point  of execution in document service and is called during the create and update operations. This method is basically used when we have to perform any business logic on the created or updated entity. for ex: If I am using a document service to create a sales order in AX and my requirement is to confirm the same sales order while creation only. In this scenario I can write my business logic for sales order confirmation in updatenow() method.
  • expandSurrogateForeignKeys(): This method return true by default, which means that if there is any foreign key exist on the table than the framework can automatically convert the input for that field into replacement key. For ex: If I am creating a customer and my customer has a foreign key relation with custGroup. Now if this method is set to return true than it will take custGroupId as input and at backend find the surrogate key and associate it with RecId of custgroup record and populate the recId on custTable.
  • PrepareForSaveExtended(): This method is the entry point and is called in sequence with the data sources mentioned on the query in AX2012 R3. This method is very important with the customization point of view. We can include the value default logics, validations and custom field value handling in this method.
2. Terminology:  While going through the Axd classes, you can come across lot of terms which  are new, explaining few terms below which will help in understanding the code written in axdClasses.

  • AxdRecordProcessingContext: Record processing context is defined based on the data source levels defined on the document service query. Based this context Axd class proceed to create the records in child and parent tables. This term is basically found in prepareForSaveExtended method(). This method has been called multiple times when the record are processed in respective table. we can also define our custom code based on these contexts.
  • AxdRecordAction: This is another base enum which can be used for executing the particular logic based on the record action type. Record action type is defined by the operation which is triggered, basically there are create, read, update and delete operations. For ex. If wee need to perform some logic related to calculation of discount on a sales order if there is change in customer account. So, in order to update the customer account we will trigger the update operation. now we can place our code in prepareForSaveExtended() method below the relared processing context in following manner.
  if  (_axBcStack.top().recordAction() == AxdRecordAction::Update)
    {
      Perform your business logic and set parm value accordingly.

    }

In my next post I will demonstrate the difference between service operations and their relevance.





Sunday, 23 October 2016

C# code to update the Logistics postal address through AIF.

In my second post of this series, I demonstrated the creation of document service for a new entity. The post also include the solution on how to add the related postal address using AIF document service.

In order to achieve this we have added  calls to some standard API's in prepareForSaveDetailsExtended() method of our service class.

In this post I am sharing the code which can be used to test the update operation on student entity with address.

In order to update the surrogate key indexed entity, we need to use the find operation for finding the entity key, and then based on the found entity key we can do the read operation which will return the Axd class instance which can be updated as per the requirement. Below is the code which will demonstrate the same requirement.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestStudentService.ServiceReference1;//Added namespace o access service in below application

namespace TestStudentService
{
class Program
{
static void Main(string[] args)
{
int rollNumber = 4;

AxdStudentTable studentAxd = null;
CallContext context = new
CallContext();
context.Company = "EAM";

StudentTableServiceClient proxy = new StudentTableServiceClient();
try
{
studentAxd = proxy.read(context, Program.readCritera(rollNumber));
//Console.WriteLine("Read worked");
Program.updateCustomer(studentAxd, rollNumber);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
private static EntityKey[] readCritera(int rollNumber)
{
StudentTableServiceClient client = new StudentTableServiceClient();
AxdStudentTable localAxd = new AxdStudentTable();

CriteriaElement[] criteriaElements = new CriteriaElement[1];
criteriaElements[0] = new CriteriaElement();
criteriaElements[0].DataSourceName = "StudentTable";
criteriaElements[0].FieldName = "RollNumber";
criteriaElements[0].Value1 = rollNumber.ToString();
QueryCriteria queryCriteria = new QueryCriteria();
queryCriteria.CriteriaElement = criteriaElements;
CallContext readContext = new
CallContext();
readContext.Company = "EAM";
//Using find operation
localAxd = client.find(readContext, queryCriteria);
//Using read operation
EntityKey[] entityKeyList = new EntityKey[1];
EntityKey key = new EntityKey();
KeyField[] keyFields = new KeyField[1];
KeyField keyField = new KeyField();
keyField.Field = "RecID";


keyField.Value = localAxd.StudentTable[0].RecId.ToString();
keyFields[0] = keyField;
key.KeyData = keyFields;
entityKeyList[0] = key;
return entityKeyList;
}
private static void updateCustomer(AxdStudentTable studentTableAxd, int rollNumber)
{
try
{
StudentTableServiceClient updateProxy = new StudentTableServiceClient();
CallContext updateContext = new CallContext();
updateContext.Company = "EAM";

AxdEntity_StudentTable OldStudentEntity = studentTableAxd.StudentTable[0];
AxdEntity_StudentTable newStudentEntity = new AxdEntity_StudentTable();
AxdStudentTable newStudentAxd = new AxdStudentTable();

newStudentAxd.ValidTimeStateType = studentTableAxd.ValidTimeStateType;
newStudentAxd.ValidTimeStateTypeSpecified = true;

newStudentAxd.ValidAsOfDateTime = studentTableAxd.ValidAsOfDateTime;
newStudentAxd.ValidFromDateTime = studentTableAxd.ValidFromDateTime;
newStudentAxd.ValidToDateTime = studentTableAxd.ValidToDateTime;
newStudentEntity._DocumentHash = OldStudentEntity._DocumentHash;
newStudentEntity.RecId = OldStudentEntity.RecId;
newStudentEntity.RecVersion = OldStudentEntity.RecVersion;
newStudentEntity.action = AxdEnum_AxdEntityAction.update;
newStudentEntity.actionSpecified = true;
newStudentEntity.Name = "Suresk kumar";
#region Update the date effective entities
newStudentEntity.DirPartyTable = OldStudentEntity.DirPartyTable;
newStudentEntity.DirPartyTable[0].action = AxdEnum_AxdEntityAction.update;
newStudentEntity.DirPartyTable[0].actionSpecified = true;
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].StreetNumber = "10";
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].Roles = "Home";
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].Street = "street updated 1223";
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].action = AxdEnum_AxdEntityAction.update;
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].actionSpecified = true;
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].updateMode = AxdEnum_ValidTimeStateUpdate.Correction;
newStudentEntity.DirPartyTable[0].DirPartyPostalAddressView[0].updateModeSpecified = true;
newStudentEntity.DirPartyTable[0].DirPartyContactInfoView[0].Locator = "+61 (0) 12 345 678";
newStudentEntity.DirPartyTable[0].DirPartyContactInfoView[0].Roles = "Home";
newStudentEntity.DirPartyTable[0].DirPartyContactInfoView[0].Type = AxdEnum_LogisticsElectronicAddressMethodType.Phone;
newStudentEntity.DirPartyTable[0].DirPartyContactInfoView[0].action = AxdEnum_AxdEntityAction.update;
newStudentEntity.DirPartyTable[0].DirPartyContactInfoView[0].actionSpecified = true;

newStudentAxd.StudentTable = new AxdEntity_StudentTable[1] { newStudentEntity };
#endregion
updateProxy.update(updateContext, Program.readCritera(rollNumber), newStudentAxd);
Console.Write("Worked");
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Failed");
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
}



In my next post I will demonstrate the use of methods of document service class which we can use for our customizations.