Search code examples
c#acumatica

Custom Selector Attribute - 'Customer' Cannot be found in the system


I made a custom selector that only displays the customers of the current user but when I select a customer I get the error: 'Customer' Cannot be found in the system. enter image description here

The code for the Custom selector and how I implemented it on the DAC:

[PXNonInstantiatedExtension]
public class SO_SOOrder_ExistingColumn : PXCacheExtension<PX.Objects.SO.SOOrder>
{
    #region CustomerID  
    [PXMergeAttributes(Method = MergeMethod.Merge)]     
    [PXForeignReference(typeof(Field<SOOrder.customerID>.IsRelatedTo<BAccount.bAccountID>))]
    [SalesRepCustomer]  
   
    public int? CustomerID { get; set; }
    #endregion
}

public class SalesRepCustomer : PXCustomSelectorAttribute
{
    public SalesRepCustomer() : base(typeof(Customer.acctCD))
    {
        this.DescriptionField = typeof(Customer.acctCD);
    }
    protected virtual IEnumerable GetRecords()
    {

        foreach (Customer pc in PXSelect<Customer>.Select(this._Graph))
        {
            //Getting Current UserID
            var cache1 = _Graph.Caches[BqlCommand.GetItemType(typeof(AccessInfo.userName))];
            AccessInfo currentCacheObjecta = (AccessInfo)cache1.Current;
            var userName = currentCacheObjecta.UserName;
            SalesPerson person = PXSelect<SalesPerson, Where<SalesPerson.descr, Equal<Required<AccessInfo.userName>>>>.Select(_Graph, userName);
            if (person != null)
            {
                CustSalesPeople custSalesPeople = PXSelect<CustSalesPeople, Where<CustSalesPeople.salesPersonID, Equal<Required<SalesPerson.salesPersonID>>, And<CustSalesPeople.bAccountID, Equal<Required<CustSalesPeople.bAccountID>>>>>.Select(_Graph, person.SalesPersonID, pc.BAccountID);
                //return all customers related to this SalesPersonID in the CustSalesPeople table                   
                if (!(custSalesPeople is null))
                {
                    yield return pc;
                }
            }
            else
            {
                //current user is not a sales person
                //return all of the customers
                yield return pc;
            }
        }
    }
}

Screenshot of the selector on the Sales Order Screen: enter image description here

Any help on this will be appreciated


Solution

  • You should have the constructor look something like

            public SalesRepCustomer() : base(typeof(Customer.bAccountID))
            {
                this.DescriptionField = typeof(Customer.acctName);
                this.SubstituteKey = typeof(Customer.acctCD);
            }
    

    The first type that you pass to the base constructor is the type of the value that will be used for the field.

    In this case you want the customer id(an int) and you are currently using the AcctCD(a string) field. The description field would typically be the name of the customer account and the substitute key will make it so that the users see the AcctCD instead of the the customer ID(the actual value) which is just an integer.