Reading Json in Microsoft Dynamics AX/D365 is very common requirement in integration.Here we will use standard AX/D365 class RetailCommonWebAPI for reading JSON string.
Sample Code
[code language=”cpp”]
static void readJSON(Args _args)
{
mapEnumerator mapEnumerator;
Map data;
List testList;
str _jsonStr;
container test;
ListEnumerator listEnumerator;
int containerList;
Map map1;
int lengthIterate;
;
_jsonStr = ‘{“branchList”:[“601″,”602″,”603″,”604”]}’;
data = RetailCommonWebAPI::getMapFromJsonString(_jsonStr);
mapEnumerator = data.getEnumerator();
while (mapEnumerator.moveNext())
{
switch (mapEnumerator.currentKey())
{
case “branchList” :
test = mapEnumerator.currentValue();
testList =List::create(test);
listEnumerator = testList.getEnumerator();
while (listEnumerator.moveNext())
{
test = listEnumerator.current();
info(conpeek(test, 1));
}
break;
}
}
}
Import records from CSV file into D365/AX table using X++ code.
Sample Code
[code language=”cpp”]
class ACXInventBatchImport
{
public static void main(Args _args)
{
AsciiStreamIo file;
Array fileLines;
FileUploadTemporaryStorageResult fileUpload;
ItemId itemId;
InventBatchId inventBatchId;
MaximumRetailPrice_IN mrp, mrpMultiple;
AcxMfgInventBatchId mfgInventBatchID;
ExpirationDate expDate;
real qtyMultiple;
InventTable inventTable;
//Upload a file
fileUpload = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
file = AsciiStreamIo::constructForRead(fileUpload.openResult());
if (file)
{
if (file.status())
{
throw error(“@SYS52680”);
}
file.inFieldDelimiter(‘,’); // CSV must have Comma , separator
file.inRecordDelimiter(‘\r\n’);
}
Call any method of caller form into a class or any other form
Requirement
Here is very impressive piece of code for calling any method of custom or base form in a class or any form.
In standard form “PurchCreateFromSalesOrder” there is a base method specifyVendAccount which i need to call on extension class of form “PurchCreateFromSalesOrder” on the modification of a new custom control i have created separately on the same form “PurchCreateFromSalesOrder“.
Sample Code With Explanation
I created one class and a new event method.
[FormControlEventHandler(formControlStr(PurchCreateFromSalesOrder, FormStringControl1), FormControlEventType::Modified)]
public static void FormStringControl1_OnModified(FormControl sender, FormControlEventArgs e)
{
FormRun formRun =sender.formRun();
SalesLine salesLine;
FormDataSource formDataSource;
Object formRunObject;
;
salesLine = SalesLine::findRecId(68719716393); formRunObject = formRun.dataSource(1); formRunObject.specifyVendAccount(true, SalesLine, ‘INMF-000001’); //here i am calling the method of form datasource 🙂
formRun.dataSource(1).write();
formRun.dataSource(1).research(true);
formRun.dataSource(1).refresh();
}
Microsoft Dynamics AX, Microsoft Dynamics 365 for Finance and Operations, D365 FO, Retail, SQL, Microsoft Power Apps, Microsoft Power BI, Microsoft Azure, Logic Apps, Microsoft Flow, Microsoft power automate, Microsoft Power Platform,.Net, X++, C#, Power BI DAX, Data Warehousing, Microsoft Analysis Services, SQL Server Reporting Services