0

Follow-up question from my previous question Make a Field Mandatory on the Graph Level.

After applying the answer from my previous question by removing the underscore (_) from my TableColumn, it is obvious that the Customer Locations screen is now picking up on the code customizations, so I accepted the answer there, as my new issue is sure to require a different solution.

Upon loading the Customer Locations (AR303020) screen, I am now receiving this error:

Failed to subscribe the event PX.Objects.AR.CustomerLocationMaint_Extension::SelectedCustomerLocation_UsrDCLID_CacheAttached in the graph PX.Objects.AR.CustomerLocationMaint. The method signature looks like an event handler, but the cache SelectedCustomerLocation has not been found in the list of auto-initialized caches. Remove unused event handlers from the code.

I used the OVERRIDE ON SCREEN LEVEL button from the Layout Editor to generate what I would assume to be the correct code stub.

enter image description here

This is the stock code produced:

using System;
using PX.Data;
using PX.Objects.CR;
using System.Collections.Generic;
using PX.Objects;
using PX.Objects.AR;

namespace PX.Objects.AR
{
  public class CustomerLocationMaint_Extension : PXGraphExtension<CustomerLocationMaint>
  {
    #region Event Handlers



    protected virtual void SelectedCustomerLocation_UsrDCLID_CacheAttached(PXCache cache)
    {

    }



    #endregion
  }
}

And this is after my simple Required modifications:

using System;
using PX.Data;
using PX.Objects.CR;
using System.Collections.Generic;
using PX.Objects;
using PX.Objects.AR;

namespace PX.Objects.AR
{
  public class CustomerLocationMaint_Extension : PXGraphExtension<CustomerLocationMaint>
  {
    #region Event Handlers


    [PXDefault]
    [PXCustomizeBaseAttribute(typeof(PXUIFieldAttribute), "Required", true)]
    protected virtual void SelectedCustomerLocation_UsrDCLID_CacheAttached(PXCache cache)
    {

    }



    #endregion
  }
}

From here I save my code changes and publish the Customization Project. Then, when I refresh the Customer Locations screen, I receive the aforementioned error.

You can see here that the Data Class for this form is PX.Objects.AR.SelectedCustomerLocation

Layout Editor

But when I inspect the field on the page, it shows the Data Class is Location, which I've found correlates to PX.Objects.CR.Location. I believe the AR.SelectedCustomerLocation extends CR.Location

Element Properties

I'm not really sure where to go from here. Given that the system generated the SelectedCustomerLocation_UsrDCLID_CacheAttached method then I have to believe this is the event handler that is needed. I have tried changing this to Location_UsrDCLID_CacheAttached, which causes the error to go away, but the Requiredness of the field is not there, so I do not believe this is the correct method signature.

How do I get the SelectedCustomerLocation cache to exist on this page? Do I need it to exist? Should it just be Location?

Aaron St Clair
  • 128
  • 2
  • 9

1 Answers1

1

Location_UsrDCLID_CacheAttached would be correct name as you use the base DAC name for which you are extending. SelectedCustomerLocation inherits from SelectedLocation which inherits from Location.

Try this...

[PXDBInt]
[PXUIField(DisplayName="DCL Account ID", Required = true)]
protected virtual void Location_UsrDCLID_CacheAttached(PXCache cache)
{

}

You can add the PXUIRequiredAttribute if you need a conditional requirement. Example:

[PXUIRequired(typeof(Where<INItemSite.overrideInvtAcctSub, Equal<True>>))]

You can use PXUIFieldAttribute to set the required property on the DAC, Graph cached attached, or in logic such as the RowSelected event or a graph extension initialization.

Example:

protected virtual void Location_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
{
    PXUIFieldAttribute.SetRequired<LocationExt.usrDCLID>(cache, true);
}

For more flexibility you can check for the condition in the rowpersisting event like so...

