Skip to main content
Solved

Require file attachment by user role


I am trying to create a customization that requires an attachment on the Bills and Adjustment screen for certain roles. 

How should I define the roles that require an attachment and check if that user is in the role?

How do I check for attachments? 

 

Here is the base code I'm using 

namespace PX.Objects.AP
{
    public class APInvoiceEntry_Extension : PXGraphExtension<APInvoiceEntry>
    {
        #region Event Handlers
        private void _(Events.RowPersisting<APInvoice> e)
        {
            if (e.Row == null) return;

            // Get the current user roles for the AP301000 screen
            PXRoleList rolesList = PXAccess.GetRoles("AP301000");

            // Extract role IDs  using the Roles property
           var roles = PXAccess.GetRoles("AP301000");

            // Define roles that require an attachment
            string[] requiredRoles = { "APClerk", "FinanceManager" };

            // Check if the user has any of the required roles
            if (requiredRoles.Any(role => roles.Contains(role)))
            {
                // Check for attachments
                bool hasAttachments = SelectFrom<NoteDoc>
                    .Where<NoteDoc.FileID.IsEqual<@P.AsGuid>>
                    .View.Select(Base, e.Row.NoteID).Any();

                if (!hasAttachments)
                {
                    throw new PXException("Attachment Required","An attachment is required for this bill.", MessageButtons.OK, MessageIcon.Error);
                }
            }
        }
        #endregion
    }
}

Best answer by darylbowman

nawrasalb wrote:

How do I correctly join in the FileID , it says fileID does not exist in the ‘NoteDoc’

// Check if there are any attachments for this document
bool hasAttachments = SelectFrom<NoteDoc>
    .Where<NoteDoc.FileID.IsEqual<@P.AsGuid>>
    .View.Select(Base, e.Row.NoteID).Any();

 

Same thing here. In BQL statements, the properties need to be camel-cased:

.Where<NoteDoc.fileID.IsEqual<@P.AsGuid>>

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

9 replies

Forum|alt.badge.img+6
  • Captain II
  • 553 replies
  • March 7, 2025

I found a different function that allows you to make a call to see if the user has been assigned a specific role:

public static bool IsUserAMemberOf(string roleName, PXGraph graph)
{
  var myUserName = graph.Accessinfo.UserName;

  UsersInRoles assigned = PXSelect<UsersInRoles,
      Where<UsersInRoles.username, Equal<Required<Users.username>>, And<UsersInRoles.rolename, Equal<Required<UsersInRoles.rolename>>>>>
      .Select(graph, myUserName, roleName);

  if (assigned == null)
    return false;

  return true;
}

I call it like this:

bool HasApproveCommEditRole = IsUserAMemberOf("Can Edit Commission", Base);

It’s an easy way to just give a user a role to enable role specific functionality.


Nilkanth Dipak
Varsity II
Forum|alt.badge.img+10

Hi ​@nawrasalb,

I believe ​@Django’s suggestion is more specific for finding the user from a specific role.

Hope, it helps!


  • Author
  • Freshman I
  • 5 replies
  • March 7, 2025

Thank you ​@Django and ​@Nilkanth Dipak

The function above like Nilkanth stated is more for finding a user in the role and not making specific roles required on that screen. 

What function would I use to make the specific roles require an attachment, and check that the user is in the required role defined.  

 

How do I correctly join in the FileID , it says fileID does not exist in the ‘NoteDoc’

// Check if there are any attachments for this document
                     bool hasAttachments = SelectFrom<NoteDoc>
                    .Where<NoteDoc.FileID.IsEqual<@P.AsGuid>>
                    .View.Select(Base, e.Row.NoteID).Any();

                if (!hasAttachments)

 

 


Nilkanth Dipak
Varsity II
Forum|alt.badge.img+10

Hi ​@nawrasalb,

Please try below code snippet.

  bool hasAttachments = SelectFrom<NoteDoc>
                    .Where<NoteDoc.NoteID.IsEqual<@P.AsGuid>>
                    .View.Select(Base, e.Row.NoteID).Any();

                if (!hasAttachments)
                {
                    throw new PXException("An attachment is required for this bill.");
                }

Hope, it helps!


  • Author
  • Freshman I
  • 5 replies
  • March 7, 2025
@Nilkanth Dipak - Still getting the same error just on the NoteID now.error CS0426: The type name 'NoteID' does not exist in the type 'NoteDoc'

 

And on this line I am getting another error.
            bool userInRequiredRole = requiredRoles.Any(role => userRoles.Contains(role));

error CS0103: The name 'userRoles' does not exist in the current context

Nilkanth Dipak
Varsity II
Forum|alt.badge.img+10

Hi ​@nawrasalb,

Have you added using PX.Data; ?
 


  • Author
  • Freshman I
  • 5 replies
  • March 7, 2025

Hi ​@Nilkanth Dipak ,

 yes, I have that added.


darylbowman
Captain II
Forum|alt.badge.img+13
nawrasalb wrote:
Error CS0426: The type name 'NoteID' does not exist in the type 'NoteDoc'

The code should be NoteDoc.noteID not NoteDoc.NoteID.


darylbowman
Captain II
Forum|alt.badge.img+13
  • 1690 replies
  • Answer
  • March 10, 2025
nawrasalb wrote:

How do I correctly join in the FileID , it says fileID does not exist in the ‘NoteDoc’

// Check if there are any attachments for this document
bool hasAttachments = SelectFrom<NoteDoc>
    .Where<NoteDoc.FileID.IsEqual<@P.AsGuid>>
    .View.Select(Base, e.Row.NoteID).Any();

 

Same thing here. In BQL statements, the properties need to be camel-cased:

.Where<NoteDoc.fileID.IsEqual<@P.AsGuid>>


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