How to call SetPropertyException from another event handler? - acumatica

Below is my code to insert whatever value is entered into my UsrWLAmt field into my BudgetGrid representing the history of the fields values.
I want to raise a warning prompting the user to enter a value into the details field in the BudgetGrid History
protected void PMProject_UsrWLAmt_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e, PXFieldUpdated InvokeBaseHandler)
{
if(InvokeBaseHandler != null)
InvokeBaseHandler(cache, e);
var row = (PMProject)e.Row;
PMProject con = Base.Project.Current;
PX.Objects.PM.ProjectExt item = con.GetExtension<PX.Objects.PM.ProjectExt>();
if (item.UsrWLAmt > 0)
{
atcBudgetHis bud = new atcBudgetHis();
bud.CreatedDateTime = DateTime.Now;
bud.Value = item.UsrWLAmt;
BudgetGrid.Insert(bud);
// to attach the exception object to the field
BudgetGrid.View.Cache.RaiseExceptionHandling<atcBudgetHis.details>(
bud, " ",
new PXSetPropertyException(
"Please specifiy reason for budget change.",
PXErrorLevel.Warning));
}
}
I've also tried BudgetGrid.Cahce.RaiseExceptionHandling
The code above doesn't raise any trace errors.
EDIT:
PXUIFieldAttribute.SetWarning<atcBudgetHis.details>(BudgetGrid.Cache, null, "Please specifiy reason for budget change.");
Works for all rows but
PXUIFieldAttribute.SetWarning<atcBudgetHis.details>(BudgetGrid.Cache, bud, "Please specifiy reason for budget change.");
Doesn't raise any warnings.
I could create another field above the grid for the notes to be inserted, but is there a way I can set the warning for the last row in the BudgetGird?

First things first, to show a warning in Acumatica one of the following events must be used:
FieldVerifying and throw PXSetPropertyException, when warning should appear only during the time user updates a record
RowUpdating with RaiseExceptionHandling method invoked on PXCache, if warning should appear on multiple fields only during the time user updates a record
RowSelected with RaiseExceptionHandling method invoked on PXCache, if warning should appear on multiple fields all the time until a user addresses the cause of warning
I guess for your particular scenario, RowSelected might work best to constantly show warnings for all empty cells within Notes column:
public void atcBudgetHis_RowSelected(PXCache sender, PXRowSelectedEventArgs e)
{
atcBudgetHis row = e.Row as atcBudgetHis;
if (row == null) return;
if (string.IsNullOrEmpty(row.Details))
{
sender.RaiseExceptionHandling<atcBudgetHis.details>(row, string.Empty,
new PXSetPropertyException("Please specify reason for budget change.", PXErrorLevel.Warning));
}
else
{
sender.RaiseExceptionHandling<atcBudgetHis.details>(row, row.Details, null);
}
}

It seems like you tried to set a warning on a DAC instance that didn't exists in the grid at the moment the event was called.
Have you tried setting the warning on the existing row returned in the event handler parameter instead?
PXUIFieldAttribute.SetWarning<atcBudgetHis.details>(BudgetGrid.Cache, row, "Please specify reason for budget change.");
The warning applies to all rows that satisfy the condition that executes this line. If you want to display it for only the last row, you would have to manually check if the row received in the parameter is the same as the last row in your data view and only then execute the warning for that row.

The solution was to use the RowInserted event for my grid and pass the row variable into SetWarning

You need to change this code:
BudgetGrid.Insert(bud);
// to attach the exception object to the field
BudgetGrid.View.Cache.RaiseExceptionHandling<atcBudgetHis.details>(bud, " ",new PXSetPropertyException("Please specifiy reason for budget change.",PXErrorLevel.Warning));
To something like this:
bud = BudgetGrid.Insert(bud); //you need to get the "bud" which is in the cache
// to attach the exception object to the field
BudgetGrid.View.Cache.RaiseExceptionHandling<atcBudgetHis.details>(bud, " ",new PXSetPropertyException("Please specifiy reason for budget change.",PXErrorLevel.Warning));

Related

How to add a column to the grid that shows value from another screen in Acumatica?

