Thursday, May 21, 2009

KeySum Class

KeySum class is useful for summing data linked to keys. For example you may want to sum Invoice Amount while looping in CustInvoiceJour with grouping in Customer Group. So I recommend to use that handy class. No need to explain in theoretic let’s go practical.

KeySum keySum; // Define keySum class
;
// Initializing class:
// First Param: NumOfKeys : Length of key.
// If you specify that value 2, you have to give it a container which length of it is 2.
// Second Param: NumOfData : Length of data.
// If you specify that value 2, you have to give it a container which length of it is 2.
// Third Param: Sorting
// -1 : Descending order according to key
// 0 : It does not sort
// 1 : Ascending order according to key
keySum = new KeySum(1, 1, 0); // Initialize
keySum.updateNow(1, 15); // keySum = [1 -> 15]
keySum.updateNow(2, 47); // keySum = [1 -> 15, 2 -> 47]
keySum.updateNow(1, 100); // keySum = [1 -> 115, 2 -> 47]
keySum.updateNow(3, 15); // keySum = [1 -> 115, 2 -> 47, 3 -> 15]
keySum.updateNow(7, 47); // keySum = [1 -> 115, 2 -> 47, 3 -> 15, 7 -> 47]
keySum.updateNow(4, 100); // keySum = [1 -> 115, 2 -> 47, 3 -> 15, 7 -> 47, 4 -> 100]
print keySum.key2Data(1); // prints 115 = which is sum of 15 + 100 i.e. the data we loaded earlier for the key 1
print keySum.total(); // prints 324 which is grand total of all the data
print keySum.numOfTrans(); // prints 5 = which is the count of the keys (1,2,3,4,7)
keySum.keyDelete(2); // deletes key 2 -> 47. So total is decreased to 324-47 = 277
keySum.addKeySum(keySum); // addKeySum method can be merged with another keySum object. In this case we did with itself
print keySum.total(); // total is doubled so 277*2 = 554
pause;

No comments:

How to identify the user that was used to change an object from AOT in AX2012

Get the object name for which we need to track these (user and date&time) information's. Login to SQL Server Management Studio an...