JSON CREATION & SERIALIZATION USING FORMJSONSERIALIZER In D365Fo using X++.
This is used in custom service. Below are different examples.
Example 1:
Here I am passing the external item and qty data to update the stock on hand in D365fo.
Inputs from JSON. Operation is done in D365FO and returns the message like success / failed.
Create a service class and add the below methods.
[AifCollectionType('record', Types::Class, classStr(TestDataContract))]
public str updateStockOnHand(List record)
{
str jsread;
TestInventTable testInventTable,testInventTableUpd;
CustVendExternalItem custVendExternalItem;
ListEnumerator lEnum = record.getEnumerator();
;
while (lEnum.MoveNext())
{
try
{
TestDataContract recordsContract = lEnum.current();
ExtCodevalueTable extCodevalueTable;
InventDimCombination inventdimcombination;
InventItemBarcode inventItemBarcode;
str extTableName = 'InventDimCombination';
str extitemid = recordsContract.externalCode();
real qty = recordsContract.qty();
extCodevalueTable = ExtCodeValueTable::findValue(tableName2Id(extTableName),
ExtCodeSubModule::None,_extCodeCode,extitemid);
inventdimcombination = InventDimCombination::findRecId(
extCodevalueTable.ExtCodeRelationRecId);
select firstonly forupdate testInventTable
where testInventTable.ExternalItemId == extitemid
&& testInventTable.Active == NoYes::Yes;
if(testInventTable)
{
ttsbegin;
testInventTable.Qty = qty;
testInventTable.update();
ttscommit;
jsread += strFmt("%1, %2", extitemid,
"Is updated succesfully");
}
else
{
jsread += strFmt("%1:%2", extitemid,
"vendor part number is not exists or it is not active.");
}
}
catch
{
jsread += strFmt("%1",infolog.text());
continue;
}
}
// jsread = FormJsonSerializer::serializeClass(lEnum);
return jsread;
}
Output.
Contract class:
//[DataContractAttribute,Newtonsoft.Json.JsonObject(IsReference = false)] -- remove Id value.
[DataContractAttribute]
public class TestDataContract
{
str itemId;
real qty;
[DataMemberAttribute('qty')]
public real qty(real _qty = qty)
{
qty = _qty;
return qty;
}
[DataMemberAttribute('External code')]
public str externalCode(str _externalCode = externalCode)
{
externalCode = _externalCode;
return externalCode;
}
}
=========================================================================
Example 2:
Here I am passing the item value from PostMan to get the related details of the item from D365fo.
Input from PostMan.
Response from D365FO.(Take the inputs and send the related data).
// Return Item quantity.
Service class:
[AifCollectionType('return', Types::Class, classStr(Test2GetItemStockDataContract))]
public List GetItemStockOnHand(str itemid)
{
TestInventTable testInventTable;
InventQty stockOrdered,actualQty;
InventSum inventSum;
InventDim inventDim;
InventDimCombination inventDimCombination;
EcoResDistinctProductVariant ecoResDistinctProductVariant;
ExtCodevalueTable extCodevalueTable;
InventOnhand inventOnhand;
InventDimParm inventDimParm;
List list = new List(Types::Class);
;
select firstonly ecoResDistinctProductVariant
where ecoResDistinctProductVariant.RecId == str2Int64(itemId);
select firstonly testInventTable
where testInventTable.ProductVariantNumber == ecoResDistinctProductVariant.DisplayProductNumber
&& testInventTable.Active == NoYes::Yes;
if(testInventTable.RecId && ecoResDistinctProductVariant.RecId)
{
select firstonly inventdimcombination
where inventdimcombination.DistinctProductVariant == ecoResDistinctProductVariant.RecId;
inventDim.InventStyleId = inventDimCombination.inventDim().InventStyleId;
inventDim.InventColorId = inventDimCombination.inventDim().InventColorId;
inventDim.InventSizeId = inventDimCombination.inventDim().InventSizeId;
inventDimParm.initFromInventDim(inventDim);
inventOnhand = InventOnhand::newParameters(
inventdimcombination.ItemId,
inventDim, inventDimParm);
stockOrdered = inventOnhand.onOrder() + inventOnhand.reservOrdered();
actualQty = testInventTable.Qty - stockOrdered;
Test2GetItemStockDataContract data = new Test2GetItemStockDataContract();
data.location("Test");
data.qty(strFmt("%1",actualQty));
list.addEnd(data);
}
else
{
Test2GetItemStockDataContract data = new Test2GetItemStockDataContract();
data.location('Item code is invalid.');
list.addEnd(data);
}
return list;
}
Input/ OutPut.
========================================================================
Example 3:
Here I am getting the details from the D365FO. I have created a new custom table for storing the items in D365FO. Here every Item has an active check box. My code will return only active Items.
Response from D365FO.
// Get Active Items from invent table.
Service class:
// [AifCollectionType('return', Types::String), SysEntryPointAttribute(true)]
[AifCollectionType('return', Types::Class, classStr(Test3GetItemDetailsContract))]
public List GetItemDetails()
{
TestInventTable testInventTable;
List resultSet = new List(Types::Class);
;
while select testInventTable
where testInventTable.Active == NoYes::Yes
{
Test3GetItemDetailsContract data = new Test3GetItemDetailsContract();
data.vendorCode(testInventTable.VendorCode);
data.vendorPartNumber(testInventTable.ExternalItemId);
data.productNumber(testInventTable.ItemId);
data.productVariantNumber(testInventTable.ProductVariantNumber);
data.productName(testInventTable.ItemName);
//resultSet.addEnd(strFmt("vendorCode:%1,vendorPartNumber:%2,
// productNumber:%3, productvariantnumber:%4,
// productname:%5",
// testInventTable.VendorCode,
// testInventTable.ExternalItemId,
// testInventTable.ItemId,
// testInventTable.ProductVariantNumber,
// testInventTable.ItemName)); string format
resultSet.addEnd(data);
}
return resultSet;
}
Keep Daxing!!
No comments:
Post a Comment