Custom Scripts - X++ - Example 1
X++ Programming Fundamentals - D365 F&O Technical (Article) (Program)
9
Given Input:
Expected Output:
Program:
static void UpdateInventTransDates(Args _args)
{
InventTrans inventTrans;
DictTable dictTable;
RecId recId;
// Define the items and site combinations you want to update
container items = [
["GDY756603738", "4502"],
["???364620", "4454"],
["NIT210820", "4454"],
["COP166255009", "4505"],
["COP160018024", "4505"],
["CRL5110951", "4251"],
["MIC32346", "4401"],
["BRD000-889", "4320"],
["TOWMAX37T", "4320"]
];
// Iterate through each item and its site
for (int i = 1; i <= conLen(items); i++)
{
// Get the ITEMID and INVENTSITEID from the container
str itemId = conPeek(conPeek(items, i), 1);
str inventSiteId = conPeek(conPeek(items, i), 2);
ttsBegin;
select forUpdate inventTrans
where inventTrans.ItemId == itemId
&& inventTrans.InventSiteId == inventSiteId
&& inventTrans.DateFinancial != inventTrans.TransDate
&& inventTrans.IsExcludedFromInventoryValue == NoYes::No
&& inventTrans.StatusIssue <= InventTransStatusIssue::OnOrder // Assuming 1 = OnOrder
&& inventTrans.StatusReceipt <= InventTransStatusReceipt::OnOrder // Assuming 1 = OnOrder
&& inventTrans.DataAreaId == "40"
&& inventTrans.Partition == 5637144576; // Adjust based on your partition value
if (inventTrans.RecId)
{
// Update the DateFinancial field
inventTrans.DateFinancial = 06\04\2023; // Change the date as required
inventTrans.update();
info(strFmt("Updated ITEMID: %1, INVENTSITEID: %2, RecId: %3", itemId, inventSiteId, inventTrans.RecId));
}
else
{
info(strFmt("No records found for ITEMID: %1, INVENTSITEID: %2", itemId, inventSiteId));
}
ttsCommit;
}
}
GDY756603738 4502
???364620 4454
NIT210820 4454
COP166255009 4505
COP160018024 4505
CRL5110951 4251
MIC32346 4401
BRD000-889 4320
TOWMAX37T 4320
Complex Query
static void InventTransSelectQuery(Args _args)
{
InventTrans inventTrans;
InventTransOrigin inventTransOrigin;
InventTransPosting inventTransPosting;
InventDim inventDim;
ttsBegin;
select RecId, Qty, StatusIssue, StatusReceipt, ReferenceCategory, ReferenceId, DateFinancial, TransDate, Voucher
from inventTrans
join inventTransOrigin
where inventTrans.DataAreaId == inventTransOrigin.DataAreaId
&& inventTrans.Partition == inventTransOrigin.Partition
&& inventTrans.InventTransOrigin == inventTransOrigin.RecId
join inventDim
where inventTrans.DataAreaId == inventDim.DataAreaId
&& inventTrans.Partition == inventDim.Partition
&& inventTrans.InventDimId == inventDim.InventDimId
join inventTransPosting
where inventTrans.DataAreaId == inventTransPosting.DataAreaId
&& inventTrans.Partition == inventTransPosting.Partition
&& inventTransPosting.InventTransOrigin == inventTransOrigin.RecId
&& inventTransPosting.InventTransPostingType == 1
where inventTrans.DataAreaId == '40'
&& inventTrans.Partition == 5637144576
&& inventTrans.ItemId == 'TOY364620'
&& inventDim.InventSiteId == '4454'
&& inventTrans.StatusIssue <= InventTransStatusIssue::OnOrder // Assuming 1 corresponds to OnOrder status
&& inventTrans.StatusReceipt <= InventTransStatusReceipt::OnOrder // Assuming 1 corresponds to OnOrder status
&& inventTrans.IsExcludedFromInventoryValue == NoYes::No
&& inventTrans.Voucher == inventTransPosting.Voucher
&& inventTrans.DateFinancial != inventTransPosting.TransDate;
ttsCommit;
}
Output:
Explanation:
Program:
static void UpdateInventTransDates(Args _args) { InventTrans inventTrans; DictTable dictTable; RecId recId; // Define the items and site combinations you want to update container items = [ ["GDY756603738", "4502"], ["???364620", "4454"], ["NIT210820", "4454"], ["COP166255009", "4505"], ["COP160018024", "4505"], ["CRL5110951", "4251"], ["MIC32346", "4401"], ["BRD000-889", "4320"], ["TOWMAX37T", "4320"] ]; // Iterate through each item and its site for (int i = 1; i <= conLen(items); i++) { // Get the ITEMID and INVENTSITEID from the container str itemId = conPeek(conPeek(items, i), 1); str inventSiteId = conPeek(conPeek(items, i), 2); ttsBegin; select forUpdate inventTrans where inventTrans.ItemId == itemId && inventTrans.InventSiteId == inventSiteId && inventTrans.DateFinancial != inventTrans.TransDate && inventTrans.IsExcludedFromInventoryValue == NoYes::No && inventTrans.StatusIssue <= InventTransStatusIssue::OnOrder // Assuming 1 = OnOrder && inventTrans.StatusReceipt <= InventTransStatusReceipt::OnOrder // Assuming 1 = OnOrder && inventTrans.DataAreaId == "40" && inventTrans.Partition == 5637144576; // Adjust based on your partition value if (inventTrans.RecId) { // Update the DateFinancial field inventTrans.DateFinancial = 06\04\2023; // Change the date as required inventTrans.update(); info(strFmt("Updated ITEMID: %1, INVENTSITEID: %2, RecId: %3", itemId, inventSiteId, inventTrans.RecId)); } else { info(strFmt("No records found for ITEMID: %1, INVENTSITEID: %2", itemId, inventSiteId)); } ttsCommit; } } GDY756603738 4502 ???364620 4454 NIT210820 4454 COP166255009 4505 COP160018024 4505 CRL5110951 4251 MIC32346 4401 BRD000-889 4320 TOWMAX37T 4320 Complex Query
static void InventTransSelectQuery(Args _args) { InventTrans inventTrans; InventTransOrigin inventTransOrigin; InventTransPosting inventTransPosting; InventDim inventDim; ttsBegin; select RecId, Qty, StatusIssue, StatusReceipt, ReferenceCategory, ReferenceId, DateFinancial, TransDate, Voucher from inventTrans join inventTransOrigin where inventTrans.DataAreaId == inventTransOrigin.DataAreaId && inventTrans.Partition == inventTransOrigin.Partition && inventTrans.InventTransOrigin == inventTransOrigin.RecId join inventDim where inventTrans.DataAreaId == inventDim.DataAreaId && inventTrans.Partition == inventDim.Partition && inventTrans.InventDimId == inventDim.InventDimId join inventTransPosting where inventTrans.DataAreaId == inventTransPosting.DataAreaId && inventTrans.Partition == inventTransPosting.Partition && inventTransPosting.InventTransOrigin == inventTransOrigin.RecId && inventTransPosting.InventTransPostingType == 1 where inventTrans.DataAreaId == '40' && inventTrans.Partition == 5637144576 && inventTrans.ItemId == 'TOY364620' && inventDim.InventSiteId == '4454' && inventTrans.StatusIssue <= InventTransStatusIssue::OnOrder // Assuming 1 corresponds to OnOrder status && inventTrans.StatusReceipt <= InventTransStatusReceipt::OnOrder // Assuming 1 corresponds to OnOrder status && inventTrans.IsExcludedFromInventoryValue == NoYes::No && inventTrans.Voucher == inventTransPosting.Voucher && inventTrans.DateFinancial != inventTransPosting.TransDate; ttsCommit; }