Loading ...

Find the custom fields in Acumatica using ScreenUtils class

We'll guide you on how to show all custom fields on a screen, even if they are not associated with your package. You can use the PX.Api.ScreenUtils class to locate any field. This class contains a method called GetScreenInfo() that returns a Content. Inside the Content object, there's a field named Containers, which you can use to find the required fields.

When creating custom fields, it is necessary to add the prefix 'usr'. So, we can use it to find them.

Let’s imagine we need to show all custom fields in the SOShipments screen and show their DisplayName and FieldName using the action called FieldSeeker.

As an example, we have added a field with “NextSODetails” DisplayName and “UsrSODetails” FieldName. So, by clicking on the FieldSeeker button, we should see those details about the custom field.

 

Here is how you can get the Container field:

    // Acuminator disable once PX1016 ExtensionDoesNotDeclareIsActiveMethod extension should be constantly active
    public class SOShipmentEntryExt : PXGraphExtension<SOShipmentEntry>
    {
        #region Actions
        public PXAction<SOShipment> FieldSeeker;
        [PXUIField(Visible = true, DisplayName = "Field Seeker")]
        [PXButton(CommitChanges = true)]
        public virtual IEnumerable fieldSeeker(PXAdapter adapter)
        {
            string customFields = string.Empty;
            foreach (Container container in ScreenUtils.GetScreenInfo(Base.Accessinfo.ScreenID, truetrue)?.Containers?
                         .Where(x => x.Fields?.Any(f => f.FieldName.StartsWith("Usr")) == true))
            {
                foreach (Field customField in container.Fields.Where(f => f.FieldName.StartsWith("Usr")))
                {
                    customFields += "<b>DisplayName:</b> " + customField.Name +
                                    "<br><b>FieldName:</b> " + customField.FieldName + "<br><br>";
                }
            }
            Base.Document.Ask(customFields, MessageButtons.OK);
​
            return adapter.Get();
        }
        #endregion
    }

 

In this code, we are using the foreach loop to locate all the Containers in the Containers array and check if there is any Container including any field with the ‘Usr’ prefix. Since the Containers field is an array, it can have multiple containers, so we need to use another loop to retrieve all the records in each container. Therefore, we have a nested loop within the main loop to achieve this.

In the nested foreach loop, we look for every field that starts with 'Usr' within the Container. Once we find them, we assign their Name and FieldName values to an empty string variable that was created earlier.

As we want to show them on the pop-up, we call the Ask method from the graph view and give the customFields variable to that method as a message parameter.

Here is the result:

Summary

ScreenUtils class may be used for findinging customly added fields in Acumatica, and allows you to build flexible applications, which will interop with other customizations.