Hello! I am no expert in C# in customization projects so all my knowledge is based on ChatGPT, which has been super helpful so far.
We have a UDF currently made available on the SOLine on Details tab. It is a string data type and is a drop-down combo box. The goal: this UDF should only be editable when the item on this SO is in a specific item class (CUSTOMNS). If the item is not in this item class, then the field should be locked and not editable.
I have pasted the codes below, open to any suggestions. I am able to publish the project, but with what I have, the field is locked and not editable regardless of the item class. Thanks in advance!
The UDF code is:
[PXMergeAttributes(Method = MergeMethod.Merge)]
[PXStringList(
new string[] { "MCC", "FORMENS", "JTex", "HongKong" },
new string[] { "MCC", "Formens", "JTex", "HongKong" }
)]
[PXUIField(DisplayName = "Factory ID", Enabled = false)]
I also manually added a SOOrderEntry Graph Extension:
using System;
using PX.Data;
using PX.Objects.SO;
using PX.Objects.IN;
namespace PX.Objects.SO
{
public class SOOrderEntry_Extension : PXGraphExtension<SOOrderEntry>
{
protected void _(Events.RowSelected<SOLine> e)
{
if (e.Row == null) return;
var line = (SOLine)e.Row;
bool editable = IsCustomNSItem(e.Cache, line);
PXUIFieldAttribute.SetReadOnly<SOLineExt.usrFactoryID>(e.Cache, null, false);
PXUIFieldAttribute.SetEnabled<SOLineExt.usrFactoryID>(e.Cache, line, editable);
}
protected void _(Events.FieldUpdated<SOLine.inventoryID> e)
{
if (e.Row == null) return;
var line = (SOLine)e.Row;
bool editable = IsCustomNSItem(e.Cache, line);
PXUIFieldAttribute.SetReadOnly<SOLineExt.usrFactoryID>(e.Cache, null, false);
PXUIFieldAttribute.SetEnabled<SOLineExt.usrFactoryID>(e.Cache, line, editable);
if (!editable)
e.Cache.SetValueExt<SOLineExt.usrFactoryID>(line, null);
}
protected void _(Events.RowInserted<SOLine> e)
{
if (e.Row == null) return;
var line = (SOLine)e.Row;
bool editable = IsCustomNSItem(e.Cache, line);
PXUIFieldAttribute.SetReadOnly<SOLineExt.usrFactoryID>(e.Cache, null, false);
PXUIFieldAttribute.SetEnabled<SOLineExt.usrFactoryID>(e.Cache, line, editable);
}
protected void _(Events.RowUpdated<SOLine> e)
{
if (e.Row == null) return;
var line = (SOLine)e.Row;
bool editable = IsCustomNSItem(e.Cache, line);
PXUIFieldAttribute.SetReadOnly<SOLineExt.usrFactoryID>(e.Cache, null, false);
PXUIFieldAttribute.SetEnabled<SOLineExt.usrFactoryID>(e.Cache, line, editable);
if (!editable)
e.Cache.SetValueExt<SOLineExt.usrFactoryID>(line, null);
}
private bool IsCustomNSItem(PXCache cache, SOLine line)
{
if (line?.InventoryID == null)
return false;
InventoryItem item =
PXSelectorAttribute.Select<SOLine.inventoryID>(cache, line) as InventoryItem;
if (item?.ItemClassID == null)
return false;
INItemClass itemClass = INItemClass.PK.Find(Base, item.ItemClassID);
return string.Equals(itemClass?.ItemClassCD, "CUSTOMNS", StringComparison.OrdinalIgnoreCase);
}
}
}