Skip to main content
Answer

How to use PXSelector base on condition

  • May 2, 2025
  • 3 replies
  • 128 views

Forum|alt.badge.img

In Employee Time Activity screen (EP307000) we can select project and project task in grid area.

Now we need to restrict project task selection base on login user’s roll.

In other word if login user have XYZ role then he can able to view only those task which contains “Labor” word in it.

 

So, basically PXSelector should be dynamic.

Best answer by darylbowman

This is a complicated-enough requirement that I think your best bet would be to create a custom selector for ProjectTasks. If you inherit PXCustomSelectorAttribute you can use the GetRecords() method to filter out records which you don’t want to return. These can get complicated, but here’s a simple example of how I used one:

public class NiceLabelLabelSelectorAttribute : PXCustomSelectorAttribute
{
public NiceLabelLabelSelectorAttribute()
: base(typeof(NiceLabelLabel.itemPath))
{
DescriptionField = typeof(NiceLabelLabel.labelName);
}

protected virtual IEnumerable GetRecords()
{
var labelSetupGraph = PXGraph.CreateInstance<RXLabelSetupMaint>();
var labelSetup = labelSetupGraph.Setup.SelectSingle();

var labels = NiceLabelFunctions.GetLabels(labelSetup?.SubscriptionKey, labelSetup?.LabelFolderPath);
if (labels is null)
throw new PXException("No labels were returned from the NiceLabel service");

foreach (NiceLabelLabel label in labels)
{
yield return label;
}
}
}

 

3 replies

Forum|alt.badge.img+5

Hi ​@bjani23,

You can apply a PXRestrictor or define a custom selector attribute for the Project Task field.
Make sure to implement this before the CacheAttached event is triggered for the field. This ensures that your restriction or custom logic is applied at the time the field is initialized and rendered on the screen.

Hope, it helps!


Forum|alt.badge.img
  • Author
  • Jr Varsity III
  • May 2, 2025

Hi ​@bjani23,

You can apply a PXRestrictor or define a custom selector attribute for the Project Task field.
Make sure to implement this before the CacheAttached event is triggered for the field. This ensures that your restriction or custom logic is applied at the time the field is initialized and rendered on the screen.

Hope, it helps!

@Ankita Tayana  For task syntax something like 

[PXDefault(typeof(Search<PMTask.taskID, Where<PMTask.projectID, Equal<Current<EPActivityApprove.projectID>>, And<PMTask.isDefault, Equal<True>>>>), PersistingCheck = PXPersistingCheck.Nothing)]
[ProjectTask(typeof(EPActivityApprove.projectID), BatchModule.TA, DisplayName = "Project Task")]
[PXFormula(typeof(Switch<
Case<Where<Current2<EPActivityApprove.projectID>, Equal<NonProject>>, Null>,
EPActivityApprove.projectTaskID>))]
protected virtual void EPActivityApprove_ProjectTaskID_CacheAttached(PXCache cache)
{

}

While role can access by

 PXSelect<UsersInRoles, Where<UsersInRoles.username, Equal<Current<AccessInfo.userName>>,
And<UsersInRoles.rolename, Equal<Required< UsersInRoles.rolename>>>>>.Select(this.Base, "Field-Level Audit");

So how can I use PXRestrictor?

I mean I am not following syntax for that.

 

Basically if user have "Field-Level Audit" role then we need to display only those task which contains labor word in it.

 

 


darylbowman
Captain II
Forum|alt.badge.img+15

This is a complicated-enough requirement that I think your best bet would be to create a custom selector for ProjectTasks. If you inherit PXCustomSelectorAttribute you can use the GetRecords() method to filter out records which you don’t want to return. These can get complicated, but here’s a simple example of how I used one:

public class NiceLabelLabelSelectorAttribute : PXCustomSelectorAttribute
{
public NiceLabelLabelSelectorAttribute()
: base(typeof(NiceLabelLabel.itemPath))
{
DescriptionField = typeof(NiceLabelLabel.labelName);
}

protected virtual IEnumerable GetRecords()
{
var labelSetupGraph = PXGraph.CreateInstance<RXLabelSetupMaint>();
var labelSetup = labelSetupGraph.Setup.SelectSingle();

var labels = NiceLabelFunctions.GetLabels(labelSetup?.SubscriptionKey, labelSetup?.LabelFolderPath);
if (labels is null)
throw new PXException("No labels were returned from the NiceLabel service");

foreach (NiceLabelLabel label in labels)
{
yield return label;
}
}
}