Skip to main content
Solved

Make a field required but only in one company

  • November 26, 2024
  • 4 replies
  • 44 views

We have a custom field that we’ve added to a page and the client wants to make it required, but only require it based on the company they are in. So it would not be required if they are in Company A, Company B, or Company C but it would be required if they are in Company D. Is there an easy way to accomplish something like this?

Best answer by Django

The RowSelected event will work perfectly for this. You can decide how you want to determine the logic for making the field required/not required and call this. I’d be tempted to add a field to the relevant Preferences screen that toggles this behaviour on and off. That way if they add a new company, you don’t need to recompile anything - just check, or uncheck a box.
 

protected virtual void _(Events.RowSelected<SOOrder> e, PXRowSelected baseHandler)
{
  baseHandler?.Invoke(e.Cache, e.Args);

  SOOrder row = e.Row as SOOrder;
  if (row == null) return;

  bool flagVariable = MethodUsedToDetermineRequiredOrNot();

  PXUIFieldAttribute.SetRequired<SOOrder.usrYourField>(e.Cache, flagVariable);

}

I also have this method that I found within the Acumatica code that I use to check to make sure that all required fields have been filled so I can control that better:

if (CheckRequiredFieldsFilled(Base.Document.Cache, row) == false)
{
  PopupNoteManager.RegisterText(Base.Document.Cache, row, "OrderNbr", "Please enter the missing field data.");
}


private static bool CheckRequiredFieldsFilled(PXCache aCache, IBqlTable aFilter)
{
  bool allFilled = true;
  foreach (var field in aCache.BqlFields)
  {
    PXFieldState state = aCache.GetStateExt(aFilter, field.Name) as PXFieldState;
    if (state != null)
    {
      object value = aCache.GetValue(aFilter, field.Name);
      if (state.Required == true && value == null)
      {
        aCache.RaiseExceptionHandling(field.Name, aFilter, null,
          new PXSetPropertyException(Messages.RequiredField));
        allFilled = false;
      }
    }
  }
  return allFilled;
}

 

View original
Did this topic help you find an answer to your question?

4 replies

Forum|alt.badge.img+6
  • Captain II
  • 580 replies
  • Answer
  • November 26, 2024

The RowSelected event will work perfectly for this. You can decide how you want to determine the logic for making the field required/not required and call this. I’d be tempted to add a field to the relevant Preferences screen that toggles this behaviour on and off. That way if they add a new company, you don’t need to recompile anything - just check, or uncheck a box.
 

protected virtual void _(Events.RowSelected<SOOrder> e, PXRowSelected baseHandler)
{
  baseHandler?.Invoke(e.Cache, e.Args);

  SOOrder row = e.Row as SOOrder;
  if (row == null) return;

  bool flagVariable = MethodUsedToDetermineRequiredOrNot();

  PXUIFieldAttribute.SetRequired<SOOrder.usrYourField>(e.Cache, flagVariable);

}

I also have this method that I found within the Acumatica code that I use to check to make sure that all required fields have been filled so I can control that better:

if (CheckRequiredFieldsFilled(Base.Document.Cache, row) == false)
{
  PopupNoteManager.RegisterText(Base.Document.Cache, row, "OrderNbr", "Please enter the missing field data.");
}


private static bool CheckRequiredFieldsFilled(PXCache aCache, IBqlTable aFilter)
{
  bool allFilled = true;
  foreach (var field in aCache.BqlFields)
  {
    PXFieldState state = aCache.GetStateExt(aFilter, field.Name) as PXFieldState;
    if (state != null)
    {
      object value = aCache.GetValue(aFilter, field.Name);
      if (state.Required == true && value == null)
      {
        aCache.RaiseExceptionHandling(field.Name, aFilter, null,
          new PXSetPropertyException(Messages.RequiredField));
        allFilled = false;
      }
    }
  }
  return allFilled;
}

 


  • Author
  • 7 replies
  • November 26, 2024

I originally thought to use the preferences as well, however I believe those are by tenant and not by company. So the next logical place would be to just add it on the Companies page instead I guess?


Forum|alt.badge.img+6
  • Captain II
  • 580 replies
  • November 26, 2024

Yeah - sorry, late in the day for my brain. Put it where it makes the most sense for the setting.


  • Author
  • 7 replies
  • November 26, 2024

No biggie, I was just curious if there was an easier way to do it but this makes sense.

Thanks!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings