Solved

Override field value and use it as condition of Automation Schedule

  • 23 February 2024
  • 6 replies
  • 54 views

Userlevel 3
Badge

Hello 

I want to override value of field Asset Class and Department of data class FAAccrualTran (screen FA504500)

I implemented event handler RowInserting to set value for two fields: Asset Class and Department as following:

protected void FAAccrualTran_RowInserting(PXCache cache, PXRowInsertingEventArgs e, PXRowInserting InvokeBaseHandler)
{
if (InvokeBaseHandler != null)
InvokeBaseHandler(cache, e);
var row = (FAAccrualTran)e.Row;
if (row != null && row.GLTranInventoryID != null)
{
InventoryItem inventory = PXSelect<InventoryItem, Where<InventoryItem.inventoryID, Equal<Required<InventoryItem.inventoryID>>>>.Select(Base, row.GLTranInventoryID);
if (inventory != null)
{
var inventoryItemExt = PXCache<InventoryItem>.GetExtension<InventoryItemExt>(inventory);
if (inventoryItemExt != null && inventoryItemExt.UsrDepartment != null)
{
cache.SetValueExt<FAAccrualTran.department>(row, inventoryItemExt.UsrDepartment.Trim());
}
if (inventoryItemExt != null && inventoryItemExt.UsrAssetClass != null)
{
cache.SetValueExt<FAAccrualTran.classID>(row, inventoryItemExt.UsrAssetClass.Trim());
}
}
}
}

Then I create a Schedule Automation as use these field as condition. However, the value of these field aren’t override 

Do you know how to achieve the goal: set value of field Asset Class and Department which is taken from Stock Item and could use it as trigger condition for Schedule Automation

 

Tx,

Khoi

icon

Best answer by darylbowman 23 February 2024, 12:44

View original

6 replies

Badge +11

RowInserting is only called once, and how much information the row has when it is inserted varies depending upon how it was written. It's very unreliable for things like this.

I believe the proper way would be to write a FieldDefaulting event for each field. If there is already a FieldDefaulting event, you'll need to write it as an override. Then, write a FieldUpdated event for InventoryID that calls SetDefaultExt for both fields. This will call the defaulting event when InventoryID is changed, leading to your values being set.

Badge +11

@mrthanhkhoi - Were you able to make any progress?

Userlevel 3
Badge

Hello @darylbowman

Unfortunately it still doesn't work. I implemented 2 event FieldDefaulting for field AssetClass and Department, also 1 event FieldUpdated for InventoryID but the value of AssetClass and Department wasn’t taken from InventoryID

Here is my code:

using PX.Data;
using PX.Objects.IN;

namespace PX.Objects.FA
{
// Acuminator disable once PX1016 ExtensionDoesNotDeclareIsActiveMethod extension should be constantly active
public class AssetGLTransactions_Extension : PXGraphExtension<AssetGLTransactions>
{
#region Event Handlers
protected void FAAccrualTran_GLTranInventoryID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e, PXFieldUpdated InvokeBaseHandler)
{
if (InvokeBaseHandler != null)
InvokeBaseHandler(cache, e);
var row = (FAAccrualTran)e.Row;
if (row != null && row.GLTranInventoryID != null)
{
InventoryItem inventory = PXSelect<InventoryItem, Where<InventoryItem.inventoryID, Equal<Required<InventoryItem.inventoryID>>>>.Select(Base, row.GLTranInventoryID);
if (inventory != null)
{
var inventoryItemExt = PXCache<InventoryItem>.GetExtension<InventoryItemExt>(inventory);
if (inventoryItemExt != null && inventoryItemExt.UsrDepartment != null)
{
cache.SetDefaultExt<FAAccrualTran.department>(inventoryItemExt.UsrDepartment);
}
if (inventoryItemExt != null && inventoryItemExt.UsrAssetClass != null)
{
cache.SetDefaultExt<FAAccrualTran.classID>(inventoryItemExt.UsrAssetClass);
}
}
}
}

protected void FAAccrualTran_Department_FieldDefaulting(PXCache cache, PXFieldDefaultingEventArgs e, PXFieldDefaulting InvokeBaseHandler)
{
if (InvokeBaseHandler != null)
InvokeBaseHandler(cache, e);
var row = (FAAccrualTran)e.Row;
if (row != null && row.GLTranInventoryID != null)
{
InventoryItem inventory = PXSelect<InventoryItem, Where<InventoryItem.inventoryID, Equal<Required<InventoryItem.inventoryID>>>>.Select(Base, row.GLTranInventoryID);
if (inventory != null)
{
var inventoryItemExt = PXCache<InventoryItem>.GetExtension<InventoryItemExt>(inventory);
if (inventoryItemExt != null && inventoryItemExt.UsrDepartment != null)
{
e.NewValue = inventoryItemExt.UsrDepartment;
e.Cancel = true;
}
}
}
}

protected void FAAccrualTran_ClassID_FieldDefaulting(PXCache cache, PXFieldDefaultingEventArgs e, PXFieldDefaulting InvokeBaseHandler)
{
if (InvokeBaseHandler != null)
InvokeBaseHandler(cache, e);
var row = (FAAccrualTran)e.Row;
if (row != null && row.GLTranInventoryID != null)
{
InventoryItem inventory = PXSelect<InventoryItem, Where<InventoryItem.inventoryID, Equal<Required<InventoryItem.inventoryID>>>>.Select(Base, row.GLTranInventoryID);
if (inventory != null)
{
var inventoryItemExt = PXCache<InventoryItem>.GetExtension<InventoryItemExt>(inventory);
if (inventoryItemExt != null && inventoryItemExt.UsrAssetClass != null)
{
e.NewValue = inventoryItemExt.UsrAssetClass;
e.Cancel = true;
}
}
}
}
#endregion
}
}

Do you have further idea?

Badge +11

You’re close, but SetDefaultExt does not get a value passed into it. The default value is handled by the event. Here’s how I would write one of the defaulting events and the field updated:

protected virtual void _(Events.FieldDefaulting<FAAccrualTran, FAAccrualTran.department> e, PXFieldDefaulting b)
{
FAAccrualTran row = e.Row;
if (row is null) return;

b?.Invoke(e.Cache, e.Args);

InventoryItem inventory = InventoryItem.PK.Find(e.Cache.Graph, row?.GLTranInventoryID);
var inventoryItemExt = inventory?.GetExtension<InventoryItemExt>();
if (inventoryItemExt?.UsrDepartment is object)
{
e.NewValue = inventoryItemExt.UsrDepartment;
e.Cancel = true;
}
}

protected virtual void _(Events.FieldUpdated<FAAccrualTran, FAAccrualTran.gLTranInventoryID> e, PXFieldUpdated b)
{
FAAccrualTran row = e.Row;
if (row is null) return;

b?.Invoke(e.Cache, e.Args);

// Calls the defaulting event
e.Cache.SetDefaultExt<FAAccrualTran.department>(row);
}

Notice I’ve passed SetDefaultExt the row from the event instead of a value. The value will be set by the logic in the field defaulting event handler.

Also note that this uses ? frequently to check for null instead of many if statements.

Userlevel 3
Badge

@darylbowman, thank you for your suggestion.

I have tried with your propose but it still doesn’t work. I can’t find value of Department column of FAAccuralTran even newly row is added

 

Badge +11

I’m not familiar with this screen, and my original suggestions were based on the assumption that rows were being inserted on this screen like your code suggested. However, (still not really understanding much about this screen), I don’t think FAAccrualTran is being inserted on this screen, therefore, FieldDefaulting isn’t going to fire. From the graph code, it looks like this list is simply selected from the database. The inserting seems to have happened previously from a different graph. In order to use the above model, it would have to be on a screen where the rows are being inserted.

The interesting thing is, when the Department field at the top is changed, the Department field of FAAccrualTran is being changed in the cache:

 

It stands to reason that perhaps you could do something similar at an appropriate time.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved