Skip to main content
Solved

Changing default sort order of Data View has odd results


darylbowman
Captain II
Forum|alt.badge.img+13

I am trying to change the default sort order of the details on Approve Bills for Payment. I’ve read these articles:

The APApproveBills graph has a data view and a data view delegate, but according to the above articles, it seems like the following code should achieve what I need:

namespace PX.Objects.AP
{
    public class APApproveBills_Extension : PXGraphExtension<PX.Objects.AP.APApproveBills>
    {
        public override void Initialize()
        {
            Base.APDocumentList.OrderByNew<OrderBy<Vendor.acctName.Asc>>();
        }
    }
}

This does change the sort, but the result is not what I am expecting.

 

Can someone explain why this is happening and/or how to fix it?

Best answer by Keith Richardson

I was able to get this accomplished through a DAC extension adding the Vendor.AcctCD to the APInvoice, and then using your initial idea to sort by that new field. It works with the filters!

 


    public class APInvoiceApproveBillsExt : PXCacheExtension<APInvoice>
    {

        #region UsrVendorCD 
        [PXString]
        [PXFormula(typeof(Selector<APInvoice.vendorID, Vendor.acctCD>))]
        [PXUIField(DisplayName = "Vendor")]
        public virtual String UsrVendorCD { get; set; }
        public abstract class usrVendorCD : PX.Data.BQL.BqlString.Field<usrVendorCD> { }
        #endregion
    }
    public class APApproveBillsExt : PXGraphExtension<APApproveBills>
    {
        public override void Initialize()
        {
            Base.APDocumentList.OrderByNew<OrderBy<APInvoiceApproveBillsExt.usrVendorCD.Asc>>();
        }
    }

 

View original
Did this topic help you find an answer to your question?

11 replies

aaghaei
Captain II
Forum|alt.badge.img+9
  • Captain II
  • 1176 replies
  • May 27, 2023

@darylbowman when you have Delegate, it overrides all predecessors which includes initialization.

you will need to override the delegate. If you didn’t  have Delegate then initialization override would do the work. 


darylbowman
Captain II
Forum|alt.badge.img+13

I suspected this may be the case. So I overrode the data view and delegate like this, including copying a private method (not shown) which actually returns the data, like this (summary below): 

namespace PX.Objects.AP
{
	public class APApproveBills_Extension : PXGraphExtension<APApproveBills>
	{
		[PXFilterable]
		public PXSelect<APInvoice> APDocumentList;

		protected virtual IEnumerable apdocumentlist()
		{
			ApproveBillsFilter filter = Base.Filter.Current;

			if (filter != null && filter.SelectionDate != null)
			{
				DateTime PayInLessThan = ((DateTime)filter.SelectionDate).AddDays(filter.PayInLessThan.GetValueOrDefault());
				DateTime DueInLessThan = ((DateTime)filter.SelectionDate).AddDays(filter.DueInLessThan.GetValueOrDefault());
				DateTime DiscountExpiresInLessThan = ((DateTime)filter.SelectionDate).AddDays(filter.DiscountExpiresInLessThan.GetValueOrDefault());

				//PXView view = new PXView(Base, false, BqlCommand.CreateInstance(getAPDocumentSelect(false)));
				BqlCommand command = BqlCommand.CreateInstance(getAPDocumentSelect(false));
				command.OrderByNew<OrderBy<Vendor.acctCD.Asc>>();
				PXView view = new PXView(Base, false, command);

				foreach (PXResult<APInvoice> res in
				  view.Graph.QuickSelect(view.BqlSelect, new object[] { PayInLessThan, DueInLessThan, DiscountExpiresInLessThan }, null, false))
				{
					APInvoice apdoc = res;

					if (string.IsNullOrEmpty(apdoc.PayTypeID))
					{
						try
						{
							APDocumentList.Cache.SetDefaultExt<APInvoice.payTypeID>(apdoc);
						}
						catch (PXSetPropertyException e)
						{
							APDocumentList.Cache.RaiseExceptionHandling<APInvoice.payTypeID>(apdoc, apdoc.PayTypeID, e);
						}
					}

					if (apdoc.PayAccountID == null)
					{
						try
						{
							APDocumentList.Cache.SetDefaultExt<APInvoice.payAccountID>(apdoc);
						}
						catch (PXSetPropertyException e)
						{
							APDocumentList.Cache.RaiseExceptionHandling<APInvoice.payAccountID>(apdoc, apdoc.PayAccountID, e);
						}
					}

					yield return apdoc;
				}
			}
		}
	}
}

 

