Using “Locate” to Obtain a Modified Record in the Cache

There is a great difference between knowledge, understanding, and wisdom. Knowledge is the accumulation of facts. Understanding is the ability to apply those facts in a meaningful way. Wisdom is the ability to project how the various applications of knowledge will affect the outcome and then to make decisions accordingly.

While it is possible to create functional code with a little knowledge, it takes a certain degree of understanding to write code in a way that produces responsive, reliable, quality results. We demonstrate wisdom as we gain the ability to anticipate problems that will arise both within our code as well as in the user experience and change our approach to change the outcome.

This month, while reviewing the webinar for T230 Actions in 2019R2, I gained knowledge of Locate to access data manipulated in a base method when overriding that method. Previously, I would attempt to locate the record via View.Search<> and wonder why my results were less than stellar. Consider the following abbreviated sample taken from the T230 Actions training guide.

public delegate void UpdateBalancesDelegate(
	ARAdjust adj, ARRegister adjddoc, ARTran adjdtran);
[PXOverride]
public virtual void UpdateBalances(ARAdjust adj,
		ARRegister adjddoc, ARTran adjdtran,
		UpdateBalancesDelegate baseMethod)
{
	baseMethod(adj, adjddoc, adjdtran);
	ARRegister ardoc = (ARRegister)adjddoc;
	ARRegister cached =
		(ARRegister)Base.ARDocument.Cache.Locate(ardoc);
	if (cached != null)
	{
		ardoc = cached;
	}
}

One goal of this code sample is to work with ardoc which originates in this example as adjddoc passed into the original method. As you can see, this is passed into the Base method at the beginning of the override, but it is not returned from that method. This means that the override has no visibility of changes that occurred within the base method.

To work with this record, accounting for any changes already applied by the base method, we need to Locate the record in the cache layer of data. You might wonder how we are going to pass in the record we want to locate and expect to receive anything different. This is accomplished because only the key fields of the object (ardoc in this case) are used by Locate. These key fields will be used to locate the same record in the cache, which means that any changes applied in the base method will be returned for that record.

ARRegister cached =
 (ARRegister)Base.ARDocument.Cache.Locate(ardoc);

In this example, ardoc starts out as the object passed in as adjddoc. Within the scope of the override, adjddoc as an object passed in remains unchanged. However, the base method (baseMethod) may have changed values within this specific record within the cache. The use of Locate returns the potentially modified record into the object cached.

Next, if Locate found our record, we will replace our currently scoped record of ardoc with the one that we found in the cache as shown below.

if (cached != null)
{
	ardoc = cached;
}

Now that we have the updated values for ardoc, we can continue modify ardoc within our override without abandoning the changes applied within the base method.

Happy coding!

Leave a Reply