Tuesday, October 22, 2019

Deserializing Json Array in D365

I was experiencing an issue while creating a purchase requisition which has more than single line in D365  environment via service. The issue was actually specific to de-serializing the JSON array D365 (X++).  Here I am sharing with you the code which may be helpful for you.


   "purchaseRequisitionList":{ 
      "PurchaseRequisition":[ 
         { 
            "ItemId":"Item1",
            "ItemQty":3
  },
         { 
            "ItemId":"Item2",
            "ItemQty":2
         }
      ]
   }
}

First thing let's create data contracts similar to json above, where would define list for JSON  array.

[DataContract]
class PurchaseRequisitionList
{
    List purchaseRequistion;

    [DataMember("PurchaseRequisition")]

    public List parmLinesDetail(List _purchaseRequistion = purchaseRequistion)
    {
        purchaseRequistion = _purchaseRequistion;
        return purchaseRequistion;
    }

}


[DataContract]
class CreatePurchaseRequisitionContract

    ItemId                   itemId;
    Qty                      itemQty;


    [DataMember("ItemId")]

    public ItemId parmItemId(ItemId _itemId = itemId)
    {
        itemId = _itemId;
        return itemId;
    }

    [DataMember("ItemQty")]

    public Qty parmItemQty(Qty _itemQty = itemQty)
    {
        itemQty = _itemQty;
        return itemQty;
    }
}


It was the issue relating to de serializing of JSON array to our contract class. Add the following code to utilize the newtosoft for JObject.

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Given below is the code for posting the purchase requisition. You can add exception handling and fields the way you like.

public EntityResponseContract PostPurchaseRequisitionNew(PurchaseRequisitionList purchaseRequisitionList)
{
  EntityResponseContract      response = new EntityResponseContract();
  str                         message;
  boolean                     success = false;
  PurchReqTable               purchReq;
  PurchReqLine                purchReqLine;


purchReq.clear();
purchReq.initValue();

purchReq.PurchReqId              = NumberSeq::newGetNum(PurchReqTable::numRefPurchReqId()).num();

purchReq.insert();

ListIterator linesDetailIterator = new ListIterator(purchaseRequisitionList.parmLinesDetail());


while(linesDetailIterator.more())

{
JObject headerJObject = linesDetailIterator.value();
purchaseRequisition = JsonParser::JsonToObject(classStr(CreatePurchaseRequisitionContract), headerJObject.ToString());

ttsbegin;

purchReqLine.clear();
purchReqLine.initValue();
purchReqLine.initFromPurchReqTable(purchReq);
purchReqLine.Name                   = purchaseRequisition.parmItemId();
purchReqLine.PurchQty               = purchaseRequisition.parmItemQty();
/*All your remaining fields you can place here*/
purchReqLine.insert();
ttscommit;

linesDetailIterator.next();

}
}

You can write a new class call it JsonParser, or simply type this method is class above.

public class JsonParser
{
//This method will deserialize the json to object
public static object JsontoObject(str _className,str _json)
    {
        Object      returnObject=null;
        try
        {
            returnObject= FormJsonSerializer::deserializeObject(className2Id(_className),_json);
        }
        catch
        {
            error("Unable to deserialize due to an error");
        }
        return returnObject;
    }
}

I hope that will also be a quick start for service end point.

Friday, July 26, 2019

Data access using OData - Filter

As many of you may already be familiar that D365 uses OData (Open data protocol). You can access the data entity let's for example you have developed a Data entity with public collection name "Students".
  1.  Query below will produce the data related to all the students. /data/Students
      2.  Modify The query further to give you only top 10 records. /data/Students?$top=10

     3.  Modify the query to get the students selecting only columns FirstName and the LastName.
              /data/Students?$top=10&$select=FirstName,LastName

     4.  Modify the query to get the students filtering the records specifying the Firstname.
             /data/Students?$top=10&$select=FirstName,LastName&$filter=FirstName%20eq%20%27Elsa%27


Hope this little blog post will help you in filtering the relevant data in Data entities.

Wednesday, July 24, 2019

Download a file after saving in AX 365

I required quite simple code to download an XML file on client machine after saving it to a local directory. This is the small code snippet that might be useful for you as well.


Also this blog post http://erconsult.eu/blog/exposing-dynamics-365-onebox-lan/ will help you configuring the download storage so that we could download file to client browser.