I changed

PXView view = new PXView(Base, false, BqlCommand.CreateInstance(getAPDocumentSelect(false)));

to

BqlCommand command = BqlCommand.CreateInstance(getAPDocumentSelect(false));
command.OrderByNew<OrderBy<Vendor.acctCD.Asc>>();
PXView view = new PXView(Base, false, command);

which I believe should completely change the sort. Yet, the result is the same as simply changing the sort in the Initialize method, which is fantastically easier, but the result is still incorrect.

Have I done this wrong in some way?

@Nayan Mansinha @slesin 


Fernando Amadoz
Jr Varsity I
Forum|alt.badge.img+2

@darylbowman AFAIK, you will need to override the delegate if it’s originally defined in the main logic (as you are doing it), but it will be the actual Data view’s orderby the one that will define the order.

 

So try this:

  • Remove the logic from the Initialize() method.
  • Define your overridden data view + delegate like this:
		[PXFilterable]
		public PXSelectOrderBy<APInvoice, OrderBy<Asc<APInvoice.[yourSortField]>>> APDocumentList;

		protected virtual IEnumerable apdocumentlist()
		{
            (...) //your delegate definition
		}

 


darylbowman
Captain II
Forum|alt.badge.img+13

Thank you for your reply. 

Unfortunately, the result is the same. The sort is applied in some way, but not like I would expect:

 


andriikravetskyi35
Jr Varsity I
Forum|alt.badge.img+1

Hi guys,

  1. The view “APDocumentList” doesn’t use Vendor table:
  2. fix in graph extension:
   public class APApproveBillsExt : PXGraphExtension<PX.Objects.AP.APApproveBills>
    {
        public static bool IsActive() => true;

        [PXFilterable]
        public SelectFrom<APInvoice>.InnerJoin<BAccount>.On<APInvoice.vendorID.IsEqual<BAccount.bAccountID>>.OrderBy<BAccount.acctName.Asc>.View APDocumentList;

    }
  1. Result on my local Sales Demo DB:

 


darylbowman
Captain II
Forum|alt.badge.img+13

I guess this makes sense. Vendor is joined in the data view delegate, which I guess is why I thought I would be able to do that.

I tried it, with the data view delegate also defined, and the result was the same:

 

I realized that you hadn’t mentioned the data view delegate, so I removed that and tried again. It sorted the list fantastically. However, without the data view delegate, the whole APInvoice table is returned, which is not correct. The data view delegate is necessary to return the correct results.

So yet one more way to get a very close (and obviously working), but not correct result.

 

Also, since the result is exactly the same as my original attempt, it stands to reason that you can actually sort by tables that are not explicitly joined in the data view.


darylbowman
Captain II
Forum|alt.badge.img+13

Bump


darylbowman
Captain II
Forum|alt.badge.img+13
  • Author
  • 1593 replies
  • August 8, 2023

This behavior is also present in 23.102


Keith Richardson
Varsity II
Forum|alt.badge.img+2

I was able to get this accomplished through a DAC extension adding the Vendor.AcctCD to the APInvoice, and then using your initial idea to sort by that new field. It works with the filters!

 


    public class APInvoiceApproveBillsExt : PXCacheExtension<APInvoice>
    {

        #region UsrVendorCD 
        [PXString]
        [PXFormula(typeof(Selector<APInvoice.vendorID, Vendor.acctCD>))]
        [PXUIField(DisplayName = "Vendor")]
        public virtual String UsrVendorCD { get; set; }
        public abstract class usrVendorCD : PX.Data.BQL.BqlString.Field<usrVendorCD> { }
        #endregion
    }
    public class APApproveBillsExt : PXGraphExtension<APApproveBills>
    {
        public override void Initialize()
        {
            Base.APDocumentList.OrderByNew<OrderBy<APInvoiceApproveBillsExt.usrVendorCD.Asc>>();
        }
    }

 


darylbowman
Captain II
Forum|alt.badge.img+13
  • Author
  • 1593 replies
  • August 8, 2023

Fantastically simple solution that I would not have thought of otherwise. Thank you!


aaghaei
Captain II
Forum|alt.badge.img+9
  • Captain II
  • 1176 replies
  • August 8, 2023

Thank you for sharing your solution with the community @Keith Richardson 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings