Skip to main content
Answer

How to restrict order types to specific companies

  • January 27, 2025
  • 3 replies
  • 93 views

Forum|alt.badge.img+2

In Order Types preference screen, I have added a custom field as usrCompanyAcctCD. In that user will select a company for each order type. In the sales order screen, order type selector should only list down the order types which have current logged company id or null. to that I add a restrictor to Order Type as bellow. but it is not working.
 

[PXDBString(2, IsKey = true, IsFixed = true, InputMask=">aa")]
[PXDefault(SOOrderTypeConstants.SalesOrder, typeof(SOSetup.defaultOrderType))]
[PXSelector(typeof(Search2<SOOrderType.orderType,
InnerJoin<SOOrderTypeOperation, On2<SOOrderTypeOperation.FK.OrderType, And<SOOrderTypeOperation.operation, Equal<SOOrderType.defaultOperation>>>>,
Where<FeatureInstalled<FeaturesSet.inventory>.Or<SOOrderType.behavior.IsNotEqual<SOBehavior.bL>>>>))]
[PXRestrictor(typeof(Where<SOOrderTypeOperation.iNDocType, NotEqual<INTranType.transfer>, Or<FeatureInstalled<FeaturesSet.warehouse>>>), ErrorMessages.ElementDoesntExist, typeof(SOOrderType.orderType))]
[PXRestrictor(typeof(Where<SOOrderType.requireAllocation, NotEqual<True>, Or<AllocationAllowed>>), ErrorMessages.ElementDoesntExist, typeof(SOOrderType.orderType))]

[PXRestrictor(typeof(Where<
SOOrderType.active, Equal<True>,
And<Where<
SOOrderTypeExt.usrCompanyAcctCD, IsNull,
Or<SOOrderTypeExt.usrCompanyAcctCD, Equal<Current<AccessInfo.userID>>,
Or<SOOrderTypeExt.usrCompanyAcctCD, Equal<Selector<AccessInfo.branchID, BAccount.acctCD>>>
>>>>),
"The order type is not available for the current company.")]


[PXUIField(DisplayName = "Order Type", Visibility = PXUIVisibility.SelectorVisible)]
[PX.Data.EP.PXFieldDescription]

 

Best answer by DipakNilkanth

Hi ​@PDharmasena10,

I am seeing issues in your PXRestrictor code, as you matching your custom field to both User ID and Branch ID, I believe you need to match your custom field to the company name.(Based on what you are storing the custom field value in DB)

3 replies

Forum|alt.badge.img
  • Jr Varsity III
  • January 27, 2025

Hi ​@PDharmasena10  below i got it from AI result

To implement a restrictor on the Order Type selector in the sales order screen, ensuring that it only displays order types associated with the currently logged company ID or those that are null, you can follow these steps:

Step 1: Define the Restrictor

You need to create a restrictor that filters the order types based on the usrCompanyAcctCD field. This restrictor will check if the order type's company account code matches the current user's company ID or if it is null.

Sample Code for Restrictor

Here’s a basic example of how you might define this restrictor in your code:

 

public class OrderTypeRestrictor : PXGraphExtension<YourSalesOrderGraph>
{
    public void OrderType_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
    {
        if (e.Row is SalesOrder order)
        {
            var currentCompanyId = GetCurrentCompanyId(); // Implement this method to get the current company ID
            var orderTypes = PXSelect<OrderType,
                Where<OrderType.usrCompanyAcctCD, IsNull,
                    Or<OrderType.usrCompanyAcctCD, Equal<Required<OrderType.usrCompanyAcctCD>>>>>
                .Select(sender.Graph, currentCompanyId);

            // Set the available values for the Order Type selector
            sender.SetValue<OrderType.orderType>(e.Row, orderTypes);
        }
    }
}


 

Step 2: Implement Current Company ID Retrieval

You need to implement a method to retrieve the currently logged-in user's company ID. This may vary depending on your specific application setup, but generally, it could look something like this:

private string GetCurrentCompanyId()
{
    // Assuming you have a way to get the current user's company ID
    return PXAccess.GetUserBranchID().ToString();
}

Step 3: Update UI Elements

Ensure that your UI elements (like dropdowns or selectors) are bound to this restrictor so that they reflect the filtered list of order types based on the current company.

Step 4: Test Your Implementation

After implementing the restrictor, thoroughly test it to ensure that:

  • The order type selector only shows relevant order types.
  • It behaves correctly when there are no matching order types (it should still allow null selections).

Forum|alt.badge.img+2
  • Author
  • Semi-Pro I
  • February 10, 2025

TO archive this i tried two approaches. But both are not worked for me.

Method one:

Add a PXResrictor to orderType. I add below code, but it is not working
 

[PXMergeAttributes(Method = MergeMethod.Append)]

[PXRestrictor(typeof(Where<
SOOrderTypeExt.usrCompanyAcctCD, Equal<
Search2<Organization.organizationCD,
InnerJoin<Branch, On<Branch.organizationID, Equal<Organization.organizationID>>>,
Where<Branch.branchID, Equal<Current<SOOrder.branchID>>>>
>,
Or<SOOrderTypeExt.usrCompanyAcctCD, IsNull>>), "Unsupported order type")]

[PXRestrictor(typeof(Where<SOOrderTypeExt.usrBranchID, Equal<Current<SOOrder.branchID>>,
Or<SOOrderTypeExt.usrBranchID, IsNull>>), "TEST ERROR")]
protected virtual void SOOrder_OrderType_CacheAttached(PXCache cache)
{
}



Method two: Add an unbound field to store current Organization in the SOOrder DAC and then use it inside the PXResrictor logic. This filters the Order types as expected. but when going to save sales order it triggers the PXResrictor error ("Unsupported order type")
 

public class SOOrderExtApp : PXCacheExtension<PX.Objects.SO.SOOrder>
{
#region UsrOrganizationCD
[PXString(30, IsUnicode = true)]
[PXUnboundDefault(typeof(
SelectFrom<Organization>
.InnerJoin<Branch>
.On<Organization.organizationID.IsEqual<Branch.organizationID>>
.Where<Branch.branchID.IsEqual<SOOrder.branchID.FromCurrent>>
.SearchFor<Organization.organizationCD>
))]
[PXUIField(DisplayName= "UsrOrganizationCD")]
public virtual string UsrOrganizationCD { get; set; }
public abstract class usrOrganizationCD : PX.Data.BQL.BqlString.Field<usrOrganizationCD> { }
#endregion
}


SOOrderTypeExt DAC extension

public class SOOrderTypeExt : PXCacheExtension<PX.Objects.SO.SOOrderType>
{

#region UsrCompanyAcctCD
[PXDBString(30)]
[PXSelector(typeof(Organization.organizationCD),
typeof(Organization.organizationCD),
typeof(Organization.organizationName),
DescriptionField = typeof(Organization.organizationName))]

[PXDefault(PersistingCheck = PXPersistingCheck.Nothing)]
[PXUIField(DisplayName = "Company ID", Visibility = PXUIVisibility.SelectorVisible)]
public virtual string UsrCompanyAcctCD{ get; set; }
public abstract class usrCompanyAcctCD: PX.Data.BQL.BqlString.Field<usrCompanyAcctCD> { }
#endregion
}

SOOrderEntry
 

public class SOOrderEntry_Extension_01 : PXGraphExtension<PX.Objects.SO.SOOrderEntry>
{
#region Event Handlers

[PXMergeAttributes(Method = MergeMethod.Append)]
[PXRestrictor(typeof(Where<SOOrderTypeExt.usrCompanyAcctCD, Equal<Current<SOOrderExtApp.usrOrganizationCD>>
>), "TEST ERROR")]

protected virtual void SOOrder_OrderType_CacheAttached(PXCache cache)
{
}

#endregion
}

 


DipakNilkanth
Pro III
Forum|alt.badge.img+13
  • Pro III
  • Answer
  • February 10, 2025

Hi ​@PDharmasena10,

I am seeing issues in your PXRestrictor code, as you matching your custom field to both User ID and Branch ID, I believe you need to match your custom field to the company name.(Based on what you are storing the custom field value in DB)