protected virtual void Location_RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
{
    var row = (Location)e.Row;
    if(row == null)
    {
        return;
    }

    var rowExt = row.GetExtension<LocationExt>();
    if (rowExt != null && rowExt.UsrDCLID == null)
    {
        if (sender.RaiseExceptionHandling<LocationExt.usrDCLID>(e.Row, null, new PXSetPropertyException(ErrorMessages.FieldIsEmpty, typeof(LocationExt.usrDCLID).Name)))
        {
            throw new PXRowPersistingException(typeof(LocationExt.usrDCLID).Name, null, ErrorMessages.FieldIsEmpty, typeof(LocationExt.usrDCLID).Name);
        }
    }
}
Brendan
  • 5,428
  • 2
  • 17
  • 33
  • I have tried using both `Location` and `SelectedLocation`. I get the same error outlined in my original post when using `SelectedLocation`, so `Location` seems correct as you said. [The docs](https://help.acumatica.com/(W(19))/Main?ScreenId=ShowWiki&pageid=97f5b92f-906a-4edd-ae38-f5605e83d845) say to use `PXCustomizeBaseAttribute` to make this field required. I have tried using that with `Location_UsrDCLID_CacheAttached` but it doesn't work. When attempting to use your suggestion of `PXUIRequiredAttribute` the project fails validation. I cannot find that in the docs. – Aaron St Clair Mar 20 '18 at 15:57
  • I believe it may be failing due to the arguments I'm passing. I would look this up in the docs to understand the arguments you've provided but I cannot locate it. Maybe I'm not understanding fully. I believe the [PXUiFieldAttribute](https://help.acumatica.com/(W(19))/Main?ScreenId=ShowWiki&pageid=ea7ce94e-4b3e-0f91-df1d-a4ce8023b184) and the [Required attribute](https://help.acumatica.com/(W(19))/Main?ScreenId=ShowWiki&pageid=16feb9ec-fa46-3873-967d-9e8468e87959) get combined to something like `PXUIRequired`. Could you explain your `typeof` expression there? – Aaron St Clair Mar 20 '18 at 16:05
  • PXDefault is marking the field as required but necessarily showing the field with the red asterisk * if that is what you are looking for. typeof for PXUIRequired is indicating the where condition that when true marks the field as required. I can provide some alternatives to see if they work for you to indicate the red asterisk – Brendan Mar 20 '18 at 16:20
  • updated the cache attached and provided an alternative to set via the RowSelected event – Brendan Mar 20 '18 at 16:29
  • I have updated using your latest suggestion with `[PXUIField(Required = true)]` on the `CacheAttached` event, but to no avail. However, the method using the `RowSelected` event does work as expected. I had considered going that route, but did not want to resort to workarounds when all the docs say I can do it using [C# Attributes](https://msdn.microsoft.com/en-us/library/z0w1kczw(v=vs.100).aspx). Can you offer any further insight as to why it may not work the Attribute route for the `CacheAttached` event? Perhaps that event isn't firing, but exists so the compiler doesn't fail validation? – Aaron St Clair Mar 20 '18 at 17:08
  • can you include the customization XML in your question or link to download and I can review? this way i can get the field in my instance easier to test locally. I am curious myself. – Brendan Mar 20 '18 at 17:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167204/discussion-between-brendan-and-aaron-st-clair). – Brendan Mar 20 '18 at 17:41
  • I am stuck on why the cache attached here doesn't work. Using this should work: Location_UsrDCLID_CacheAttached(PXCache cache) but no attribute added here will work. – Brendan Mar 20 '18 at 21:26
  • Since using `string` did not help, I'm going to switch back to `int`. – Aaron St Clair Mar 22 '18 at 12:46
  • yea sorry I have no clue why the cache attached in this case would not work. I have not seen it not work. I confirmed it wouldnt work as I was trying to set the display name to a different value with no luck. – Brendan Mar 22 '18 at 13:25