I am trying to create the move transaction (AM302000) automatically when the release button is clicked in the materials screen (AM300000)
This is the override of the release button I have attempted below but it is not creating the new move transaction but there are no validation errors.
#region Release Override
public delegate IEnumerable ReleaseDelegate(PXAdapter adapter);
[PXOverride]
public IEnumerable Release(PXAdapter adapter, ReleaseDelegate baseMethod)
{
// First, execute the base release method to ensure original functionality
var documents = baseMethod(adapter).Cast<AMBatch>().ToList();
// Process move transactions after base release
foreach (AMBatch batch in documents)
{
if (batch.Status == "R") // Check if the batch is Released
{
CreateMoveTransactions(batch);
}
}
return documents;
}
#endregion
#region Private Methods
private void CreateMoveTransactions(AMBatch batch)
{
PXTrace.WriteInformation($"CreateMoveTransactions started for batch: {batch.BatNbr}");
var moveGraph = PXGraph.CreateInstance<MoveEntry>();
// Create batch with mandatory fields
var moveBatch = new AMBatch
{
DocType = "Move",
TranDate = batch.TranDate,
FinPeriodID = batch.FinPeriodID,
Hold = false // Make sure Hold is false to allow saving
};
// Insert the batch and let AutoNumber attribute handle the numbering
moveBatch = moveGraph.batch.Insert(moveBatch);
moveGraph.batch.Current = moveBatch;
if (string.IsNullOrEmpty(moveBatch.BatNbr))
{
throw new PXException("Failed to generate batch number. Please check numbering sequence setup for Move transactions.");
}
// Save the batch first
try
{
moveGraph.Actions.PressSave();
PXTrace.WriteInformation($"Move batch saved successfully with BatNbr: {moveBatch.BatNbr}");
}
catch (Exception ex)
{
PXTrace.WriteError($"Error saving move batch: {ex.Message}");
throw new PXException("Failed to save move batch. Please check numbering sequence setup.", ex);
}
// Updated query to include DocType
var materialTrans = SelectFrom<AMMTran>
.Where<AMMTran.batNbr.IsEqual<@P.AsString>
.And<AMMTran.docType.IsEqual<@P.AsString>>>
.View.Select(moveGraph, batch.BatNbr, batch.DocType)
.RowCast<AMMTran>()
.ToList();
foreach (AMMTran mtran in materialTrans)
{
var refreshedMTran = (AMMTran)PXSelect<AMMTran,
Where<AMMTran.batNbr, Equal<Required<AMMTran.batNbr>>,
And<AMMTran.docType, Equal<Required<AMMTran.docType>>,
And<AMMTran.lineNbr, Equal<Required<AMMTran.lineNbr>>>>>>
.Select(moveGraph, mtran.BatNbr, mtran.DocType, mtran.LineNbr)
.FirstOrDefault();
if (refreshedMTran == null) continue;
var moveTran = moveGraph.transactions.Cache.CreateInstance() as AMMTran;
if (moveTran != null)
{
moveTran.OrderType = refreshedMTran.OrderType;
moveTran.ProdOrdID = refreshedMTran.ProdOrdID;
moveTran.OperationID = refreshedMTran.OperationID;
moveTran.SiteID = refreshedMTran.SiteID;
moveTran.LocationID = refreshedMTran.LocationID;
moveTran.LotSerialNbr = refreshedMTran.ParentLotSerialNbr;
moveTran.DocType = "Move";
moveTran.BatNbr = moveBatch.BatNbr; // Set the BatNbr from the newly created batch
moveTran.Qty = 1m;
moveTran.TranDate = moveBatch.TranDate;
try
{
moveGraph.transactions.Insert(moveTran);
PXTrace.WriteInformation($"Move transaction inserted successfully for BatNbr: {moveTran.BatNbr}");
}
catch (Exception insertEx)
{
PXTrace.WriteError($"Error inserting move transaction: {insertEx.Message}");
throw;
}
}
}
try
{
moveGraph.Actions.PressSave();
PXTrace.WriteInformation("All move transactions saved successfully.");
}
catch (Exception ex)
{
PXTrace.WriteError($"Error saving move transactions: {ex.Message}");
throw new PXException("Failed to create move transactions.", ex);
}
}
#endregion
Please let me know if there is anything missing for the override in the release, thank you!
I am trying to create the move transaction (AM302000) automatically when the release button is clicked in the materials screen (AM300000)
This is the override of the release button I have attempted below but it is not creating the new move transaction but there are no validation errors.
#region Release Override
public delegate IEnumerable ReleaseDelegate(PXAdapter adapter);
[PXOverride]
public IEnumerable Release(PXAdapter adapter, ReleaseDelegate baseMethod)
{
// First, execute the base release method to ensure original functionality
var documents = baseMethod(adapter).Cast<AMBatch>().ToList();
// Process move transactions after base release
foreach (AMBatch batch in documents)
{
if (batch.Status == "R") // Check if the batch is Released
{
CreateMoveTransactions(batch);
}
}
return documents;
}
#endregion
#region Private Methods
private void CreateMoveTransactions(AMBatch batch)
{
PXTrace.WriteInformation($"CreateMoveTransactions started for batch: {batch.BatNbr}");
var moveGraph = PXGraph.CreateInstance<MoveEntry>();
// Create batch with mandatory fields
var moveBatch = new AMBatch
{
DocType = "Move",
TranDate = batch.TranDate,
FinPeriodID = batch.FinPeriodID,
Hold = false // Make sure Hold is false to allow saving
};
// Insert the batch and let AutoNumber attribute handle the numbering
moveBatch = moveGraph.batch.Insert(moveBatch);
moveGraph.batch.Current = moveBatch;
if (string.IsNullOrEmpty(moveBatch.BatNbr))
{
throw new PXException("Failed to generate batch number. Please check numbering sequence setup for Move transactions.");
}
// Save the batch first
try
{
moveGraph.Actions.PressSave();
PXTrace.WriteInformation($"Move batch saved successfully with BatNbr: {moveBatch.BatNbr}");
}
catch (Exception ex)
{
PXTrace.WriteError($"Error saving move batch: {ex.Message}");
throw new PXException("Failed to save move batch. Please check numbering sequence setup.", ex);
}
// Updated query to include DocType
var materialTrans = SelectFrom<AMMTran>
.Where<AMMTran.batNbr.IsEqual<@P.AsString>
.And<AMMTran.docType.IsEqual<@P.AsString>>>
.View.Select(moveGraph, batch.BatNbr, batch.DocType)
.RowCast<AMMTran>()
.ToList();
foreach (AMMTran mtran in materialTrans)
{
var refreshedMTran = (AMMTran)PXSelect<AMMTran,
Where<AMMTran.batNbr, Equal<Required<AMMTran.batNbr>>,
And<AMMTran.docType, Equal<Required<AMMTran.docType>>,
And<AMMTran.lineNbr, Equal<Required<AMMTran.lineNbr>>>>>>
.Select(moveGraph, mtran.BatNbr, mtran.DocType, mtran.LineNbr)
.FirstOrDefault();
if (refreshedMTran == null) continue;
var moveTran = moveGraph.transactions.Cache.CreateInstance() as AMMTran;
if (moveTran != null)
{
moveTran.OrderType = refreshedMTran.OrderType;
moveTran.ProdOrdID = refreshedMTran.ProdOrdID;
moveTran.OperationID = refreshedMTran.OperationID;
moveTran.SiteID = refreshedMTran.SiteID;
moveTran.LocationID = refreshedMTran.LocationID;
moveTran.LotSerialNbr = refreshedMTran.ParentLotSerialNbr;
moveTran.DocType = "Move";
moveTran.BatNbr = moveBatch.BatNbr; // Set the BatNbr from the newly created batch
moveTran.Qty = 1m;
moveTran.TranDate = moveBatch.TranDate;
try
{
moveGraph.transactions.Insert(moveTran);
PXTrace.WriteInformation($"Move transaction inserted successfully for BatNbr: {moveTran.BatNbr}");
}
catch (Exception insertEx)
{
PXTrace.WriteError($"Error inserting move transaction: {insertEx.Message}");
throw;
}
}
}
try
{
moveGraph.Actions.PressSave();
PXTrace.WriteInformation("All move transactions saved successfully.");
}
catch (Exception ex)
{
PXTrace.WriteError($"Error saving move transactions: {ex.Message}");
throw new PXException("Failed to create move transactions.", ex);
}
}
#endregion
Please let me know if there is anything missing for the override in the release, thank you!
Share Improve this question edited 20 hours ago Tharidhi P asked Feb 17 at 9:42 Tharidhi PTharidhi P 93 bronze badges 1- you do not need to set doc type (and doc type "Move" is not valid as this is a single char field). The graph itself knows the type of document it is. Also, you would not set BatNbr on a transaction line. The batch is set in the Header (AMBatch) during save. TranDate will also get set automatically. I would suggest splitting Insert and Update for teh new line. Set insert to set OrderType, ProdOrdID and then take the return of insert to then set values for update. – Brendan Commented 2 hours ago
1 Answer
Reset to default 0The master view of your movegraph
doesn't have any record, so your new AMMTran
record doesn't have a "parent"; and it goes to nowhere. I am not sure why you don't get an error when this function runs. I am not sure you can add a move transaction to a released batch, at least the Move
screen doesn't allow me to do this on my instance. Anyway, you need to have a record in the batch view
. Either you need to create a new one, or you can use the batch
from the function parameter. I added it to your code.
Also, it would be better if you don't use Base
graph anywhere in this function, use movegraph
instead. This is not a problem here, but it could cause interesting bugs when you query a record in one graph, but you try to update it in another one.
private void CreateMoveTransactions(AMBatch batch)
{
// Create a new graph for Move Entry
var moveGraph = PXGraph.CreateInstance<MoveEntry>();
// Put batch to the cache
moveGraph.batch.Current = AMBatch.PK.Find(moveGraph, batch);
// Get all material transactions for this batch
var materialTrans = SelectFrom<AMMTran>
.Where<AMMTran.batNbr.IsEqual<@P.AsString>>
.View.Select(moveGraph, batch.BatNbr) //Base was replaced here
.RowCast<AMMTran>();
foreach (AMMTran mtran in materialTrans)
{
// Create the Move transaction
var moveTran = new AMMTran
{
ProdOrdID = mtran.ProdOrdID, // Production Nbr
SiteID = mtran.SiteID, // Warehouse
LocationID = mtran.LocationID, // Location
LotSerialNbr = mtran.ParentLotSerialNbr, // Lot/Serial Nbr
InventoryID = mtran.InventoryID,
UOM = mtran.UOM,
Qty = mtran.Qty,
TranType = "M" // Move transaction type
};
// Insert the move transaction into MoveEntry graph
moveGraph.transactions.Insert(moveTran);
}
// Save and process the move transactions
moveGraph.Actions.PressSave();
}