AIF Custom response value in Dynamics AX 2012 R3
For this demonstration, I use AIF service to create Sales order with SalesSalesOrderService
and I gonna consume AIF using C#.NET.
Normally, in consume service application we handle return value by using EntityKeyList
, EntityKey
, KeyData[0].Value
. And for Sales Order It will return Created SalesID.
Let's take a look on AxdSalesOrder
class. More about Axd
class please prefer this.
Then go to createList
method, this method will handle response value
public AifEntityKeyList createList(
AifDocumentXml _xml,
AifEndpointActionPolicyInfo _actionPolicyInfo,
AifConstraintListCollection _constraintListCollection)
{
AifEntityKeyList aifEntityKeyList;
aifEntityKeyList = super(_xml, _actionPolicyInfo, _constraintListCollection);
// Sales orders are committed - master planning explosion can be executed and confirmed dates be set
this.postSalesOrderCreation(aifEntityKeyList);
return aifEntityKeyList;
}
So, how about customer wants another meaning value beside SalesID
likes InventTransId
information in SalesLine
table or another tables base on your requirement. To do that, we need to customize this method.
How to do
I will use Map
and MapEnumerator
classes (Please refer MSDN to understand these class), this is steps:
- We get the
SalesId
from the originalentityKeyList
and add intoMap
. - add
Map
toMapEnumerator
. - Use
SalesId
to find the sales line. (mapEnumerator.currentValue()
can get currentSalesID
). - Create a new
entityKey
and insert the sales line information to thisentityKey
. - add back
entityKey
toentityKeyList
From here in entityKeyList
will store information of SalesID
and List fields of SalesLine
table.
Here is the code for createList
method, beside SalesId
I will try to get InventTransId
in SalesLine
Table
public AifEntityKeyList createList(
AifDocumentXml _xml,
AifEndpointActionPolicyInfo _actionPolicyInfo,
AifConstraintListCollection _constraintListCollection)
{
AifEntityKeyList aifEntityKeyList;
SalesId salesId;
SalesLine salesLine;
AifEntityKey entityKey;
AifEntityKey salesEntityKey;
Map keyDataMap;
Map salesOrderMap;
MapEnumerator mapEnumerator;
aifEntityKeyList = super(_xml, _actionPolicyInfo, _constraintListCollection);
// Sales orders are committed - master planning explosion can be executed and confirmed dates be set
this.postSalesOrderCreation(aifEntityKeyList);
entityKey = aifEntityKeyList.getEntityKey(1);
keyDataMap = entityKey.parmKeyDataMap();
mapEnumerator = keyDataMap.getEnumerator();
while (mapEnumerator.moveNext())
{
salesId = mapEnumerator.currentValue();
if (salesId)
{
while select InventTransId, RecId from salesLine
where salesLine.SalesId == salesId
{
salesEntityKey = new AifEntityKey();
salesOrderMap = new Map(Types::Integer, Types::Container);
salesEntityKey.parmTableId(tableNum(SalesLine));
salesEntityKey.parmRecId(salesLine.RecId);
salesOrderMap.insert(fieldNum(SalesLine, InventTransId), [salesLine.InventTransId]);
salesEntityKey.parmKeyDataMap(salesOrderMap);
aifEntityKeyList.addEntityKey(salesEntityKey);
}
}
}
return aifEntityKeyList;
}
Code in C#.NET to get list key
SalesOrderServiceClient client = new SalesOrderServiceClient();
try
{
EntityKey[] salesOrderCreatedEntity = client.create(callContext, salesOrder);
//EntityKey salesOrderCreated = (EntityKey)salesOrderCreatedEntity.GetValue(0);
System.Collections.IEnumerator enumerator = salesOrderCreatedEntity.GetEnumerator();
while (enumerator.MoveNext())
{
EntityKey salesOrderCreated = (EntityKey)enumerator.Current;
Console.WriteLine(salesOrderCreated.KeyData[0].Field);
Console.WriteLine(salesOrderCreated.KeyData[0].Value);
}
//Console.WriteLine("The sales order created has a Sales ID of " + salesOrderCreated.KeyData[0].Value);
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}
Output:
Thank you for reading.