acumaticaacumatica-kb

How can I make the results of a selector change based on the value of another field in a grid?


I'd like to create a customization that does the following, but I'm not sure exactly how:

I've put a user field (char 32) on the Account Groups screen (PM201000) in the header section:

Account Groups screen

This user field will have a value which would be the same as the 'Project Task' grid field on the Purchase Orders screen (PO301000) in order to tie those two together:

Project Task field

Now, what I'd like to be able to do is populate the Account selector in the Purchase Orders screen's grid based on the Accounts listed in the Account Group which have a Project Task (user field) value that's the same as the Purchase Orders 'Project Task' grid field.
In addition, if the Purchase Order screen's Project Task grid field is empty, then I'd like to use the Account field's stock / regular selector values.

I've tried setting up a custom selector attribute, but I'm not sure exactly how to do that. I've got what's shown below (which I know is not correct - but the general outline I got from a different customization) - so maybe that's a start...

Add to that, the attribute for the Account selector is NOT a PXSelector, but an [Account] attribute - which I could not find any PXSelect statement in the source code showing how it was getting its values - and the DACs in the Account Groups screen aren't tied to any database tables.

public class AccountSelectorAttribute : PXCustomSelectorAttribute
{
    public AccountSelectorAttribute() : base(typeof(Search<AccountPtr.accountID>), typeof(Search<AccountPtr.accountID>), typeof(Search<AccountPtr.description>))
    {
    }

    public IEnumerable GetRecords() 
    {
        var poline = (POLine)PXView.CurrentGraph.Caches[typeof(POLine)].Current;

        if (poline == null) yield break;

        if (poline.TaskID != null)
        {
            foreach (PMAccountGroup row in PXSelect<PMAccountGroup,
                                           Where<PMAccountGroupExt.usrProjectTask, Equal<Current<PMAccountGroupExt.usrProjectTask>>>>.Select(PXView.CurrentGraph))
            {
                var newRow = new AccountPtr { AccountID = row.AccountID, Description = row.Description };
                yield return newRow;
            }
        }
        else
        {
            //foreach (<Whatever logic selects the accounts in the original selector>)
            //{
            //    yield return row;
            //}
        }
    }

    public override void FieldVerifying(PXCache sender, PXFieldVerifyingEventArgs e)
    {
        var poline = e.Row as POLine;
        //var pmtaskext = sender.GetExtension<PMTaskExt>(pmtask);
        if (poline.TaskID != null)
        {
            base.FieldVerifying(sender, e);
        }

    }


}

Any help would be greatly appreciated. Thanks!


Solution

  • Was able to accomplish this by adding a PXRestrictor attribute to the ExpenseAccountID field via CacheAttached, and adding code to the TaskID_FieldUpdated event.

    protected virtual void POLine_TaskID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
            {
                var poline = e.Row as POLine;
                if (poline != null)
                {
    
    
                    using (new PXConnectionScope())
                    {
                        //get the DAC extension...
                        var polineext = PXCache<POLine>.GetExtension<POLineExt>(poline);
    
                        if (poline.TaskID == null)
                            polineext.UsrAccountGroup = null;
    
                            //Get the task CD from the Task ID...
                            var pmtask = (PMTask)PXSelect<PMTask, Where<PMTask.taskID, Equal<Required<PMTask.taskID>>>>.Select(Base, poline.TaskID);
                        if (pmtask != null)
                        {
                            //Get the account group based on the grid's ProjectTask CD...
                            var pag = (PMAccountGroup)PXSelect<PMAccountGroup, Where<PMAccountGroupExt.usrProjectTask, Equal<Required<PMAccountGroupExt.usrProjectTask>>>>.Select(Base, pmtask.TaskCD);
                            if (pag != null)
                            {
                                polineext.UsrAccountGroup = pag.GroupID;
                            }
                            else
                            {
                                polineext.UsrAccountGroup = null;
                            }
                        }
                    }
                }
            }
    
    [Account(typeof(POLine.branchID), DisplayName = "Account", Visibility = PXUIVisibility.Visible, Filterable = false, DescriptionField = typeof(Account.description), AvoidControlAccounts = true)]
            [PXRestrictor(typeof(Where<Current<POLineExt.usrAccountGroup>, IsNull, Or<Account.accountGroupID, Equal<Current<POLineExt.usrAccountGroup>>>>), null)]
            [PXDefault(PersistingCheck = PXPersistingCheck.Nothing)]
            protected void POLine_ExpenseAcctID_CacheAttached(PXCache sender) { }