I'm new to Acumatica, could you please help me? I have too screens IN202500 (stock items) and SO301000(sales orders). I added a field to stock items and now I need to show a value from that field in grid column of sale orders for each stock items. I suppose that I need to use PXDefault attribute for this?
There are a number of ways you can do this. I'll provide 3 possibilities.
If your View used by the grid contains InventoryItem, you may be able simply to select your custom field from InventoryItem and add it directly to the screen. I'll assume this is not an option or you likely would have found it already.
Create a custom field in a DAC extension on SOLine where you add your custom field as unbound (PXString, not PXDBString) and then use PXDBScalar or PXFormula to populate it. I haven't used PXDBScalar or PXFormula to retrieve a value from a DAC Extension, so I'll leave it to you to research. I do know this is super easy if you were pulling a value directly from InventoryItem, so worth doing the research.
Create as an unbound field as in #2, but populate it in the SOLine_RowSelecting event. This is similar to JvD's suggestion, but I'd go with RowSelecting because it is the point where the cache data is being built. RowSelected should be reserved, in general, for controlling the UI experience once the record is already in the cache. Keep in mind that this will require using a new PXConnectionScope, as Acuminator will advise and help you add. (Shown in example.) In a pinch, this is how I would do it if I don't have time to sort out the generally simpler solution provided as option 2.
Code for Option 3:
#region SOLine_RowSelecting
protected virtual void _(Events.RowSelecting<SOLine> e)
{
SOLine row = (SOLine)e.Row;
if (row == null)
{
return;
}
using (new PXConnectionScope())
{
SOLineExt rowExt = row.GetExtension<SOLineExt>();
InventoryItem item = SelectFrom<InventoryItem>
.Where<InventoryItem.inventoryID.IsEqual<#P.AsInt>>
.View.Select(Base, row.InventoryID);
InventoryItemExt itemExt = item.GetExtension<InventoryItemExt>();
rowExt.UsrSSMyDatAField = itemExt.UsrSSMyDataField;
}
}
#endregion

Trying to customize JAMS LaborEntry screen, looking for the line of code that will update the header

