xxxAcuGeekxxx
xxxAcuGeekxxx

Reputation: 133

Add Attributes fields on Inventory Lookup

I want to add the attributes for my inventory lookup on Sales Order and Purchase Order, does anyone know how to? or any ideas?

Upvotes: 0

Views: 1899

Answers (2)

Vidhya
Vidhya

Reputation: 56

Updated solution for versions later than 20R1:

Below is the sample scenario to add custom attributes to both CustomerID and InventoryID lookups of Sales Orders (SO301000) screen.

  1. Create the required attributes from Attributes (CS205000) screen.
  2. Add the necessary attribute to the corresponding Customer Classes (AR201000) screen to be added to the CustomerID lookup and to the corresponding Item Classes (IN201000) screen to be added to the InventoryID lookup.
  3. Create a new Attribute class CustomerAttributeSetFieldsAttribute extending CustomerActiveAttribute (since it is used in CustomerID) and override SetColumns by rewriting the same in order to change the FieldList and HeaderList.
public class CustomerAttributeSetFieldsAttribute : CustomerActiveAttribute
{
    public static void SetColumns(PXCache cache, string field, string[] fieldList, string[] headerList)
    {
        PXSelectorAttribute.SetColumns(cache, field, fieldList, headerList);
        foreach (CustomerAttributeSetFieldsAttribute attr in cache.GetAttributes(field).OfType<CustomerAttributeSetFieldsAttribute>())
        {
            attr._FieldList = fieldList;
            attr._HeaderList = headerList;
        }
    }
}
  1. Override Initialize method and call Activate for activating the attribute by passing proper cache - BAccountR - for CustomerID and InventoryItem - for InventoryID. Also, call SetColumns from the custom attribute for CustomerID CustomerAttributeSetFieldsAttribute and PXSelectorAttribute for InventoryID and pass the cache type (where the lookup is present), field (field name), fieldList (list of names of all the fields including the attributes) and headerList (list of names of all the headers including the attributes).
  2. Override the attributes to merge the newly added attribute to CustomerID by adding CustomerAttributeSetFields to SOOrder_CustomerID_CacheAttached method.
public class SOOrderEntry_Extension : PXGraphExtension<SOOrderEntry>
{
    public override void Initialize()
    {
        base.Initialize();
        //for InventoryID lookup
        CR.CRAttributesFieldAttribute.Activate(Base.Caches[typeof(InventoryItem)]);
        PXSelectorAttribute.SetColumns(Base.Caches[typeof(SOLine)],
          "InventoryID", new string[] { "InventoryCD", "Descr", "itemClassID", "Color_ATTRIBUTES" },
           new string[] { "InventoryCD", "Descr", "itemClassID", "$Attributes$-Color" });
            
        //for CustomerID lookup
        CR.CRAttributesFieldAttribute.Activate(Base.Caches[typeof(BAccountR)]);

        CustomerAttributeSetFieldsAttribute.SetColumns(Base.Caches[typeof(SOOrder)],
          "CustomerID", new string[] { "AcctCD", "acctName", "COMPREV_Attributes" },
           new string[] { "AcctCD", "acctName", "$Attributes$-Company Revenue" });
    }

    [PXMergeAttributes(Method = MergeMethod.Merge)]
    [CustomerAttributeSetFields]
    protected virtual void SOOrder_CustomerID_CacheAttached(PXCache sender)
    {
    }
}

Note: Since Customer DAC is derived from BAccount and CustomerActive attribute is derived from Customer, it has the cache type BAccountR and only the attributes which are of the same cache type are available to add to the CustomerID popup. If the user needs to add other attributes of different cache type, it will be better to create a new Attribute and use that instead of CustomerActive.

Upvotes: 1

DChhapgar
DChhapgar

Reputation: 2349

Please refer below code example to include Attributes Columns in Selector and Grid Control utilizing out-of-box CRAttributesFieldAttribute.

Declare a class PXAddAtttributeColumns inherited from CRAttributesFieldAttribute.

