Skip to main content

Confusion about items in a cache vs. items in a View

  • 16 May 2024
  • 0 replies
  • 30 views

In my graph extension, if I loop through the items in a cache, I get a set of items I can view.  When I loop through a View based on that cache, there are more items than what are in the cache.  

I guess I don’t understand the mechanics of the cache.

In the code below, when a user updates a record in the CSAnswers cache, I loop through all the attributes in the View and check to see which item is being updated in the RowUpdating event handler.  I do some validations, and I set some variables.  I am checking to see if TWO of the attributes have values that I will use to do some calculations, then update TWO other attributes with the results of those calculations

At the end, if the two attributes used to do the calculation have valid values, I loop through the View AGAIN, to update the other two attributes that store the results of the calculations.  When I find the attribute that needs updated, I set the cache value to that calculated amount.  When I refresh the grid, the screen shows my updated values.  So, it HAS to be in the cache!

Rather than loop through the View, I’d like to access the attributes DIRECTLY in the cache by locating the attributes in the cache.  

Since the list of items in the cache does not include all the items in the View, I cannot do that. 

My question is this, if I can update items in the View (which is based on the cache passed into the event handler), why can’t I locate the cached item?

Here is my code:

using GTISeedWeightCalculation;
using PX.Data;
using PX.Objects.CN.Common.Extensions;
using PX.Objects.CS;
using System.Collections.Generic;

namespace PX.Objects.PO
{

// Acuminator disable once PX1016 ExtensionDoesNotDeclareIsActiveMethod extension should be constantly active
public class SeedPOReceiptEntry_Extension : PXGraphExtension<PX.LotSerialNbrAttribute.Ext.POReceiptEntryPXLotSerialExt, POReceiptEntry>
{
protected void CSAnswers_RowUpdating(PXCache cache, PXRowUpdatingEventArgs e)
{
var row = (CSAnswers)e.Row;

string measurement = null;
double seedsPerLB = 0;
double seedsPerGR = 0;
double dweight = 0;
double dseedsPer = 0;

bool test = false;

//This is just a test to see what items are in the cache
Dictionary<string, string> mydict = new Dictionary<string, string>();

//this returns fewer items that when I loop through the records in the View.
foreach(CSAnswers obj in Base1.Attributes.Cache.Cached)
{
mydict.Add(obj.AttributeID, obj.Value);
}
int count = mydict.Count;

//This always returns a value
CSAnswers cs = new CSAnswers();
cs.AttributeID = "MEASURE";
cs.RefNoteID = row.RefNoteID;
CSAnswers cached = Base1.Attributes.Locate(cs);

//this returns null
CSAnswers cs2 = new CSAnswers();
cs2.AttributeID = "WEIGHT";
cs2.RefNoteID = row.RefNoteID;
CSAnswers cached2 = Base1.Attributes.Locate(cs2);

//this returns null
CSAnswers cs3 = new CSAnswers();
cs3.AttributeID = "SEEDCALCLB";
cs3.RefNoteID = row.RefNoteID;
CSAnswers cached3 = Base1.Attributes.Locate(cs3);

//Loop through the View and check for attributes to validate and store in variables
foreach (CSAnswers obj in Base1.Attributes.Select())
{
if (obj.AttributeID.ToString() == "WEIGHT" && obj.Value != null)
{
test = double.TryParse(obj.Value.ToString(), out dweight);
if (!test)
{
throw new PXException(ICSMessages.WeightNotANumber);
}
}
if (obj.AttributeID.ToString() == "SEEDSPER" && obj.Value != null)
{
test = double.TryParse(obj.Value.ToString(), out dseedsPer);
if (!test)
{
throw new PXException(ICSMessages.SeedsPerNotANumber);
}
}
if (obj.AttributeID.ToString() == "MEASURE" && obj.Value != null)
{
measurement = obj.Value.ToString();
}

if (dweight > 0 && measurement != null && dseedsPer > 0)
{
if (measurement == "POUNDS")
{
seedsPerLB = dseedsPer;
seedsPerGR = (dweight * dseedsPer) / (453.592 * dweight);
}
if (measurement == "GRAMS")
{
seedsPerLB = (dweight * dseedsPer) / (dweight / 453.592);
seedsPerGR = dseedsPer;
}
}
}

bool updated = false;

//if the variables have valid values, loop through the View again and set the values on two of the attributes
if (seedsPerLB > 0 && seedsPerGR > 0)
{
foreach (CSAnswers obj in Base1.Attributes.Select())
{
if (obj.AttributeID.ToString() == "SEEDCALC")
{
//obj.Value = seedsPerGR.ToString();
cache.SetValue<CSAnswers.value>(obj, seedsPerGR.ToString());
updated = true;
}
if (obj.AttributeID.ToString() == "SEEDCALCLB")
{
//obj.Value = seedsPerLB.ToString();
cache.SetValue<CSAnswers.value>(obj, seedsPerLB.ToString());
updated = true;
}
}
}

//if there were any updates, refresh the grid on the screen
if (updated)
{
Base1.Attributes.View.RequestRefresh();
}
}
}
}

 

Be the first to reply!

Reply