Solved

Make conditionally visible custom field required

  • 8 June 2023
  • 6 replies
  • 348 views

Userlevel 4
Badge

I am attempting to make a custom field on a sales order required. This custom field is only visible when OrderType is “NC”.

I am struggling with how to determine if the field is visible and if it is null or blank.
I have tried several different methods I have found from various sources. The code below is totally wrong… but is where I got to the point of asking for help…. This code is in my DAC extension.
 
 

        protected void _(Events.RowPersisting<SOOrderExt> e)
{
var isVisible = SOOrder.orderType.IsEqual<ncOrder>;
if (BqlString.Field<usrNoChargeDept> == null && isVisible) {
throw new PXException(Messages.RequiredNCDept);
}
}

 ncOrder is:

  public class ncOrder : PX.Data.BQL.BqlString.Constant<ncOrder>
{
public ncOrder() : base("NC") { }
}

RequiredNCDept is a string in a file with other messages.

I attempted the solution here: 

But ran into the issue of finding when the field is visible.

How can I make the field required only when visible?

icon

Best answer by darylbowman 8 June 2023, 21:30

View original

6 replies

Userlevel 7
Badge +5

You want to put this in RowSelected most of the time.

PXUIFieldAttribute.SetVisible<TheDAC.theField>(cache, row, true_or_false);

Userlevel 4
Badge

@Django Thank you. That line only sets visibility, right? How do I then read/get the visibility? And, then use the visibility to set required/not required?

Userlevel 7
Badge +5

Yes, that only does the visibility.

Is the challenge that you don’t know if the field is visible or not when you’re in the RowPersisting event? In theory you should have full access to the record’s fields to determine if the criteria has been met for the fields visible/hidden state and you can test for the same condition.

 

Badge +11

Is this question related to this one?

If this is the same field and you want to conditionally make the field visible AND make it required when it’s visible, you can use PXUIRequired on the DAC field in the same way as PXUIVisible, like this:

#region CustomField
[PXDBString(IsUnicode = true, InputMask = "")]
[PXUIVisible(typeof(Where<SOOrder.orderType.IsEqual<ncOrder>>))]
[PXUIRequired(typeof(Where<SOOrder.orderType.IsEqual<ncOrder>>))]
[PXUIField(DisplayName = "Custom Field")]
public virtual string CustomField { get; set; }
public abstract class customField : PX.Data.BQL.BqlString.Field<customField> { }
#endregion

 

Userlevel 4
Badge

@darylbowman :-) yes - related, same project.
 Thank you again my friend! I did have to add [PXDefault]  to the field (I had removed it because it caused the field to be required when hidden)

I REALLY appreciate your help. Thanks so much!
 

Is there somewhere I can find these attributes(not sure what to call them?)?

Badge +11

There may be, but I learned them by seeing other code where people used them. They are remarkably useful. Worlds easier in many cases, than having to define an event just to do the simple things.

 

The third musketeer is PXUIEnabled. Works exactly the same as the other two.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved