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?
Solved
How to make a field required
Best answer by DipakNilkanth
Hi
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."
);
}
}
}
}
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.