Is there a way to iterate through the fields in a row of a PXResultSet?

Viewed 136

Is it possible to use a foreach loop in a BLC to iterate through the fields of a PXResultSet to get the FieldNames?

Is this doable? I can't seem to find a good way.

Thanks...

1 Answers

The PXResultset records are selected from a view. You can get the field names from the View.

Here's a full example:

public class SOOrderEntry_Extension : PXGraphExtension<SOOrderEntry>
{
    public override void Initialize()
    {
        // Get field list from data view
        var dataView = new PXSelect<SOOrder>(Base);
        string fieldNames = string.Join(",", GetFieldNames(dataView.View, Base.Caches));
          
        // You don't need result set to get field names
        PXResultset<SOOrder> resultSet = dataView.Select();
                   
        throw new PXException(fieldNames);
    }

    public string[] GetFieldNames(PXView view, PXCacheCollection caches)
    {
        var list = new List<string>();
        var set = new HashSet<string>();

        foreach (Type t in view.GetItemTypes())
        {
            if (list.Count == 0)
            {
                list.AddRange(caches[t].Fields);
                set.AddRange(list);
            }
            else
            {
                foreach (string field in caches[t].Fields)
                {
                    string s = String.Format("{0}__{1}", t.Name, field);

                    if (set.Add(s))
                    {
                        list.Add(s);
                    }
                }
            }
        }

        return list.ToArray();
    }
}

When run, this example will show the fields names used in the data view in Sales Order screen SO301000 as an exception. enter image description here

Field names are contained in Cache object. If you really need to get field names from PXResultset you need to iterate the cache types in the result set.

Example for first DacType (0) of result set:

public class SOOrderEntry_Extension : PXGraphExtension<SOOrderEntry>
{
    public override void Initialize()
    {
        var dataView = new PXSelect<SOOrder>(Base);
        PXResultset<SOOrder> resultSet = dataView.Select();
        
        foreach (PXResult result in resultSet)
        {
            Type dacType = result.GetItemType(0);

            foreach (var field in Base.Caches[dacType].Fields)
                PXTrace.WriteInformation(field);
        }
    }
}
Related