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

I’m using AIF document service with NETTCP or HTTP Adapter to Create Purchase order service, here is steps

Create Query

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

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

Using AIF Wizards

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

Choose recent created query and click next.

Create Service operation and AxBC class

Click next and then Generate.

You will get service project in Private project

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

Active recent created Service

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

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!

Scenarios:

I’m trying to create product/master product in Dynamics AX using AIF inbound port, the AIF services consume by C#.NET.

From AX 2012 R2, Item is replaced with Product. Item master was in Inventory Management Module, now there is a separate module for item/product creation Product information Management.

Some definitions you should know There are two types of Products in 2012 they are:

  1. Product

    Product information management/Common/Products/Products

  2. Product Master

    Product information management/Common/Products/Products master

    a. Variants:

To create a product variant, you must define at least one product dimension for a product master. You can also rename dimensions. To create product variants, you must complete the following tasks:

  • Set up dimensions, such as size, color, and style.

  • Set up variant groups.

  • Assign variant groups to a retail hierarchy.

  • Create a product master and variants.

    b. Product dimensions

Product dimensions are characteristics that serve to identify a product variant. You can use combinations of product dimensions to define product variants. You must define at least one product dimension for a product master to create a product variant.

Process:

Normally in AX, we create items master follow process:

  • Create product/product master.

  • Assigning Dimensions Groups to a Product Master.

  • Create Product dimension combinations (Product Variants)

  • Release product to legal entities

  • Assigning Item Model Group & Item Groups to a Product Master

How to do:

Ax provides us standard services for this purpose, so we don’t need to create any custom services for this. I will use 4 services for this purpose, descriptions below

Service Purpose
EcoResProductService Create products (all types). The service can also be used to retrieve data that has already been created (Create Product details in The EcoRes tables).
EcoResProductMasterDimValueService Specify values of product dimensions for a product master. These values become available for the creation of product variants. The service can also be used to retrieve data that has already been created.
ItemService Release distinct products and product masters. The service can also be used to retrieve data that has already been created.
InventDimCombinationService Release product variants. The service can also be used to retrieve data that has already been created.

we have 4 steps

  1. Create 4 AIF inbound services against Services operation above and active it http://DEV-ERP:8103/DynamicsAx/Services/BCEcoResProduct http://DEV-ERP:8103/DynamicsAx/Services/BCEcoResProductMasterDimValue http://DEV-ERP:8103/DynamicsAx/Services/BCItemsMaster http://DEV-ERP:8103/DynamicsAx/Services/BCInventDimCombination

  2. After services creation, open visual studio then creates new Console project and add service References for that, you will get somethings like pic below:

  3. Using C#.Net to consume service

using ItemsMaster.ItemsRef;
using ItemsMaster.EcoResProductRef;
using ItemsMaster.EcoResProductMasterRef;
using ItemsMaster.InventDimRef;
static void Main(string[] args)
{
	Program master = new Program();
	master.createDistinctProduct();
	Program.releaseProduct();
}

EcoResProductServiceClient

public void createDistinctProduct()
{
	AxdEntity_Product_EcoResDistinctProduct distinctProduct = new AxdEntity_Product_EcoResDistinctProduct()
	{
		DisplayProductNumber = "MAX00002",
		ProductType = AxdEnum_EcoResProductType.Item,
		SearchName = "Max",
	   
	};

	distinctProduct.Translation = new AxdEntity_Translation[1];
	distinctProduct.Translation[0] = new AxdEntity_Translation()
	{
		LanguageId = "en-us",
		Name = "Max Nguyen"
	};

	distinctProduct.Identifier = new AxdEntity_Identifier[1];
	distinctProduct.Identifier[0] = new AxdEntity_Identifier()
	{
		ProductNumber = "MAX00002"
	};

	distinctProduct.StorageDimGroup = new AxdEntity_StorageDimGroup[1];
	distinctProduct.StorageDimGroup[0] = new AxdEntity_StorageDimGroup()
	{
		Product = "MAX00002",
		StorageDimensionGroup = "SW_P"
	};

	distinctProduct.TrackingDimGroup = new AxdEntity_TrackingDimGroup[1];
	distinctProduct.TrackingDimGroup[0] = new AxdEntity_TrackingDimGroup()
	{
		Product = "MAX00002",
		TrackingDimensionGroup = "Batch Only"
	};

	AxdEcoResProduct product = new AxdEcoResProduct()
	{
		Product = new AxdEntity_Product_EcoResProduct[1] { distinctProduct }
	};

	EcoResProductRef.CallContext EcoResProductSctx = new EcoResProductRef.CallContext()
	{
		Company = "bgr",
		Language = "en-us",
	};

	EcoResProductRef.EcoResProductServiceClient ecoResProductSClient = new EcoResProductRef.EcoResProductServiceClient();
	
	ecoResProductSClient.create(EcoResProductSctx, product);

}

