Skip to main content
Solved

Update a custom field whenever stock item attributes change

  • September 6, 2024
  • 8 replies
  • 113 views

Forum|alt.badge.img

I am trying to get a custom field to update whenever the stock item attributes are updated, but I can’t find a way to make this work.

If base the row updated event on InventoryItem, it will update if other stock item fields are updated, but that doesn’t include the attributes.

If I base the row updated event on CSAnswers, then that does nothing.

Does anyone have any insight?

public class InventoryItemMaint_Extension : PXGraphExtension<PX.Objects.IN.InventoryItemMaint>
{
    protected void _(Events.RowUpdated<InventoryItem> e)
    {
        var invRow = base.Base.Item.Current;
        var invCache = base.Base.Item.Cache;
        var extension = invCache.GetExtension<InventoryItemExt>(invRow);

        if (invRow != null && invCache != null && extension != null)
        {
            var widthState = (PXStringState)invCache.GetValueExt(invRow, "WIDTH_Attributes");
            var heightState = (PXStringState)invCache.GetValueExt(invRow, "HEIGHT_Attributes");

            string width = PXStringState.GetStringValue(widthState, "D", "d");
            string height = PXStringState.GetStringValue(heightState, "D", "d");

            extension.UsrTestDimensions = $"Dimensions here: {width} x {height}";
            invCache.Update(extension);
        }
    }
}

 

Best answer by taras

Hi @tararosenthal 

If you want to listen any updates of attributes on a Stock Item screen you should subscribe on RowUpdated event of CSAnswers.

Here a sample code:

    public class InventoryItemMaint_Extension : PXGraphExtension<PX.Objects.IN.InventoryItemMaint>
    {
        protected void _(Events.RowUpdated<CSAnswers> e)
        {
            if (e.Row != null)
            {
                CSAnswers widthAttr = Base.Answers.Select().FirstTableItems.Where(a => a.AttributeID == "WIDTH").FirstOrDefault();
                CSAnswers heightAttr = Base.Answers.Select().FirstTableItems.Where(a => a.AttributeID == "HEIGHT").FirstOrDefault();
                string width = widthAttr?.Value;
                string height = heightAttr?.Value;
                if (!string.IsNullOrEmpty(width) && !string.IsNullOrEmpty(height))
                {
                    Base.Item.Cache.SetValueExt<InventoryItemExt.usrTestDimensions>(Base.Item.Current, $"Dimensions here: {width} x {height}");
                }
            }
        }
    }

 

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

8 replies

Forum|alt.badge.img+8
  • Captain II
  • 398 replies
  • September 8, 2024

Hi @tararosenthal 

 

Have you tried finding the values with the below?

var answers = Base.Answers.Current;

 

Aleks


Forum|alt.badge.img
  • Author
  • Freshman I
  • 28 replies
  • September 9, 2024

@aiwan Thanks, I will look into that, but I’m not sure why it would make a difference since it won’t change when the event is called in the first place.


Forum|alt.badge.img
  • Author
  • Freshman I
  • 28 replies
  • September 9, 2024

Not sure if this is unclear from my original post, but with the code I posted above, if you update any fields, excluding the attributes on the stock item, then it will fill in the correct information in that custom field. If you update the attributes on that stock item, then nothing will happen. If I use CSAnswers as the RowUpdated DAC instead of InventoryItem, then nothing will ever get updated in that custom field no matter what.


Forum|alt.badge.img+8
  • Captain II
  • 398 replies
  • September 10, 2024

@tararosenthal 

 

Ahhh, I got the impression that you couldn’t get the values at all.

If you use the CSAnswers as the ‘RowUpdated’, do you leave the code the same?

Do you create a variable of InventoryItem joining CSAnswers?

 

 

Aleks

 


Forum|alt.badge.img
  • Author
  • Freshman I
  • 28 replies
  • September 10, 2024

@aiwan I figured that Base would work the same way since the row updated function is still in the InventoryItemMaint graph extension either way. Is that not the case and you specifically have to do a join to get the data from InventoryItem when using CSAnswers?


Forum|alt.badge.img+8
  • Captain II
  • 398 replies
  • September 11, 2024

@tararosenthal 

 

Not 100% sure as I haven’t worked too closely with attributes, just throwing suggestions to see if they would help :) 

Your code doesn’t mention the CSAnswers table or view at all, which was the reason for my first suggestion.

If you were to use a BQL query to retrieve the values from CSAnswers, you could set a constant to the attribute names which you would then use in the BQL query e.g. 

CSAnswers width = SelectFrom<CSAnswers>.Where<CSAnswers.refNoteID.IsEqual<P.AsGuid>.

And<CSAnswers.AttributeID.IsEqual<P.AsString>>>.View.Select(Base, row.NoteID, “YourAttributeName”);

 

 

 

Hope this helps.

Aleks

 


Forum|alt.badge.img+1
  • Jr Varsity I
  • 24 replies
  • Answer
  • September 11, 2024

Hi @tararosenthal 

If you want to listen any updates of attributes on a Stock Item screen you should subscribe on RowUpdated event of CSAnswers.

Here a sample code:

    public class InventoryItemMaint_Extension : PXGraphExtension<PX.Objects.IN.InventoryItemMaint>
    {
        protected void _(Events.RowUpdated<CSAnswers> e)
        {
            if (e.Row != null)
            {
                CSAnswers widthAttr = Base.Answers.Select().FirstTableItems.Where(a => a.AttributeID == "WIDTH").FirstOrDefault();
                CSAnswers heightAttr = Base.Answers.Select().FirstTableItems.Where(a => a.AttributeID == "HEIGHT").FirstOrDefault();
                string width = widthAttr?.Value;
                string height = heightAttr?.Value;
                if (!string.IsNullOrEmpty(width) && !string.IsNullOrEmpty(height))
                {
                    Base.Item.Cache.SetValueExt<InventoryItemExt.usrTestDimensions>(Base.Item.Current, $"Dimensions here: {width} x {height}");
                }
            }
        }
    }

 


Forum|alt.badge.img
  • Author
  • Freshman I
  • 28 replies
  • September 11, 2024

Thanks so much @taras. I still don’t understand why your code works but this silently does nothing:

public class InventoryItemMaint_Extension : PXGraphExtension<PX.Objects.IN.InventoryItemMaint>
{
    protected void _(Events.RowUpdated<CSAnswers> e)
    {
        var invRow = base.Base.Item.Current;
        var invCache = base.Base.Item.Cache;
        if (invRow == null || invCache == null) return;
        var extension = invCache.GetExtension<InventoryItemExt>(invRow);
        if (extension == null) return;

        var widthState = (PXStringState)invCache.GetValueExt(invRow, "WIDTH_Attributes");
        var heightState = (PXStringState)invCache.GetValueExt(invRow, "HEIGHT_Attributes");

        string width = PXStringState.GetStringValue(widthState, "D", "d");
        string height = PXStringState.GetStringValue(heightState, "D", "d");

        extension.UsrTestDimensions = $"Dimensions here: {width} x {height}";
        invCache.Update(extension);
    }
}

But I really appreciate your help.


Reply


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