Skip to main content

CRUD Purchase order using AIF in Dynamics AX 2012 R3

Due to Purchase order doesn’t have Standard document service so we have to create new Document service for that using AIF wizards.

We can use the AIF document service with NETTCP or HTTP Adapter to Create Purchase order service, here is steps

1. Create Query

with three datasouce (PurchTable, PurchLine, InventDim) likes below

CRUD-Purchase-order-using-AIF-in-Dynamics-AX-2012-R3

As best practice for Document service, name of query should be start with Axd* prefix.

2. Using AIF Wizards

In AX development environment, go to Tools > Wizards > AIF document service wizards

CRUD-Purchase-order-using-AIF-in-Dynamics-AX-2012-R3

Choose recent created query and click next.

3. Create Service operation and AxBC class

CRUD-Purchase-order-using-AIF-in-Dynamics-AX-2012-R3

Click next and then Generate.

You will get service project in Private project

4. Deploy and create service

• Right click on PurchOrderService > Add-Ins > Register service

• System administration > Setup > Services and AIF > Inbound ports

• Click New on Inbound ports form and name the Service.

• Adapter: NETTCP (it also works with HTTP adapter)

• In Service contract customizations fast tab click Service operations

CRUD-Purchase-order-using-AIF-in-Dynamics-AX-2012-R3

Active recent created Service

5. Consume service using C#.NET

After service is activated, you can get WSDL URI likes

http://servername:port/DynamicsAx/Services/PurchaseOrder

Create console project and Add Service References, the code below CRUD-Purchase-order-using-AIF-in-Dynamics-AX-2012-R3

In Class, I will write method to return list of EnityKey PurchId

private static EntityKey[] EntityForPurchId(string purchId)
{
KeyField field = new KeyField()
{
Field = "PurchId",
Value = purchId
};

EntityKey key = new EntityKey()
{
KeyData = new[] { field }
};

return new[] { key };
}

Code for create purchase order

var dim = new AxdEntity_InventDim()
{
InventSiteId = "DN",
InventLocationId = "F10-S120",
InventBatchId = "BATCH001"
};

var purchLine = new AxdEntity_PurchLine()
{
ItemId = "220067",
PurchQty = 15,
PurchUnit= "ea",

InventDim = new AxdEntity_InventDim[] { dim }
};


var purchTable = new AxdEntity_PurchTable()
{
OrderAccount = "101-01-75441",
LanguageId = "en-us",
CurrencyCode = "KRW",
PurchName = "AIF PO Test",
PurchLine = new AxdEntity_PurchLine[] { purchLine }

};

AxdMav_PurchOrderService purchOrder = new AxdMav_PurchOrderService();

purchOrder.PurchTable = new AxdEntity_PurchTable[] { purchTable };

CallContext callContext = new CallContext
{
Company = "bgr",
Language = "en-us"
};

Mav_PurchOrderServiceServiceClient client = new Mav_PurchOrderServiceServiceClient();

try
{
EntityKey[] purchOrderCreatedEntity = client.create(callContext, purchOrder);
EntityKey purchOrderCreated = (EntityKey)purchOrderCreatedEntity.GetValue(0);

Console.WriteLine("The purch order created has a Purch ID of " + purchOrderCreated.KeyData[0].Value);
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}

Code for read purchase order

EntityKey[] entityKeyList = EntityForPurchId("BGR-000054");

CallContext callContext = new CallContext();
callContext.Company = "bgr";
Mav_PurchOrderServiceServiceClient client = new Mav_PurchOrderServiceServiceClient();

AxdMav_PurchOrderService purchOrders = client.read(callContext, entityKeyList);
AxdEntity_PurchTable[] purchTables = purchOrders.PurchTable;
AxdEntity_PurchTable purchTable = purchTables[0];
AxdEntity_PurchLine purchLine = purchTable.PurchLine[0];

Console.WriteLine("Purch Name: " + purchTable.PurchName);
Console.WriteLine("Order Account: " + purchTable.OrderAccount);
Console.WriteLine("Language Id: " + purchTable.LanguageId);
Console.WriteLine("Qty: " + purchLine.PurchQty);
Console.WriteLine("Item Id: " + purchLine.ItemId);

client.Close();
Console.ReadLine();

Code for update purchase order

Mav_PurchOrderServiceServiceClient client = new Mav_PurchOrderServiceServiceClient();
CallContext callContext = new CallContext();
callContext.Company = "bgr";

EntityKey[] entityKeyList = EntityForPurchId("BGR-000078");
AxdMav_PurchOrderService purchOrders = client.read(callContext, entityKeyList);

//salesOrders.GetHashCode();
AxdEntity_PurchTable[] purchTables = purchOrders.PurchTable;
AxdEntity_PurchTable purchTable = new AxdEntity_PurchTable();
purchTable = purchTables.First();

//salesTable.GetHashCode();
AxdEntity_PurchLine purchLine = new AxdEntity_PurchLine();
purchLine = purchTable.PurchLine.First();

decimal purchQty = 20;
purchLine.PurchQty = purchQty;

try
{
client.update(callContext, entityKeyList, purchOrders);
EntityKey purchOrdersUpdated = (EntityKey)entityKeyList.GetValue(0);
Console.WriteLine("The purchase order has been updated has a Purch ID of " + purchOrdersUpdated.KeyData[0].Value + " with Qty " + purchQty.ToString() + "");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}

Code for delete purchase order

Mav_PurchOrderServiceServiceClient client = new Mav_PurchOrderServiceServiceClient();
CallContext callContext = new CallContext();
callContext.Company = "bgr";

EntityKey[] entityKeyList = EntityForPurchId("BGR-000054");
try
{
client.delete(callContext, entityKeyList);
EntityKey purchOrdersDeleted = (EntityKey)entityKeyList.GetValue(0);
Console.WriteLine("The purch order has been deleted has a purch ID of " + purchOrdersDeleted.KeyData[0].Value);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}

Thank you for reading!