EcoResProductServiceClient

public void createMaster()
{
	AxdEntity_Product_EcoResProductMaster productMaster = new AxdEntity_Product_EcoResProductMaster()
	{
		DisplayProductNumber = "MAX00002",
		ProductType = AxdEnum_EcoResProductType.Item,
		SearchName = "Max Nguyen"
	};

	productMaster.Translation = new AxdEntity_Translation[1];
	productMaster.Translation[0] = new AxdEntity_Translation()
	{
		LanguageId = "en-us",
		Name = "Max Nguyen"
	};

	productMaster.Identifier = new AxdEntity_Identifier[1];
	productMaster.Identifier[0] = new AxdEntity_Identifier()
	{
		ProductNumber = "MAX00002"
	};
	productMaster.ProductDimGroup = new AxdEntity_ProductDimGroup[1];
	productMaster.ProductDimGroup[0] = new AxdEntity_ProductDimGroup()
	{
		Product = "MAX00002",
		ProductDimensionGroup = "MAX10"
	};
	productMaster.VariantConfigurationTechnology = AxdEnum_EcoResVariantConfigurationTechnologyType.PredefinedVariants;

	AxdEcoResProduct axdProduct = new AxdEcoResProduct()
	{
		Product = new AxdEntity_Product_EcoResProduct[1] { productMaster }
	};

	EcoResProductRef.CallContext EcoResProductSctx = new EcoResProductRef.CallContext()
	{
		Company = "bgr",
		Language = "en-us",
	};

	EcoResProductRef.EcoResProductServiceClient ecoResProductSClient = new EcoResProductRef.EcoResProductServiceClient();
	
	ecoResProductSClient.create(EcoResProductSctx, axdProduct);

}

EcoResProductMasterDimValueServiceClient

static void createMasterDimensions()
{

	EcoResProductMasterRef.AxdEntity_EcoResSize ecoResSizeL = new EcoResProductMasterRef.AxdEntity_EcoResSize()
	{
		Name = "L"
	};

	EcoResProductMasterRef.AxdEntity_EcoResSize ecoResSizeM = new EcoResProductMasterRef.AxdEntity_EcoResSize()
	{
		Name = "M"
	};
	//master dimensions definition (two sizes, L and M)
	AxdEntity_MasterDim_EcoResProductMasterSize sizeDimensionL = new AxdEntity_MasterDim_EcoResProductMasterSize()
	{
		SizeProductMaster = "MAX00002",
		Size = "L",
		EcoResSize = new EcoResProductMasterRef.AxdEntity_EcoResSize[1] { ecoResSizeL }
	};
	AxdEntity_MasterDim_EcoResProductMasterSize sizeDimensionM = new AxdEntity_MasterDim_EcoResProductMasterSize()
	{
		SizeProductMaster = "MAX00002",
		Size = "M",
		EcoResSize = new EcoResProductMasterRef.AxdEntity_EcoResSize[1] { ecoResSizeM }

	};

	AxdEcoResProductMasterDimValue axdDimValue = new AxdEcoResProductMasterDimValue()
	{
		MasterDim = new AxdEntity_MasterDim_EcoResProductMasterDimensionValue[2] { sizeDimensionL, sizeDimensionM }
	};

	EcoResProductMasterRef.CallContext masterDimctx = new EcoResProductMasterRef.CallContext();
	EcoResProductMasterDimValueServiceClient masterDimensionService = new EcoResProductMasterDimValueServiceClient();
	try
	{
		masterDimensionService.create(masterDimctx, axdDimValue);
	}
	catch (Exception e)
	{
		System.Console.WriteLine(e.Message);
		System.Console.ReadKey();
	}
}

