Search code examples
c#acumaticaacumatica-kb

How do you mark a field as required in Acumatica?


I have a requirement to make some fields in the system required. I see that on the attribute listing in customizations, there is a 'Required' check box for the on screen properties.

Are there any other steps that need to be performed in a field attribute to make it required and be on a persistent check?


Solution

  • The best way used to set a field as required and to enforce it, is to use PXDefaultAttribute. More on that later, first I will talk about the layout editor Required setting.

    Layout editor Required setting

    It is possible to mark a field as required without forcing it to have a value. This can be seen as a red star next to the field.

    enter image description here

    To do this, customize your existing screen and follow these 3 easy steps

    1. Select the field from the layout editor.
    2. Make sure the Filter button is unselected, which will show more settings that can be applied.
    3. Set the Required field to True.

    enter image description here

    Required field using PXDefaultAttribute

    Before setting the PXDefaultAttribute on a field, it is advised to explore how that field is already defined.

    Attributes at DAC level

    The first place to have a look is at DAC level. Using the Element Inspector (shortcut Ctrl + Alt + Click) on a field will give us the information we need. Note the Data Field and then click Actions and View Data Class Source...

    enter image description here

    On this screen, we need to make sure that the field doesn't already have a PXDefaultAttribute.

    enter image description here

    In most case, settings it on DAC level is considered a bad pratice because every screen that refers to this field will validate that it contains a value. This is better done on a per-screen basis by using a CacheAttached.

    Attributes at Graph level using CacheAttached

    Now we need to check the graph code to see if a PXDefaultAttribute exists already. Again we use the Element Inspector but this time we click on View Business Logic Source...

    enter image description here

    We need to find a function with this signature and check [PXDefault] can be found over it:

    protected void SOOrder_OrderDesc_CacheAttached(PXCache sender) { }
    

    If there is nothing, you are can apply your customization with peace of mind.

    SOOrder Code Example

    Finally, to enforce the required field at graph level, you follow this pattern.

    [PXMergeAttributes(Method = MergeMethod.Append)]
    [PXDefault(PersistingCheck = PXPersistingCheck.NullOrBlank)]
    protected void SOOrder_OrderDesc_CacheAttached(PXCache sender) { }
    

    PXMergeAttributes MergeMethod has 3 possible values : Append, Merge, Replace. Basically this tells the framework how to handle conflicts between attributes found on this DAC field.

    PXDefault PersistingCheck also has 3 values : Nothing, Null, NullOrBlank. Empty is used when the field should not be required but should have a default value. This isn't usefull in the context of this post. Null and NullOrBlank determine the validation to be done, where NullOrBlank is mostly usefull for strings.

    More information can be found on these subjects at the links I added at the bottom of this post.

    This result of the previous code would give you this when trying to save without a Description value.

    enter image description here

    Using layout editor Required setting in conjunction with PXDefaultAttribute

    The PXDefaultAttribute will validate data if you set it correctly, but in some situation the UI will not reflect that the field is required. A good example of this is PXNumberEdit element, even with a PXDefault, the red star will not appear next to the field. If you really need the red star, you can set the Required setting to true as describe at the top of this article. Another way would be to set the Required parameter to true in the PXUIField attribute of your field.

    e.g. [PXUIField(DisplayName = "My Value", Required = true)]

    Here are informative links to expand on this subject

    How to make a field mandatory

    Append and Replace of DACs Attributes / PXMergeAttributes

    PXDefault API Reference