public class PXAddAtttributeColumns : CRAttributesFieldAttribute
{
    string[] _names;
    bool _IsForSelector;

    public PXAddAtttributeColumns(string[] names, Type entityType, Type entityIDField, Type classIDField, bool IsForSelector = true)
        : base(entityType, entityIDField, classIDField)
    {
        _names = names;
        _IsForSelector = IsForSelector;
    }

    public override void CacheAttached(PXCache sender)
    {
        this._IsActive = true;
        base.CacheAttached(sender);
    }

    protected override void AttributeFieldSelecting(PXCache sender, PXFieldSelectingEventArgs e, PXFieldState state, string attributeName, int idx)
    {
        if (_names.Any(attributeName.Equals))
        {
            //Out-of-box DisplayName is prefixed with "$Attributes$-" - if you need to take that off.
            state.DisplayName = (!String.IsNullOrEmpty(state.DisplayName)) ? (state.DisplayName.Replace("$Attributes$-", "")) : attributeName;
            state.Visible = true;
            //Requires AutoGenerateColumns="AppendDynamic" for PXGrid Control for dynamic Attribute columns creation
            state.Visibility = (_IsForSelector) ? PXUIVisibility.SelectorVisible : PXUIVisibility.Dynamic;
        }
        base.AttributeFieldSelecting(sender, e, state, attributeName, idx);
    }

    public override void CommandPreparing(PXCache sender, PXCommandPreparingEventArgs e)
    {
        base.CommandPreparing(sender, e);

        if (e.BqlTable == null && aggregateAttributes && sender.GetItemType().IsDefined(typeof(PXProjectionAttribute), true))
        {
            e.BqlTable = _BqlTable;
        }
    }
}

To include attributes as Columns in Inventory Look up, declare DAC Extension as below:

public class InventoryItemPXExt : PXCacheExtension<PX.Objects.IN.InventoryItem>
{
    #region Attributes

    public abstract class attributes : IBqlField { }

    [PXAddAtttributeColumns(new[] { "ASSETID", "HWMODEL" },
            typeof(CSAnswerType.inventoryAnswerType),
            typeof(InventoryItem.inventoryID),
            typeof(InventoryItem.itemClassID))]
    public virtual string[] Attributes { get; set; }

    #endregion
}

Fields will show up as below:

enter image description here

Search can be Enabled on Attribute Columns by setting FilterByAllFields to True

enter image description here

To include attributes as Columns in Sales Order Details Grid, declare DAC Extension as below:

public class SOLineExtension : PXCacheExtension<SOLine>
{
    public abstract class itemAttributes : IBqlField { }

    [PXAddAtttributeColumns(new[] { "ASSETID", "HWMODEL" },
            typeof(CSAnswerType.inventoryAnswerType),
            typeof(SOLine.inventoryID),
            typeof(InventoryItem.itemClassID), false)]
    public virtual string[] ItemAttributes { get; set; }
}

Make sure to specify AutoGenerateColumns="AppendDynamic" for PXGrid control dynamic Attribute columns creation

enter image description here

Fields will show up as below:

enter image description here

To include attributes as Columns in grid of Add Stock Item Dialog, declare DAC Extension as below:

public class SOSiteStatusSelectedExtension : PXCacheExtension<SOSiteStatusSelected>
{
    public abstract class itemAttributes : IBqlField { }

    [PXAddAtttributeColumns(new[] { "ASSETID", "HWMODEL" },
            typeof(CSAnswerType.inventoryAnswerType),
            typeof(InventoryItem.inventoryID),
            typeof(InventoryItem.itemClassID), false)]
    public virtual string[] ItemAttributes { get; set; }
}

Make sure to specify AutoGenerateColumns="AppendDynamic" for PXGrid control dynamic Attribute columns creation

enter image description here

Fields will show up as below:

enter image description here

Note: This example is applicable to 5.3 series – Build 5.30.1367 onwards.

Upvotes: 5

Related Questions