EcoResProductServiceClient

static void createVariant()
{
	//product variant definition
	AxdEntity_Product_EcoResDistinctProductVariant productVariant = new AxdEntity_Product_EcoResDistinctProductVariant()
	{
		DisplayProductNumber = "MAXL",
		ProductType = AxdEnum_EcoResProductType.Item,
		SearchName = "MAXL",
		ProductMaster = "MAX00002"
	};
	productVariant.Translation = new AxdEntity_Translation[1];
	productVariant.Translation[0] = new AxdEntity_Translation()
	{
		LanguageId = "en-us",
		Name = "Max L size"
	};
	productVariant.VariantDimValue = new AxdEntity_VariantDimValue_EcoResProductVariantDimensionValue[1];
	productVariant.VariantDimValue[0] = new AxdEntity_VariantDimValue_EcoResProductVariantSize()
	{
		DistinctProductVariant = "MAXL",
		ProductDimensionAttribute = 3173,//The ID of the EcoResSize table
		Size = "L",
		EcoResSize = new EcoResProductRef.AxdEntity_EcoResSize[1]
		{
			new EcoResProductRef.AxdEntity_EcoResSize() { Name = "L" }
		}
	};

	AxdEcoResProduct axdProduct = new AxdEcoResProduct()
	{
		Product = new AxdEntity_Product_EcoResProduct[1] { productVariant }
	};
	EcoResProductRef.CallContext inventDimctx = new EcoResProductRef.CallContext();
	EcoResProductServiceClient productService = new EcoResProductServiceClient();
	try
	{
		productService.create(inventDimctx, axdProduct);
	}
	catch (Exception e)
	{
		System.Console.WriteLine(e.Message);
		System.Console.ReadKey();
	}
}

this method can use to release a distinct product or a product master

public static void releaseProduct()
{
	var invent = new AxdEntity_Invent()
	{
		ItemId = "MAX00002",
		UnitId = "box"
	};

	var purch = new AxdEntity_Purch()
	{
		ItemId = "MAX00002",
		UnitId = "box"
	};

	var sales = new AxdEntity_Sales()
	{
		ItemId = "MAX00002",
		UnitId = "box"
	};

	var inventTable = new AxdEntity_InventTable()
	{
		ItemId = "MAX00002",
		PmfProductType = AxdEnum_PmfProductType.MainItem,
		Product = "MAX00002",
		Invent = new AxdEntity_Invent[] { invent },
		Purch = new AxdEntity_Purch[] { purch },
		Sales = new AxdEntity_Sales[] { sales },
		
	};

	var item = new AxdItem()
	{
		InventTable = new AxdEntity_InventTable[1] { inventTable }
	};

	ItemsRef.CallContext callContext = new ItemsRef.CallContext();
	ItemsRef.ItemServiceClient client = new ItemsRef.ItemServiceClient();

	callContext.Company = "bgr";
	callContext.Language = "en-us";

	try
	{
		ItemsRef.EntityKey[] itemCreatedEntity = client.create(callContext, item);
		ItemsRef.EntityKey itemCreated = (ItemsRef.EntityKey)itemCreatedEntity.GetValue(0);

		Console.WriteLine("AxdEntity_Invent " + itemCreated.KeyData[0].Value);
		Console.ReadLine();
	}
	catch (Exception e)
	{
		Console.WriteLine(e.ToString());
		Console.ReadLine();
	}
}

Release product Variants

