I have a user field in the sales order header. When a user types their amount into that header, it loops through the lines and sets another user field based on that header. However, those lines are not saving their values. They are updated on the screen, but I am not sure they are updated in the cache. However, if I click on a line in the details window first, then update the value in the header, that line saves just fine and the others do not.
protected virtual void SOOrder_UsrMarkupPercent_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
{
SOOrder order = e.Row as SOOrder;
if (order == null || (order.LastModifiedByScreenID == "CR304000" && order.OrderNbr.Trim().ToLower().Contains("<new>"))) return;
SOOrderExt orderExt = order.GetExtension<SOOrderExt>();
SetMarkupPercentAllLines(cache, orderExt.UsrMarkupPercent);
}
public void SetMarkupPercentAllLines(PXCache cache, decimal? percent)
{
foreach (SOLine line in Base.Transactions.Select())
{
SOLineExt lineExt = line.GetExtension<SOLineExt>();
lineExt.UsrMarkupPercent = percent;
//cache.SetValue<SOLineExt.usrMarkupPercent>(line, percent);
}
}
I tried just doing lineExt.UsrMarkupPercent = percent as well as cache.SetValue(line, percent) and cache.SetValueExt(line, percent) and nothing seems to work. I've tried using a PXSelect rather than Base.Transactions.Select() as well, but that didn't make a different. Is this not updating the cache properly do to something I am not doing?
I appreciate any help. Thanks!
you need to update the value in cache (Ex: Base.Transactions.Update(line)) after setting the value in the extension field.
Something like this:
foreach (SOLine line in Base.Transactions.Select())
{
SOLineExt lineExt = line.GetExtension<SOLineExt>();
lineExt.UsrMarkupPercent = percent;
Base.Transactions.Update(line);
}
Usually you will need to call the cache Update or cache.SetValueExt. In your commented line you had the cache of SOOrder and not Base.Transactions.Cache.SetValueExt. Using the view.Update in your case should work fine
Related
I have a processing page which may contain multiple rows with a common id value. When a row is selected, I would like to deselect and disable all other rows with the same id value. Likewise if that row is deselected I want to re-enable all the other rows with the same id. I know I need to use a rowupdated event but I don't know how to attach to the cache for the grid. Any help is appreciated.
public void EDIOrder_RowUpdated(PXCache sender, PXRowUpdatedEventArgs e)
{
EDIOrder row = (EDIOrder)e.Row;
if (row != null)
{
// attach to cache and update other rows with same id
}
}
I had a similar situation, where PayoutDetail records belonging to the same Payout needed to stay in synch. I chose Field_Updated for the event handling to select or deselect like-records:
protected virtual void RCPayoutDetail_Selected_FieldUpdated(
PX.Data.PXCache cache, PX.Data.PXFieldUpdatedEventArgs e)
{
RCPayoutDetail row = (RCPayoutDetail)e.Row;
if (row != null)
{
// keep all Payouts in synch
foreach (RCPayoutDetail rec in this.Records.Select())
{
if (rec.RCPayoutID == row.RCPayoutID &&
row.RCPayoutDetailID != rec.RCPayoutDetailID)
{
cache.SetValue<RCPayoutDetail.selected>(rec, row.Selected);
}
}
this.Records.View.RequestRefresh();
}
}
In your case for enable/disable, try adding:
PXUIFieldAttribute.SetEnabled<RCPayoutDetail.selected>(
cache, rec, !(bool)row.Selected);
// optional, see RowSelected comments and changes suggested below
cache.SetStatus(rec, row.Selected == true ?
PXEntryStatus.Notchanged : PXEntryStatus.Modified);
In a processing page, if you are disabling the entire row, but only enabling the Selected field during RowSelected, then add the following "if" line to RowSelected for interplay with row's Status:
protected virtual void RCPayoutDetail_RowSelected(PX.Data.PXCache cache, PX.Data.PXRowSelectedEventArgs e)
{
RCPayoutDetail row = (RCPayoutDetail)e.Row;
if (row != null)
{
//Set Row Enabled = false
PXUIFieldAttribute.SetEnabled(cache, e.Row, false);
// optional if line, otherwise always enable Selected
if(cache.GetStatus(row) == PXEntryStatus.Updated || row.Selected == null)
PXUIFieldAttribute.SetEnabled<RCPayoutDetail.selected>(
cache, row, true);
}
}
I am updating the Notes on a Bill via some custom code in a graph extension. My problem is that the window that pops open when you click on the Notes icon in the upper-right corner of the screen does not reflect the changes that I made to the notes in code. If I use the < > buttons to scroll to another Bill and then back to the one that I updated, it shows the changes. So, I'm not sure how to get the notes to refresh.
This is on the AP301000 screen. I have tried Base.Document.View.RequestRefresh(), Base.Document.View.Clear() and calling the Base.Actions.PressSave() after my code changes the note for the Bill.
TIA!
Here's Code:
protected void APTran_RowInserted(PXCache cache, PXRowInsertedEventArgs e)
{
var row = (APTran)e.Row;
if (row == null)
return;
//*********************** copy notes and file attachments from PO lines and header to the Bill **********************************
if ((row.PONbr != null) && (row.POLineNbr != null))
{
POOrderEntry poEntry = (POOrderEntry)PXGraph.CreateInstance(typeof(POOrderEntry));
poEntry.Clear();
POOrder poHeader = poEntry.Document.Search<POOrder.orderNbr>(row.PONbr);
poEntry.Document.Current = poHeader;
POLine poLine = poEntry.Transactions.Search<POLine.lineNbr>(row.POLineNbr);
if (poLine != null)
{
PXNoteAttribute.CopyNoteAndFiles(poEntry.Caches[typeof(POLine)], poLine, cache, row); //// - use this for Notes and Files.
// making of copy of what the note is on the APInvoice at this point because the PXNoteAttribute.CopyNoteAndFiles() below
// will replace what is in the notes with what is in the PO instead of appending.
string oldNote = PXNoteAttribute.GetNote(Base.Caches[typeof(APInvoice)], Base.CurrentDocument.Current);
PXNoteAttribute.CopyNoteAndFiles(poEntry.Caches[typeof(POOrder)], poHeader, Base.Caches[typeof(APInvoice)], Base.CurrentDocument.Current);
PXNoteAttribute.SetNote(Base.Caches[typeof(APInvoice)], Base.CurrentDocument.Current, oldNote);
Base.Actions.PressSave();
string poNote = PXNoteAttribute.GetNote(poEntry.Caches[typeof(POOrder)], poHeader);
if (!string.IsNullOrEmpty(poNote))
{
//string oldNote = PXNoteAttribute.GetNote(Base.Caches[typeof(APInvoice)], Base.CurrentDocument.Current);
if ((oldNote == null) || !oldNote.Contains(poNote))
{
string newNote = "";
if (string.IsNullOrEmpty(oldNote))
newNote = poNote;
else
newNote = oldNote + Environment.NewLine + poNote;
//These 2 lines will not update the note without the PressSave();
//Guid noteGuid = (Guid)PXNoteAttribute.GetNoteID(Base.Caches[typeof(APInvoice)], Base.CurrentDocument.Current, null);
//PXNoteAttribute.UpdateNoteRecord(Base.Caches[typeof(APInvoice)], noteGuid, newNote);
PXNoteAttribute.SetNote(Base.Caches[typeof(APInvoice)], Base.Document.Current, newNote); // Sets the note but, screen does not refresh.
//Base.Caches[typeof(APInvoice)].Update(Base.Document.Current); //Does not refresh Notes on screen.
//PXNoteAttribute.GetNoteID<APInvoice.noteID>(Base.Caches[typeof(APInvoice)], Base.Document.Current); // Does not update the screen.
//Base.Caches[typeof(Note)].IsDirty = true; /// No Effect.
//Base.Caches[typeof(Note)].Clear(); //this has no effect on refreshing the notes that are seen on the screen.
//Base.Caches[typeof(NoteDoc)].Clear();
//Base.Actions.PressSave(); // blanks out the header if the Bill has never been saved. Does not refresh note on screen.
//Base.Document.View.Clear();
//Base.Document.View.RequestRefresh(); // this wipes out the new note if adding a second PO.
}
}
}
}
}
Here's the fix for this that I deployed. I'm thinking that there is a better way to do this but, seems to be working ok for now. I added this:
[PXOverride]
public void Persist(Action persist)
{
persist();
APInvoice invoice = (APInvoice)Base.CurrentDocument.SelectSingle();
if (invoice.Status == "H")
throw new PXRedirectRequiredException(Base, "Reloading Notes...");
}
So, I overrode the Persist() in order to refresh the page but, I only do that if the Bill is still on Hold. This refreshes the Notes that are shown on the screen.
I have extended the SOOrderEntry graph and added the following code in order to update another line on the same sales order that is related to the current line that is being updated:
protected virtual void SOLine_RowUpdating(PXCache cache, PXRowUpdatingEventArgs e)
{
if (e.NewRow == null)
{
return;
}
SOLine soLine = (SOLine)e.NewRow;
SOLine relatedLine = Base.Transactions.Search<SOLine.inventoryID>
(456);
if (relatedLine != null)
{
relatedLine.Qty = soLine.Qty;
relatedLine.CuryUnitPrice = 24.20;
Base.Transactions.Update(relatedLine);
Base.Transactions.View.RequestRefresh();
}
}
When I try to test this by updating the Qty on the current line, the Unit Price on the related line only updates every other time that I update the Qty. The related item is a Non-stock item where the current item is a Stock Item.
I'm doing this in a Sales Demo environment on 18.102.0048
I tried this but, now the Extended Price is always 0.00:
protected virtual void SOLine_RowUpdating(PXCache cache, PXRowUpdatingEventArgs e)
{
if (e.NewRow == null)
{
return;
}
SOLine soLine = (SOLine)e.NewRow;
SOLine relatedLine = Base.Transactions.Search<SOLine.inventoryID>
(456);
if (relatedLine != null)
{
SOLine oldRelatedLine = PXCache<SOLine>.CreateCopy(relatedLine);
relatedLine.Qty = soLine.Qty;
relatedLine.CuryUnitPrice = 24.20;
Base.Transactions.Cache.RaiseRowUpdated(relatedLine, oldRelatedLine);
Base.Transactions.Update(relatedLine);
Base.Transactions.View.RequestRefresh();
}
}
When a field calculated value depends on another field value it's sometimes required to trigger events for the value to be recalculated.
In some cases it only requires firing the event for the field that you modified. Assigning values using the C# assignment operator (=) or the SetValue method will not trigger the events handler, SetValueExt method will:
// Doesn't trigger events
relatedLine.Qty = soLine.Qty;
cache.SetValue<SOLine.qty>(relatedLine, soLine.Qty);
// This will trigger events
cache.SetValueExt<SOLine.qty>(relatedLine, soLine.Qty);
There are some cases where you need to fire events for the whole row too. You can use the RaiseXxxYyy methods to do that. I used Current in the example but you might have to adapt it if Current is not the row being modified:
SOLine oldRowCopy = PXCache<SOLine>.CreateCopy(Base.Transactions.Current);
Base.Transactions.Current.Qty = soLine.Qty;
Base.Transactions.Cache.RaiseRowUpdated(Base.Transactions.Current, oldRowCopy);
EDIT:
Looking at updated question, it might not make much of a difference but I suggest you switch the order of these 2 lines:
Base.Transactions.Cache.RaiseRowUpdated(relatedLine, oldRelatedLine);
Base.Transactions.Update(relatedLine);
Like this:
// You want the value in Cache to be updated
Base.Transactions.Update(relatedLine);
// After Cache value is set you want to raise the events
Base.Transactions.Cache.RaiseRowUpdated(relatedLine, oldRelatedLine);
Thanks to HB_ACUMATICA, here's the code that resolved this:
protected virtual void SOLine_RowUpdating(PXCache cache, PXRowUpdatingEventArgs e)
{
if (e.NewRow == null)
{
return;
}
SOLine soLine = (SOLine)e.NewRow;
SOLine relatedLine = Base.Transactions.Search<SOLine.inventoryID>(456);
if (relatedLine != null)
{
SOLine oldRelatedLine = PXCache<SOLine>.CreateCopy(relatedLine);
relatedLine.Qty = soLine.Qty;
relatedLine.CuryUnitPrice = 24.20;
object outPrice = 0.01;
outPrice = (relatedLine.CuryUnitPrice * relatedLine.Qty);
cache.SetValueExt<SOLine.curyExtPrice>(relatedLine, outPrice);
Base.Transactions.Cache.RaiseRowUpdated(relatedLine, oldRelatedLine);
Base.Transactions.Update(relatedLine);
Base.Transactions.View.RequestRefresh();
}
}
In the Product drop-down, there are 2 values, First value (Product) will default for first line only and Second value (Co-Product) will default from the second line.
I tried this in FieldDefaulting event
protected void TSFormulaProdsNCoProds_Product_FieldDefaulting(PXCache cache, PXFieldDefaultingEventArgs e)
{
var row = (TSFormulaProdsNCoProds)e.Row;
if (row == null)
return;
if (TSFormProdsNCoProds.Select().Count == 0)
{
e.NewValue = "P";
}
else
{
e.NewValue = "C";
}
}
Can anyone provide a suggestion to me?
Most likely you have PXDefault attribute decorated at DAC level. In that case, you need to set Cancel flag to prevent execution of FieldDefaulting event handlers that are defined in attributes.
Example :-
protected void TSFormulaProdsNCoProds_Product_FieldDefaulting(PXCache cache, PXFieldDefaultingEventArgs e)
{
var row = (TSFormulaProdsNCoProds)e.Row;
if (row == null)
return;
e.NewValue = (TSFormProdsNCoProds.Select().Count == 0) ? "P" : "C";
e.Cancel = true;
}
This is explained in Example 5.2: Inserting a Default Detail Data Record of T200 Acumatica Framework Fundamental course.
I am trying to update the Inventory Item ImageUrl if it is found to be null with some conditions. I have added a Usr field called UsrStyleImg to the Item Class screen. This field is for a basic image of an item and it is stored in the database. The functionality I want is if the Inventory Item does not have an image in the ImageUrl then it will default to the UsrStyleImg that is connected with the ItemClassID. ItemClassID is also found on the Stock Item Screen. Here is the code I have in the InventoryItemMaint graph:
protected void InventoryItem_ImageUrl_FieldDefaulting(PXCache cache, PXFieldDefaultingEventArgs e)
{
var row = (InventoryItem)e.Row;
if (row == null) return;
var item = (INItemClass)PXSelect<INItemClass, Where<INItemClass.itemClassID, Equal<Current<InventoryItem.itemClassID>>>>.Select(Base, row.ItemClassID);
var image = PXSelect<InventoryItem, Where<InventoryItem.imageUrl, Equal<Current<InventoryItem.imageUrl>>>>.Select(Base, row.ImageUrl);
if (image != null)
return;
else {
e.NewValue = item.GetExtension<INItemClassExt>().UsrStyleImg;
}
}
The code compiles fine but when I test with an Item that has an Item Class attached to it with an image in the INItemClass table called UsrStyleImg it does not populate to the imageUrl found in the Inventory Item table or the Stock Item screen. I have also tried this with FieldSelecting and using the e.ReturnValue with still the same results.
If I need more clarification please let me know.
Try using a RowSelecting Event
protected virtual void InventoryItem_RowSelecting(PXCache sender, PXRowSelectingEventArgs e)
{
InventoryItem row = e.Row as InventoryItem;
//Extra checks to prevent infinite loops
if (row != null && !string.IsNullOrWhiteSpace(row.InventoryCD) && Base.Item.Cache.GetStatus(row) == PXEntryStatus.Notchanged)
{
if (!string.IsNullOrWhiteSpace(row.ItemClassID))
{
//You must always use a PXConnectionScope if Selecting during RowSelecting
using (new PXConnectionScope())
{
//If you're going to pass in a value in .Select, use Required instead of Current.
INItemClass itemClass = PXSelectReadonly<INItemClass, Where<INItemClass.itemClassID, Equal<Required<INItemClass.itemClassID>>>>.Select(Base, row.ItemClassID);
if (itemClass != null && string.IsNullOrWhiteSpace(row.ImageUrl))
{
INItemClassExt itemClassExt = itemClass.GetExtension<INItemClassExt>();
//To prevent unneeded update if it's blank
if (!string.IsNullOrWhiteSpace(itemClassExt.UsrStyleImg))
{
row.ImageUrl = itemClassExt .UsrStyleImg;
//Force set the status in the Cache, otherwise it infinite loops
Base.Item.Cache.SetStatus(row, PXEntryStatus.Updated);
Base.Item.Update(row);
}
}
}
}
}
}