Skip to main content
Solved

How to make a field required

  • March 18, 2025
  • 2 replies
  • 157 views

Hello - we are trying to figure out how to make it so that an attachment is required for each expense receipt that is created. How would we make that a required field? 

Best answer by DipakNilkanth

Hi ​@ahafemann,

I believe you will need to create a customization to achieve this.

You can write the logic in the RowPersisting event and use a BQL query to fetch the attachment for the current record.

The code snippet below will help you head in the right direction:

using PX.Data;
using PX.Objects.EP;
using PX.SM;
using System.Linq;

namespace test
{
public class ExpenseReceiptEntry_Extension : PXGraphExtension<ExpenseReceiptEntry>
{
protected void EPExpenseReceipt_RowPersisting(PXCache cache, PXRowPersistingEventArgs e)
{
var row = (EPExpenseReceipt)e.Row;
if (row == null) return;

// Check if the document is being inserted or updated
if (e.Operation != PXDBOperation.Insert && e.Operation != PXDBOperation.Update)
return;

// Get the NoteID (attachment link)
Guid? noteID = row.NoteID;

if (noteID == null)
{
PXTrace.WriteError($"Expense Receipt {row.ReceiptNbr} has no NoteID.");
return;
}

// Check for attachments
var hasAttachments = PXSelect<UploadFileRevision,
Where<UploadFileRevision.noteID, Equal<Required<UploadFileRevision.noteID>>>>
.Select(Base, noteID)
.Any();

if (!hasAttachments)
{
cache.RaiseExceptionHandling<EPExpenseReceipt.receiptNbr>(
row,
row.ReceiptNbr,
new PXSetPropertyException("An attachment is required for this expense receipt.", PXErrorLevel.Error)
);

throw new PXRowPersistingException(
nameof(EPExpenseReceipt.receiptNbr),
row.ReceiptNbr,
"An attachment is required for this expense receipt."
);
}
}
}
}

 

2 replies

Samvel Petrosov
Jr Varsity II
Forum|alt.badge.img+8

You will have to have/develop a customization checking the file attachments and showing an error message if there are none.

Acumatica allows making “fields” required with no-code/low-code customizations, but in this case there is field tracking the attachments on the records itself that you can make required.

Also, a complication to this validation can be to identify the file that you actually want to track, as technically any attachment (file/pdf/image/etc...) is an attachment. So the program also will need to somehow understand if the correct/required file is attached. 


DipakNilkanth
Pro III
Forum|alt.badge.img+13
  • Pro III
  • Answer
  • March 19, 2025

Hi ​@ahafemann,

I believe you will need to create a customization to achieve this.

You can write the logic in the RowPersisting event and use a BQL query to fetch the attachment for the current record.

The code snippet below will help you head in the right direction:

using PX.Data;
using PX.Objects.EP;
using PX.SM;
using System.Linq;

namespace test
{
public class ExpenseReceiptEntry_Extension : PXGraphExtension<ExpenseReceiptEntry>
{
protected void EPExpenseReceipt_RowPersisting(PXCache cache, PXRowPersistingEventArgs e)
{
var row = (EPExpenseReceipt)e.Row;
if (row == null) return;

// Check if the document is being inserted or updated
if (e.Operation != PXDBOperation.Insert && e.Operation != PXDBOperation.Update)
return;

// Get the NoteID (attachment link)
Guid? noteID = row.NoteID;

if (noteID == null)
{
PXTrace.WriteError($"Expense Receipt {row.ReceiptNbr} has no NoteID.");
return;
}

// Check for attachments
var hasAttachments = PXSelect<UploadFileRevision,
Where<UploadFileRevision.noteID, Equal<Required<UploadFileRevision.noteID>>>>
.Select(Base, noteID)
.Any();

if (!hasAttachments)
{
cache.RaiseExceptionHandling<EPExpenseReceipt.receiptNbr>(
row,
row.ReceiptNbr,
new PXSetPropertyException("An attachment is required for this expense receipt.", PXErrorLevel.Error)
);

throw new PXRowPersistingException(
nameof(EPExpenseReceipt.receiptNbr),
row.ReceiptNbr,
"An attachment is required for this expense receipt."
);
}
}
}
}