Question

Make attachment field mandatory


Userlevel 4
Badge

Hello everyone,
I have tried to make the attachment field mandatory but was unable to.
I need to make it mandatory once the user approve the requisition.
 

Kindly assist 
Thank you for continuous support.
Any kind of response is always highly appreciated.


11 replies

Badge +10

I don't think you can make a file mandatory in the same way that you can make a field, but you should be able to check the Status value in an Event (perhaps RowPersisting) and throw a PXException if a file is not attached.

Userlevel 4
Badge

I don't think you can make a file mandatory in the same way that you can make a field, but you should be able to check the Status value in an Event (perhaps RowPersisting) and throw a PXException if a file is not attached.


 


​​​​​​​protected void EPApproval_RowPersisted(PXCache cache, PXRowPersistedEventArgs e, PXRowPersisted InvokeBaseHandler)
    {
        if(InvokeBaseHandler != null)
        InvokeBaseHandler(cache, e);
      var row = (EPApproval)e.Row;

if(row.NoteID == null){

 throw new PXException("Kindly attach the AFE");

}


}

Tried to do this but  dint work.

Badge +10

I meant RowSelected of the parent record.

You should be able to check for the EPApprovals during the save of the parent record.

Userlevel 7
Badge +8

You are checking the wrong field from the wrong table. For “Notes” you will need to look into “Note” DAC where Note.NoteID = EPApproval.NoteID and see if “NoteText” has value and for “Files” you will need to look into NoteDoc WHERE NoteDoc.NoteID = EPApproval.NoteID and see if any record exists there.

RowPersisted or any other event handler in your case will not help because the EPApproval record is Auto Generated initially when a document falls into Approval Map. If you put this validation on any event handler of EPApproval DAC your system will stop working. Even if you use RowUpdated, there are some internal records created or auto approval happens that will cause the same problem for you.

You might be able to play with it and incorporate the “Status” field as well as “IsPreApproved” which is used for internal stuff but I think you will face some exception-handlings.

You will need to develop this on your document Graph. What you are trying to make work, won't be so straightforward although seems so. I have played a lot with Approvals and it has a very complicated structure to override as well.

Badge +10

I just realized I said RowSelected in my second answer. I meant RowPersisting of the parent record.

Badge +10

This works on Invoices

protected virtual void _(Events.RowPersisting<ARInvoice> e)
{
ARInvoice invoice = e.Row;
if (invoice is null) return;

var invoiceGraph = (ARInvoiceEntry)e.Cache.Graph;

if (invoice.Status == ARDocStatus.PendingApproval)
{
PX.Objects.EP.EPApproval approval = invoiceGraph.Approval.Select()?.FirstTableItems?.FirstOrDefault();
if (approval is object)
{
var files = PXNoteAttribute.GetFileNotes(e.Cache, approval);
if (!(files?.Any() ?? false))
Base.Document.Ask("Attachment Required", "The required files are missing from Approvals", MessageButtons.OK, MessageIcon.Error);
}
}
}

I had to use a dialog because throwing an exception will prevent it from saving, and attaching a file requests to save, so it ends up in a loop.

Userlevel 7
Badge +8

Nice work @darylbowman it definitely gives a great head start. All it needs is a little bit of fine-tuning to check for the Pending Approval records in EPApproval and be triggered only when EPApproval Status changes from Pending to Approved/Rejected. Do not forget to exclude the IsPreApproved from verifications. Cheers pal

Userlevel 4
Badge

All these responses are GREAT!!
@aaghaei @darylbowman thanks alot

Userlevel 4
Badge

This works on Invoices

protected virtual void _(Events.RowPersisting<ARInvoice> e)
{
ARInvoice invoice = e.Row;
if (invoice is null) return;

var invoiceGraph = (ARInvoiceEntry)e.Cache.Graph;

if (invoice.Status == ARDocStatus.PendingApproval)
{
PX.Objects.EP.EPApproval approval = invoiceGraph.Approval.Select()?.FirstTableItems?.FirstOrDefault();
if (approval is object)
{
var files = PXNoteAttribute.GetFileNotes(e.Cache, approval);
if (!(files?.Any() ?? false))
Base.Document.Ask("Attachment Required", "The required files are missing from Approvals", MessageButtons.OK, MessageIcon.Error);
}
}
}

I had to use a dialog because throwing an exception will prevent it from saving, and attaching a file requests to save, so it ends up in a loop.

I implemented this on Requisition but the system keeps prompting the dialog box even after attaching the file.
what could be the reason why?

 


code:

 

protected virtual void _(Events.RowPersisting<RQRequisition> e)
{
RQRequisition requisition = e.Row;
RQRequisitionExt reqyyy = PXCache<RQRequisition>.GetExtension<RQRequisitionExt>(requisition);
if (requisition is null) return;

var requisitionGraph = (RQRequisitionEntry)e.Cache.Graph;

if (requisition.Status == RQRequisitionStatus.Bidding)
{
PX.Objects.EP.EPApproval approval = requisitionGraph.Approval.Select()?.FirstTableItems?.FirstOrDefault();
if (approval is object)
{
var files = PXNoteAttribute.GetFileNotes(e.Cache, approval);
if (!(files?.Any() ?? false) )

//&& approval.WorkgroupID != null && requisition.Approved == true && reqyyy.UsrAFN != null

Base.Document.Ask("Attachment Required", "The required AFE is missing from Approvals", MessageButtons.OK, MessageIcon.Error);
}
}
}

 

Badge +10

Make a variable of 

bool hasFiles = files?.Any()

And then the if statement like

if (!(hasFiles ?? false))

 

I ran into an issue in another project with the coalescing making the value always false and I think this is how I fixed it.

Badge +10

Looking again, I realize I made that bool non nullable. It should be:

bool? hasFiles = files?.Any()

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