Split a production order with X++
Below is code for splitting a production order in a Scheduled status based on an input quantity from a dialog. Note that if this code is put in a class, that class must be set to run on the server, as the AX split code is run on the server. Note that the status decrease is run twice to get the status to Created, and then the Estimation and Scheduling is run for both the old and new production orders.
public void run()
{
ProdParmStatusDecrease prodParmStatusDecrease;
ProdUpdStatusDecrease prodUpdStatusDecrease;
ProdTable splitProdTable;
ProdUpdCostEstimation costEstimation;
ProdParmRelease prodParmRelease;
ProdUpdScheduling scheduling;
ProdParmScheduling prodParmScheduling;
ProdParmSplit prodParmSplit;
ttsBegin;
prodParmStatusDecrease.ProdId = prodId;
prodParmStatusDecrease.initValue();
prodParmStatusDecrease.WantedStatus = ProdStatus::Created;
prodParmStatusDecrease.insert();
prodUpdStatusDecrease = ProdUpdStatusDecrease::newParmBuffer(prodParmStatusDecrease);
prodUpdStatusDecrease.run();
prodUpdStatusDecrease.run();
prodTable.reread();
prodParmSplit.initValue();
prodParmSplit.initFromProdTable(prodTable);
prodParmSplit.QtySplit = splitQty;
prodParmSplit.insert();
prodTable.status().runSplit(prodParmSplit, false);
select firstOnly forUpdate splitProdTable order by ProdId desc where splitProdTable.InventRefTransId == prodTable.InventRefTransId && splitProdTable.ProdId != prodTable.ProdId;
costEstimation = ProdUpdCostEstimation::newProdId(prodTable.ProdId);
costEstimation.run();
prodTable.reread();
costEstimation = ProdUpdCostEstimation::newProdId(splitProdTable.ProdId);
costEstimation.run();
splitProdTable.reread();
prodParmRelease.ProdId = prodTable.ProdId;
prodParmRelease.initParmDefault();
scheduling = ProdUpdScheduling::construct(ProdSchedMethod::OperationScheduling);
prodParmScheduling = ProdUpdScheduling::initParmBufferFromRelease(prodParmRelease);
prodParmScheduling.insert();
scheduling.parmParmBuffer(prodParmScheduling);
scheduling.run();
prodTable.reread();
prodParmRelease.clear();
prodParmRelease.ProdId = splitProdTable.ProdId;
prodParmRelease.initParmDefault();
scheduling = ProdUpdScheduling::construct(ProdSchedMethod::OperationScheduling);
prodParmScheduling = ProdUpdScheduling::initParmBufferFromRelease(prodParmRelease);
prodParmScheduling.insert();
scheduling.parmParmBuffer(prodParmScheduling);
scheduling.run();
splitProdTable.reread();
assignSchedule.parmProdIdCon([prodTable.ProdId, splitProdTable.ProdId]);
assignSchedule.run();
ttsCommit;
}
public void run()
{
ProdParmStatusDecrease prodParmStatusDecrease;
ProdUpdStatusDecrease prodUpdStatusDecrease;
ProdTable splitProdTable;
ProdUpdCostEstimation costEstimation;
ProdParmRelease prodParmRelease;
ProdUpdScheduling scheduling;
ProdParmScheduling prodParmScheduling;
ProdParmSplit prodParmSplit;
ttsBegin;
prodParmStatusDecrease.ProdId = prodId;
prodParmStatusDecrease.initValue();
prodParmStatusDecrease.WantedStatus = ProdStatus::Created;
prodParmStatusDecrease.insert();
prodUpdStatusDecrease = ProdUpdStatusDecrease::newParmBuffer(prodParmStatusDecrease);
prodUpdStatusDecrease.run();
prodUpdStatusDecrease.run();
prodTable.reread();
prodParmSplit.initValue();
prodParmSplit.initFromProdTable(prodTable);
prodParmSplit.QtySplit = splitQty;
prodParmSplit.insert();
prodTable.status().runSplit(prodParmSplit, false);
select firstOnly forUpdate splitProdTable order by ProdId desc where splitProdTable.InventRefTransId == prodTable.InventRefTransId && splitProdTable.ProdId != prodTable.ProdId;
costEstimation = ProdUpdCostEstimation::newProdId(prodTable.ProdId);
costEstimation.run();
prodTable.reread();
costEstimation = ProdUpdCostEstimation::newProdId(splitProdTable.ProdId);
costEstimation.run();
splitProdTable.reread();
prodParmRelease.ProdId = prodTable.ProdId;
prodParmRelease.initParmDefault();
scheduling = ProdUpdScheduling::construct(ProdSchedMethod::OperationScheduling);
prodParmScheduling = ProdUpdScheduling::initParmBufferFromRelease(prodParmRelease);
prodParmScheduling.insert();
scheduling.parmParmBuffer(prodParmScheduling);
scheduling.run();
prodTable.reread();
prodParmRelease.clear();
prodParmRelease.ProdId = splitProdTable.ProdId;
prodParmRelease.initParmDefault();
scheduling = ProdUpdScheduling::construct(ProdSchedMethod::OperationScheduling);
prodParmScheduling = ProdUpdScheduling::initParmBufferFromRelease(prodParmRelease);
prodParmScheduling.insert();
scheduling.parmParmBuffer(prodParmScheduling);
scheduling.run();
splitProdTable.reread();
assignSchedule.parmProdIdCon([prodTable.ProdId, splitProdTable.ProdId]);
assignSchedule.run();
ttsCommit;
}
Comments
Post a Comment