static void releaseProductVariants()
{
	AxdEntity_InventDimCombination releasedVariant = new AxdEntity_InventDimCombination()
	{
		DistinctProductVariant = "MAX00002",
		ItemId = ""
	};

	AxdInventDimCombination inventDimCombination = new AxdInventDimCombination()
	{
		InventDimCombination = new AxdEntity_InventDimCombination[1] { releasedVariant }
	};

	InventDimRef.CallContext itemsctx = new InventDimRef.CallContext()
	{
		Company = "bgr",
		Language = "en-us"
	};
	InventDimCombinationServiceClient inventDimCombinationService = new InventDimCombinationServiceClient();
	try
	{
		inventDimCombinationService.create(itemsctx, inventDimCombination);
	}
	catch (Exception e)
	{
		System.Console.WriteLine(e.Message);
		System.Console.ReadKey();
	}
}

Just for example, in main I only create Distinct product and release it, but you can use another method to create master, variant, masterDim and so on.

4. Try to run it and here is a result

Thank you for reading and feel free to give me a question.

Base on JournalId on LedgerJournalTable you can use code below for posting into transactions

//Contract class
[
    DataMemberAttribute('gJournalId'),
    SysOperationDisplayOrderAttribute('1')
]
public LedgerJournalId parmJournalNum(LedgerJournalId _journalId = gJournalId)
{
    gJournalId = _journalId;

    return gJournalId;
}

//Processing class
[SysEntryPointAttribute]
public void process(MAV_PostCustPaymentJourContract _contract)
{
    LedgerJournalTable      ledgerJournalTable;
    LedgerJournalCheckPost  postCustPaymentJournal;

    ledgerJournalTable = LedgerJournalTable::find(_contract.parmJournalNum());
    if (ledgerJournalTable)
    {
        postCustPaymentJournal = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable, NoYes::Yes);
        postCustPaymentJournal.run();
    }
}

The Microsoft Dynamics AX runtime manages the storage of value type data on the call stack and reference type objects on the memory heap.

The call stack is the memory structure that holds data about the active methods called during program execution. The memory heap is the memory area that allocates storage for objects that are destroyed automatically by the Microsoft Dynamics AX run-time.

Value types

Value types include the built-in primitive types, extended data types, enumeration types, and built-in collection types.

  • The primitive types are boolean, int, int64, real, date, utcDateTime, timeofday, str, and guid.
  • The extended data types are specialized primitive types and specialized base enumerations.
  • The enumeration types are base enumerations and extended data types.
  • The collection types are the built-in array and container types.

By default, variables declared as value types are assigned their zero value by the Microsoft Dynamics AX runtime. These variables can’t be set to null. Variable values are copied when variables are used to invoke methods and when they are used in assignment statements. Therefore, two value type variables can’t reference the same value.

2015-10-06-the-type-system-of-dynamics-ax-2012

Reference types

Reference types include the record types, class types, and interface types.

  • The record types are table, map, and view. User-defined record types are dynamically composed from application model layers. Microsoft Dynamics AX runtime record types are exposed in the system application programming interface (API). Although the methods are not visible in the AOT, all record types implement the methods that are members of the system xRecord type, a Microsoft Dynamics AX runtime class type.
  • User-defined class types are dynamically composed from application model layers and Microsoft Dynamics AX runtime class types exposed in the system API.
  • Interface types are type specifications and can’t be instantiated in the Microsoft Dynamics AX runtime. Class types can, however, implement interfaces. Variables declared as reference types contain references to objects that the Microsoft Dynamics AX runtime instantiates from dynamically composed types defined in the application model layering system and from types exposed in the system API. The Microsoft Dynamics AX runtime also performs memory deallocation (garbage collection) for these objects when they are no longer referenced.

Reference variables declared as record types reference objects that the Microsoft Dynamics AX runtime instantiates automatically. Class type objects are programmatically instantiated using the new operator. Copies of object references are passed as reference parameters in method calls and are assigned to reference variables, so two variables can reference the same object.

Thank you for reading!