Search code examples
c#acumatica

Acumatica Customization: Can I get value of fields in Cache using reflection?


I am writing code to call out to an external forms program from within an Acumatica Graph. I will want to pass some fields as parameters to the external URL.

To handle future changes, I would like to abstract the names of the fields, and the DACs to an external file, load the text values at runtime, and use them to determine which fields to send.

So, suppose I am extending the Field Services Appointments entry screen. The DAC and Fields I want to send, in text readable format is:

FSAppoinments
----------------
CustomerID
RefNbr
AppointmentID

I can get the Cache no problem. And I can get the correct DAC from the Cache. I can even get the names of the fields that are available. But how do I get the values of the fields?

Here's some POC code.

It gets me ALMOST there. But I can't figure out how to get the Field's VALUES.

    public PXAction<FSAppointment> myExtForms;
    [PXUIField(DisplayName = "MyXForm", MapEnableRights = PXCacheRights.Select)]
    [PXButton]
    public virtual void myExtForms()
    {
        List<String> fieldValues = new List<string>();

        // For now just code these in a List<>
        String DACNameForPassing = "FSAppointments";
        List<String> PassFields = new List<string>();
        PassFields.Add("RefNbr");
        PassFields.Add("AppointmentID");
        PassFields.Add("CustomerID");

        // Get All caches and figure out which one we want
        var allCaches = Base.Caches.Caches;
        foreach (PXCache PXC in allCaches )
        {
            if (PXC.GetName().ToUpper() != DACNameForPassing.ToUpper())
            {
                continue;
            }
           var cacheFields = PXC.Fields;
           for (int i = 0; i < cacheFields.Count; i++)
            {
                var onFld = cacheFields[i].ToString();
                string result = PassFields.FirstOrDefault(s => s.Contains(onFld));
                if (!String.IsNullOrEmpty(result))
                {
                    // So, I know this field is one I want BUT--
                    // HOW DO I GET THE FIELD VALUE???
                }
            }
        }
    }

Solution

  • This will cycle throw the rows in the view and pull out the values for the field in question

                         var mycontent = PXC.Cached;
                        foreach (var item in mycontent)
                        {
                            Object obj = item.GetType().GetProperty(onFld).GetValue(item);
                            PXTrace.WriteInformation( obj.ToString());
                        }