Brand new to Acumatica development and stuck on a simple thing. :(
I am customizing the LaborEntry screen of the JAMS MFG.
I have added a field to the header by extending the AMBatch DAC, called UsrTimeClocked.
For now I simply wish to set this field to a number right at the end of the RowInserted event at the detail level of the AMMTran and see my number on the screen, up on the header AMBatch.
public class LaborEntry_Extension : PXGraphExtension<LaborEntry>
{
protected virtual void _(Events.RowInserted<AMMTran> e)
{
AMBatchExt ext = Base.batch.Current.GetExtension<AMBatchExt>();
ext.UsrTimeClocked = 5.32;
//Insert line to update the correct object to see 5.32 in the TextBox, before RowSelected is done.
}
}
As is my value goes in the field and any refresh/save/delete of the row does update the correct object and I see my value where I want it. I wish to know the way to force this update.

Assign value to custom field on dialog panel (Allocation)

I had added a custome field "UsrWgtIndex" on Allocation panel in Purchase Receipts. And its value is the sum of the other custom field "UsrWgtPerUnit".
But strange thing happened. The value of UsrWgtIndex keeps the same when I open diffrent allocation panel. It is always the value of first row of transations.
My code is below, and I'm really confused about this. In logic, the code will sum each row of transations, and assign each row of the "UsrWgtIndex". But it's always the value of the first row.
Anyone can help on this? Thanks a lot!
namespace PX.Objects.PO {
public class POReceiptEntry_Extension: PXGraphExtension < POReceiptEntry > {
#region Event Handlers
decimal totalCgt = 0 M,
tempTotal = 0 M;
protected void POReceiptLine_RowSelected(PXCache cache, PXRowSelectedEventArgs e) {
POReceiptLine poRLine = (POReceiptLine) e.Row;
if (poRLine != null) {
totalCgt = 0 M;
foreach(POReceiptLineSplit line in Base.splits.Select()) {
POReceiptLineSplitExt poReceiptLineSplitExt = line.GetExtension < POReceiptLineSplitExt > ();
var recentQty = poReceiptLineSplitExt.UsrWgtPerUnit;
var temp = Convert.ToDecimal(recentQty);
totalCgt = totalCgt + temp;
};
var cgt = Convert.ToDecimal(totalCgt);
if (totalCgt != null) {
cache.SetValue < POReceiptLineExt.usrTotalWgt > (poRLine, cgt);
//This line is setting the value of usrWgtIndex
cache.SetValue < POReceiptLineExt.usrWgtIndex > (poRLine, cgt);
};
}
}
}
}
More detail update:
The customer field “usrWgtIndex” belongs to the data class “POReceiptLine”. But I make its control on the Allocations Panel.
I had made a test: set value to the field “UnassignedQty” on allocations panel with the same value I give to “usrWgtIndex”. It works correctly. Or I changed the other field in POReceiptLine data class with same value in the same time, It works fine again.
3.It seems that if I change a custom field on allocations panel, this strange thing would happen…
More Detail Update2:
I didn't add the "UsrWgtIndex" on the DAC LotSerOptions. I added it on the POReceiptLine. Because when I add the custom field on LotSerOptions, I can't assign its value with setValueEXT methord, it seems there is no DAC named "LotSerOptionsExt".
So I just put "UsrWgtIndex" on DAC POReceiptLine, and assign its value with
cache.SetValue<POReceiptLineExt.usrWgtIndex>(poRLine, cgt);
The 'splits' DataView depends on 'Current' POReceiptLine:
PXSelect<POReceiptLineSplit, Where<POReceiptLineSplit.receiptNbr, Equal<Current<POReceiptLine.receiptNbr>>,
And<POReceiptLineSplit.lineNbr, Equal<Current<POReceiptLine.lineNbr>>,
And<Where<POLineType.goodsForInventory, Equal<Current<POReceiptLine.lineType>>,
Or<POLineType.goodsForSalesOrder, Equal<Current<POReceiptLine.lineType>>,
Or<POLineType.goodsForDropShip, Equal<Current<POReceiptLine.lineType>>>>>>>>> splits;
The issue is that 'Current' POReceiptLine doesn't change when user clicks (select) another POReceiptLine in 'transactions' grid. Setting the grid 'SyncPosition' to true in your customization project should ensure 'Current' value is properly set when users change record selection in the grid:

How do I prevent SOLineSplit and INItemPlan from deleting when Qty = 0?

I noticed that when the quantity of a SOLine is zero, there are no SOLineSplit or INItemPlan records available for that line. The second the quantity is greater than 0, the system makes those records, and if the qty is set back to 0, the records are deleted.
Is there a way to prevent the SOLineSplit and INItemPlan objects from deleting when a record is set to 0 quantity?
Is there a way to still have the system create an SOLineSplit and INItemPlan if the SOLine is initially created with a 0 quantity?
The reason for the question is that a customer wants the system to lock the SOLine after a certain point, but also allow for the Qty to be adjusted from another screen. Since this is not directly changing the value on the SOLine screen, this isn't triggering the events to create the split and plan.
I have tried creating an instance of SOOrderEntry in the custom screen as follows:
SOOrderEntry graph = PXGraph.CreateInstance<SOOrderEntry>();
//Also tried graph.Transactions.Current = line, but did not work
graph.Transactions.Update(line);
graph.Actions.PressSave();
Doing that keeps resulting in a null object reference error:
Error: An error occurred during processing of the field OrderQty : Object reference not set to an instance of an object..
System.NullReferenceException: Object reference not set to an instance of an object.
at PX.Objects.SO.SOOrderEntry.SOLine_OrderQty_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e) ...
I think the problem with your statement in using SOOrderEntry is you are not truly loading the order in the graph to update it correctly. After you create the graph instance and before the transaction update, you should load the document header like this...
SOOrderEntry graph = PXGraph.CreateInstance<SOOrderEntry>();
graph.Document.Current = graph.Document.Search<SOOrder.orderNbr>(line.OrderNbr, line.OrderType);
if(graph.Document.Current == null)
{
return;
}
graph.Transactions.Update(line);
graph.Actions.PressSave();
As for controlling how the plan and split records are entered from SOOrderEntry... The entries are controlled through the attributes on PlanID. The cache attached is where this gets added on SOOrderEntry...
[PXMergeAttributes(Method = MergeMethod.Append)]
[SOLineSplitPlanID(typeof(SOOrder.noteID), typeof(SOOrder.hold), typeof(SOOrder.orderDate))]
protected virtual void SOLineSplit_PlanID_CacheAttached(PXCache sender)
{
}
You can make your own graph extension of Sales order and replace the attribute with your own version of SOLineSplitPlanID... it might be a battle for you as I am not sure why you would want the plan record to exist when zero qty to plan.

How can I get the number of rows of a detail grid?

I'm trying to get the number of rows in documents Details grid. I don't know how to call this inside of another instance.
for example: In this method I receive row by row, I want to know how can I get the total number of rows received.
public virtual void ARTran_RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
{
var row = (ARTran)e.Row;
}
This is for screen Invoices and Meme (AR301000)
Do a .Select() of your data view and use the Count property.
var rowCount = Base.Transactions.Select().Count;
In your case, you want the data view used by the detail grid, which is Transactions. You can find more info on how the screen in builded by using the Inspect Element tool. You can read more about it on http://acumaticaopenuniversity.com/pdf/T300_Acumatica_Cust_Platform.pdf

Resources