How to serialize int/real properties with 0/0.00 values via AIF outbound integration

It is a known limitation that AIF does not serialize null values in outbound XMLs. This is true for fields of data type int or real (or any EDT based on them) in Microsoft Dynamics AX. If the outbound document has int or real fields with value 0 or 0.0, they would not appear in the outbound XML generated by AIF. The reason is that AX does not have null values so default values (0 for int, 0.00 for real, ‘’ for strings) for each type are treated as nulls and are not serialized by outbound operations.

There are few workarounds for this limitation. If you want a field to be serialized irrespective of its value, you need to make it a “required” field. This can be done in two ways:

-          Make field mandatory at table level ie. In AOT , set the field property ‘Mandatory’ to Yes.

-          Make the field mandatory at service level.

Making a field mandatory at the table level affects all the consumers of the table such as forms. The second way is less intrusive since it only affects services stack. The tradeoff in this case is that both inbound and outbound exchanges for the document would need to include the required field or else the service operation would throw an error.

 This article demonstrates shows how to make a field mandatory at service level.

To demonstrate the outbound scenario, we are using BomBillsofMaterialsService document service already provided in Microsoft Dynamics AX and Contoso demo dataset available with product download.

Now let’s walk through this scenario

1.       Import Contoso dataset

2.       Register BomBillsofMaterialsService in AOT.

3.       Create an outbound port. Expose BomBillsofMaterialsService.* service operations on the port.

4.       Open Inventory and Warehouse Management à Common à Bills Of Materials form. This form already has AIF send API to generate outbound requests for AIF.

5.       Select an item and set ‘From qty = 0.00’ on the form. Click on “Send…”and select the outbound port. This sends an outbound BomBillsofMaterialsService.find request to the AIF.

6.       Run AifOutboundProcessingService and AifGatewaySendService batch jobs. For more information, see the section “Configure and start the AIF batch services” in this topic on MSDN: https://msdn.microsoft.com/en-us/library/hh352313.aspx ..

7.       Check the resulting xml. The outbound xml in this case does not contain the tag ‘FromQty’ since its value is 0.00

As you would have observed that the ‘FromQty’ property is missing from the outbound xml. Let’s apply workaround #2 – of making FromQty mandatory for BomBillsofMaterialsService by following steps:

1.       Open the document class for BomBillsofMaterialsService service – AxdBillsOfMaterials . Override method initMandatoryFieldsMap() from the AxdBase in AxdBillsOfMaterials class

2.       Update method body as

  protected void initMandatoryFieldsMap()
 
 {
 
 super();
 
 this.setParmMethodAsMandatory(classNum(AxBOMVersion),methodStr(AxBOMVersion,parmFromQty));
 
 }
 
  For more information about the initMandatoryFieldsMap method, see https://msdn.microsoft.com/en-us/library/axdbase.initmandatoryfieldsmap.aspx .

 

3.       Here AxBOMVersion is the AxBC class for the table that contains the field and parmFromQty is the name of the field parm method.

4.       Compile the method.

5.       Generate Incremental IL

6.       Register the service again by right clicking the service node in AOT and selecting ‘Register Service’

7.       Stop and restart the AOS to flush all caches.

8.       Click on View Schema button on Document Data Policies form for the outbound port. The updated schema now shows ‘FromQty’ as a mandatory field.

 

 

9.       Send another request to the outbound port with FromQty = 0.00.

10.       Open the outbound xml and this time you will see the FromQty property in the output xml.