Monday 24 September 2018

Dynamics C/AL Function : CALCSUMS & CALCFIELDS


CALCSUMS Function
CALCSUMS calculates the sum of one or more fields that are SumIndexFields in the record.

CALCSUMS has the following syntax.
[Ok :=] Record.CALCSUMS (Field1, [Field2],...)

For CALCSUMS, a key that contains the SumIndexFields must be selected as the current key. Similar to CALCFIELDS, CALCSUMS uses the current filter settings when performing the calculation.

For Eg:
Let the Variable be 'CustLdgentry' & Datatype be 'Record'

CustLdgentry.SETCURRENTKEY("Customer No."); // will Set the Key Customer No.
CustLdgentry.SETRANGE("Customer No.",'50000','90000');// will set Rang e of Customers No. From 50000-90000.
CustLdgentry.SETRANGE("Posting Date",0D,TODAY);  //will Filter the posting date with Present date.
CustLdgentry.CALCSUMS("Sales (LCY)"); // Will Calculate total Sales From Sales(LCY) Field.
MESSAGE ('%1 calculated sales',custledgerentry."Sales (LCY)") // Total Sales Amount will be displayed in Message Box.


CALCFIELDS Function
CALCFIELDS updates FlowFields. FlowFields are automatically updated when they are the direct source expressions of controls, but they must be explicitly calculated when they are part of a more complicated expression.

CALCFIELDS has the following syntax.
[Ok :=] Record.CALCFIELDS(Field1, [Field2],...)

We need to remember that when we use FlowFields in C/AL functions, we must use the CALCFIELDS function to update them

For Eg:
In the below example, the SETRANGE function sets a filter and then the CALCFIELDS function calculates the Balance and Balance Due fields by using the current filter and performing the calculations that are defined as the CalcFormula properties of the FlowFields.

Let the Variable Be 'Customer', Data Type be 'Record'.

Customer.GET(INDCUS-005);  // The system will select the Customer No: INDG-CUS-005
Customer.SETRANGE("Date Filter",0D,TODAY); // Will set the date Range from the beginning to the Present Date
Customer.CALCFIELDS(Balance,"Balance Due"); // Using, Calcfields system calcs the balance as per Date filter.
MESSAGE('The Balance is %1 and your Balance Due is %2',Customer.Balance,Customer."Balance Due"); // Will Displays the balance in a Message Box, with Corresponding Value due.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Snippet in AL Language Programming

AL Snippets  are pieces of  AL  code that can be reused for faster  coding  in Visual Studio code. They have the prefix “t”, followed by a m...