User Defined Functions

On this page

Function - Building blocks

Valid statements in a function

Actions in TDL Functions

TDL functions are action statements defined by an application developer. The developer has a complete control over the sequence in which these actions get executed. A TDL function extends the following benefits to the TDL programmer:

To perform a set of actions sequentially with or without any conditional statements.

Allows looping and conditional execution of a set of actions.

Defining once and executing it repetitively passing different set of parameters.

To define variables, manipulate and set values in it.

To work on data elements like getting an object context from the calling UI, changing the context, looping the objects of a collection, reading data from source and setting value in the target object, and so on.

Creation and manipulations of the existing internal objects.

With the advent of TDL the application developer can write complex business functions with ease and without much platform dependency.

Traditionally, TDL was always a non-procedural, action-driven language. The sequence of execution was not in the hands of the programmer. But, with this development in a ‘Function’ Definition, Conditional evaluation of statements and looping has been made possible. User defined Functions basically can be used for performing complex calculations or executing a set of actions serially. Functions can accept parameter(s) as input, and return a ‘Value’ to the caller.

With this development, the programmers can write business functions with complex computations by themselves, without platform dependency.

Function – Building Blocks

In TDL, Function is also a definition. It has two blocks:

Definition Block

Procedural Block

A glimpse into the function:

[Function : Function Name]

;; Definition Block

;; Parameter Specification

Parameter : Parameter 1 : Datatype

Parameter : Parameter 2 : Data t ype

;; Variable Declarations

Variable : Var 1 : Number

Variable : Var 2 : String

;; Explicit Object Association

Object : ObjName : ObjectType

;;Return Value

Returns : D atatype

;;Definition Block Ends here

;;Procedural Block

Label 1 : Statement 1

Label 2 : Statement 2

|

|

Label n : Statement n

;; Procedural Block Ends here

Definition Block

The definition Block is utilized for the following purposes:

Parameter specification

This implies specifying the list of parameters which are passed by the calling code. The values thus obtained, are referred to in the function with these variable names. The syntax for specifying the same is given below:

Syntax

PARAMETER : <Variable Name> : <Data Type of the Variable>

Where,

< Variable Name > is the name of the Variable which holds the parameter sent by the caller of the Function.

< Data Type of the Variable > is the Data type of the Variable sent by the caller of the Function.

Example

[Function: FactorialOf]

Parameter : InputNumber : Number

The Function ‘$$FactorialOf’ receives number as the parameter from the Caller.

Variable declaration

If a function requires some Variable(s) for intermediate calculation, then those Variable(s) need to be defined. The scope of these Variable(s) will be within the Function, and the Variable(s) lose their value after exit from the function.

Syntax

VARIABLE : <Variable Name> [:<Data Type of the Variable>]

Where,

< Variable Name > is the name of the Variable.

< Data Type of the Variable > is the Data type of the Variable.

Datatype is optional. If datatype is specified, a separate Variable definition is not required (these are considered as in-line variables). If data type is not specified, the interpreter will look for a variable definition, with the name specified.

Example

[Function : FactorialOf]

Parameter : InputNumber : Number

Variable : Counter : Number

Variable : Factorial : Number

The Function $$FactorialOf requires intermediate Variables ‘Counter’ and ‘Factorial’ for calculation within the ‘Function’ Definition.

Static Variable declartion

Static Variable is a Variable, whose value persists between successive calls to a Function.

The scope of the static variable is limited to the Function in which it is declared, and exists for the entire session.

Syntax

STATIC VARIABLE : <Variable Name> [:<Data Type of the Variable>]

Where,

< Variable Name > is the name of the Static Variable

< Data Type of the Variable > is the Data type of the Static Variable.

Data type is optional. If data type is specified, a separate Variable definition is not required (these are considered as in-line variables). If data type is not specified, the interpreter will look for a variable definition with the name specified.

Example

[Function : Sample Function]

Static Variable : Sample Static Var : Number

The static variable Sample Static Var retains the value between successive calls to the function Sample Function .

Return value specification

If a function returns a value to the caller, then its data type is specified by using RETURNS statement.

Syntax

RETURNS : <Data Type of the Return Value>

Where,

<Data Type of the Return Value > is the Data type of the return value of the Function.

Example

[Function : FactorialOf]

Parameter : InputNumber : Numb e r

Returns : Number

Variable : Factorial : Number

The function FactorialOf returns the value of type Number to the caller of the Function

Object specification

Function will inherit the Object context of the caller. This can be overridden by using the attribute Object for Function definition. This now becomes the current object for the function.

Syntax

Object : <ObjType> : <ObjIdValue>

Where,

< ObjType > is the type of the object, and

< ObjIdValue > is the unique identifier of the object.

Example

[Function : Sample Function]

Object : L e dger : “Party”

The function Sample Function will be in the context of the ledger Party .

Procedural Block

This block contains a set of statements. These statements can either be a programming construct or an Action specification. Every statement inside the procedural block has to be uniquely identified by a label specification.

Syntax

LABEL SPEC I FICATION : Programming Construct

Or

LABEL SPEC I FICATION : Action: A ction Parameter

Example

[Function : DispStockSummary]

01 : Display : Stock Summary

02 : Display : Stock Category Summary

The Function ‘DispStockSummary’ is having two Actions with Label.

Valid Statements inside a Function

The statements used inside the procedural block of a function can be:

A progra m ming cons t ruct, as discussed in the previous secti o ns

A TDL action

There have been major changes in some actions to work especially with functions. Some new actions have been introduced as well. Let us now discuss the various Actions used inside functions.

Programming Constructs In Function

Conditional Constructs

IF–ENDIF

The ‘IF–ENDIF’ statement is a powerful decision making statement and is used to control the flow of execution of statements. It is basically a two-way decision statement and is used in conjunction

with an expression. Initially, the expression will be evaluated and based on the whether the expression is True or False, it transfers the execution flow to a particular statement.

Figure_13.1_Flow_Chart_for_IF___ENDIF.jpg

Syntax

IF : <Condition Expression>

STATEMENT 1

STATEMENT N

ENDIF

Example:

If the f unction p arameter sent to the Function ‘FactorialOf’ is less than zero, then it is multiplied by -1 to find the absolute value.

[Function : FactorialOf]

Parameter : InputNumb e r : Number

Returns : Number

Variable : Counter : Number

Variable : Factorial : Number

1 : SET : Counter : 1

2 : SET : Factorial : 1

3 : IF ##I n putNumber < 0

4 : SET : Inp u tNumber : ##InputNum b er * -1

5 : END IF

6 : WHILE : ##Counter <= ##InputNumber

7 : SET : Fa c torial : ##Factorial * ##Counter

8 : SET : Counter : ##Counter + 1

9 : END WH I LE

10 : RETURN ##Factorial

DO IF

When an IF-ENDIF statement block contains only one statement, then the same can be written in a single line by using DO-IF statement.

Syntax

DO IF : <Condition Expression> : STATEMENT

Example

Here, IF - END IF statement is rewritten using the DO - IF statement.

[Function : FactorialOf]

Parameter : InputNumber : Num b er

Returns : Number

Variable : Counter : Number

Variable : Factorial : Number

1 : SET : Counter : 1

2 : SET : Factori a l : 1

3 : DO IF : ##InputNumber < 0 : ##InputNumber * -1

4 : WHILE : ##Counter <= ##In p utNumber

5 : SET : Fa c torial : ##Factorial * ##Counter

6 : SET : Co u nter : ##Counter + 1

7 : END WH I LE

8 : RETURN ##Factorial

IF–ELSE–ENDIF

The IF–ELSE–ENDIF statement is an extension of the simple IF-ENDIF statement. If the condition expression is True, then the ‘True’ block’s statement(s) are executed; otherwise, the ‘False’ block’s statement(s) are executed. In either case, either the True block or the False block will be executed, not both.

Figure_13.2_Flow_Chart_for_IF___ELSE_-_ENDIF.jpg

Syntax

IF : <Condition Expression>

STATEMENT 1

STATEMENT N

ELSE

STATEMENT 1

STATEMENT N

ENDIF

Example

Finding the greatest of three numbers

[Function : FindGreatestNumbe r s]

Parameter : A : Number

Parameter : B : Number

Parameter : C : Number

RETURNS : Number

01 : IF : ##A > ##B

02 : IF : ##A > ##

03 : RETURN : ##A

04 : ELSE

05 : RETURN : ##C

06 : END IF

07 : ELSE

08 : IF : ##B > ##C

09 : RETURN : ##B

10 : ELSE

11 : RETURN : ##C

12 : END IF

13 : END IF

Looping Construc t s

WHILE – ENDWHILE

In looping, a sequence of statements is executed until some condition(s) for termination of the loop is satisfied. A typical loop consists of two segments, one known as the body of the loop and the other known as the control statement. The control statement checks the condition and then directs the repeated execution of the statements contained in the body of the loop.

The WHILE – ENDWHILE is an entry-controlled loop statement. The condition expression is evaluated and if the condition is True, then the body of the loop is executed. After the execution of the statements within the body, the condition expression is once again evaluated and if it is True, the body is executed once again. This process of repeated execution of the body continues until the condition expression finally becomes False, and the control is transferred out of the loop.

Figure_13.3_Flow_Chart_for_WHILE___ENDWHILE.jpg

Syntax

WHILE : <Condition Expression>

STATEMENT 1

STATEMENT N

ENDWHILE

Example

[Function : FactorialOf]

Parameter : InputNumber : Nu m ber

Returns : Number

Variable : Counter : Number

Variable : Factorial : Numb e r

1 : SET : Counter : 1

2 : SET : Factorial : 1

3 : WHILE : ##Counter <= ##I n putNumber

4 : SET : Factorial : ##Factorial * ##Co u nter

5 : SET : Counter : ##Counter + 1

6 : END WH I LE

7 : RETURN ##Factorial

The Function ‘FactorialOf’ repeats statements 4 and 5, till the given condition is satisfied.

WALK COLLECTION – END WALK

If a Collection has ‘n’ Objects, then WALK COLLECTION – ENDWALK will be repeated for ‘n’ times. Body of the loop is executed for each object in the collection, making it the current context.

Figure_13.4_Flow_Chart_for_WALK_COLLECTION___ENDWALK.jpg

Syntax

WALK COLLE C TION : <Collection Name>

STATEMENT 1

STATEMENT N

ENDWALK

Example

Walking over all the Vouchers and counting the same.

[Collection : Vouchers Coll]

Type : Vouch e r

[Function : CountVouchers]

Returns : Number

Variable : Count : Number

001 : SET : Count : 0

002 : WALK COLLECTION : Vouch e rs Coll

003 : SET : Count : ##Count + 1

004 : END W ALK

005 : RETU R N : ##Cou n t

Control Constructs

Loops perform a set of operations repeatedly until the condition expression satisfies the given condition or the Collection is exhausted. Sometimes, when executing the loop, it becomes desirable to skip the part of the loop or to exit the loop as a certain condition occurs, or to save the current state and return back to the current state later.

BREAK

When a BREAK statement is encountered inside the loop, the loop is immediately exited and the control is transferred to the statement immediately following the loop. BREAK statement can be used inside WHILE – END WHILE and WALK COLLECION – END WALK. When loops are nested, the Break would only exit from the loop containing it.

Syntax

BREAK

Exam p le:

[Function : PrintNumbers]

Variable : Counter : Number

1 : SET : Counter : 1

2 : WHILE : ##Counter < = 10

3 : LOG : ## C ounter

4 : IF : ## C ounter > 5

5 : BREAK

6 : END IF

7 : SET : Counter : ##Counter + 1

8 : ENDWHI L E

9 : RETURN

In the Function ‘PrintNumbers’, the loop is running from 1 to 10. But because of BREAK statement, the loop will be terminated as the counter reaches 6.

CONTINUE

In some scenarios, instead of terminating the loop, the loop needs to be continued with next iteration, after skipping any statements in between. For this purpose, CONTINUE statement can be used. CONTINUE statement can be used inside WHILE – END WHILE and WALK COLLECION – END WALK.

Syntax

CONTINUE

Example:

Function to Count the total number of Journal Vouchers

[Collection : Vouchers Coll]

Type : Vou c her

[Function : CountJournal]

Returns : Number

Variable : Count : Number

01 : SET : Count : 0

02 : WALK C OLLECTION : Vouchers C oll

03 : IF : NOT $$IsJ o urnal:$VoucherTypeN ame

04 : CONTINUE

05 : ENDIF

06 : Count : ##C o unt + 1

07 : END W A LK

08 : RETURN : ##Count

START BLOCK – END BLOCK

START BLOCK –- END BLOCK has been introduced to save the current state and execute some actions within the block and return back to the original state. This is handy in cases where the Object context needs to be temporarily switched for the purpose of executing some actions. Current source and target object contexts are saved and the further statements within the START BLOCK and END BLOCK section get executed. Once the END BLOCK is encountered, the Object context is restored back to the original state.

Syntax

START BLOCK

Block Statements

END BLOCK

Example

10 : WALK C OLLECTION : EDCollDetailsExtra c t

11 : INSERT CO L LECTION OBJECT : Inv e ntoryEntries

12 : SET : QtyVar : $$String : $Qty + " Nos"

13 : SET : Amt V ar : $$String:$Amt

14 : START BLO C K

15 : SET OBJECT

16 : SET VALUE : ActualQty : $$AsQty :##QtyVar

17 : SET VALUE : BilledQty : $$AsQty :##QtyVar

18 : SET VALUE : Amount : $$AsAmount:# #AmtVar

18A : END BLOCK

19 : SET TARGET : ...

20 : SET VALUE : StockItemName : $ I tem

21 : END W A LK

;; For Explanation on Object context, i.e., Source Object and Target Object,

;; Set Target, Set Object, etc., please refer to the Topic ‘Function Execution - Object Context’

In this code snippet, the collection ‘EDCollDetailsExtract’ is being walked over and the values for the Objects within Voucher Entry are being set.

RETURN

This statement is used to return the flow of control to the calling program, with or without returning a value. When RETURN is used, the execution of the function is terminated and the calling program continues from where it had called the function.

Syntax

RETURN : <value expression>

Where,

< value expression > is optional, i.e., it can either return a value or return void.

Example

The Function ‘FactorialOf’ returns factorial of number

[Function : FactorialOf]

Parameter : InputNumber : Nu m ber Returns : Numb e r

Variable : Counter : Number

Variable : Factorial : Numb e r

1 : SET : Counter : 1

2 : SET : Facto r ial : 1

3 : WHILE : ##Counter <= ##I n putNumber

4 : SET : Factorial : ##Factorial * ##C o unter

5 : SET : Counter : ##Counter + 1

6 : ENDWHI L E

7 : RETURN : ##Factorial

Actions used in a TDL Function

Actions for Variable Manipulation

TDL provides various new actions that can be used inside User Defined Functions.

SET

This action is used to assign a value for a variable.

Syntax

SET : <VariableName> : <Value Expression>

Where,

< Variable Name > is the variable for which the value needs to be assigned.

< Value Expression > is the formula evaluating to a value.

Example

[Function : FactorialOf]

Parameter : InputNumber : Num b er Returns : Number

Variable : Counter : Number

Variable : Factorial : Number

1 : SET : Counter : 1

2 : SET : Factori a l : 1

3 : WHILE : ##Counter <= ##In p utNumber

4 : SET : Factorial : ##Factorial * ## C ounter

5 : SET : Counter : ##Counter + 1

6 : ENDWHI L E

7 : RETURN : ##Factorial

EXCHANGE

This action is used to exchange (swap) the values of two variables. But, values of only the variables of the same Data type can be exchanged.

Syntax

EXCHANGE : <First Variable Name> : <Second Variable Name>

Where,

< First Variable Name > and < Second Variable Name > are the Variables whose values need to be swapped.

Example:

01 : EXCHAN G E : Var1 : Var2

In this statement, both variables are of type ‘Number’ and their values are swapped.

INCREMENT

This action is used to increment the value of variables by any numerical value. INCREMENT is used inside the loop to increment value of control variables.

You can specify multiple variables by separating them with a comma.

Syntax

INCREMENT : <Variable Name> [,<Variable Name>, ...] [:<Number Expression>]

Where,

< Variable Name > is the name of the variable whose value needs to be incremented.

< Number Expression > is the numerical value by which the variable needs to be incremented. If no number is given, the variable will be incremented by 1.

Example

[Function: VATVchrColWalk_Inv_GUJ_AnnxError_LoadVAR]

If : ##GUJeVATAnex201BError

Increment : vVATAnxTotalCount, vVATAnxErrorCount, vVATAnx201BTotalCount, vVATAnx201BErrorCount

Else

Increment : vVATAnxTotalCount, vVATAnx201BTotalCount

End If

Note: The keyword Last will only work from release 5.4.8.

DECREMENT

This action is used to decrement the value of variables by any numerical value. DECREMENT is used inside the loop to decrement the value of the control variables.

Syntax

DECREMENT : <Variable Name> [,<Variable Name>, ...] [:<Number Expression>]

Where,

< Variable Name > is the name of the variable whose value needs to be decremented by the number specified.

< Number Expression > is the numerical value by which the variable needs to be decremented. If no number is given, the variable will be decremented by 1.

Example

[Function : PrintNumbers]

Variable : Cou n ter : Number

1 : SET : Co u nter : 10

2 : WHILE : ##Counter > 0

3 : LOG : ##Counter

4 : DECREMENT : Counter

5 : ENDWHI L E

6 : RETURN

In the function ‘PrintNumbers’, the loop is running from 10 to 1.

Action Enhancements and New Actions

The global actions have been enhanced, so that they can be called from the User Defined Functions. Some new actions have also been introduced.

Global Actio n s- Alter/Exec u te/Create

These are global actions meant for the purpose of opening a report in the mode specified. The return value of these actions is FALSE, if the user rejects the report, and TRUE, if the user accepts it.

Syntax

Display/Alter/Execute/Create : Report Name

Example

[Function : CreateReport]

01 : Create : Ledger

The Function ‘CreateReport’ opens the Ledger Creation screen.

Global Actions – MENU, DISPLAY

These global actions are used to invoke a menu or a report in Display mode. The return value of these actions is TRUE if Ctrl+Q is used to reject the report (i.e., via Form Reject To Menu action). It returns FALSE when user uses ‘Esc’ to reject the report (i.e., via ‘Form Accept’ action). For menu, this is only applicable if it is the first in the stack.

Syntax

Menu : <Menu name>

Display : <ReportName>

Example

[Function : DispPaySheet]

01 : Menu : Statements of Payroll

02 : Display : Pay Sheet

The Function ‘DispPaySheet’ opens the Pay Sheet and by pressing Escape, it will pop up the ‘Statements of Payroll’ Menu.

MSG BOX

This action is used to Display a message box to the user. It comes back to the original screen on the pressing of a key. This can be used by the programmers to display intermediate values of variables during calculations, thus helping in error diagnosis.

Syntax

MSG BOX : <Title Expression> : <Message Expression> : <GreyBack Flag>

Where,

< Title Expression > is the value that is displayed on the title bar of the message window.

< Message Expression > is the actual message which is displayed in the box. This can be an expression as well, i.e., the variable values can be concatenated and displayed along with the message in the display area of the box.

< GreyBack Flag > indicates if the background window is to be greyed out during message display. It takes two values, i.e., YES/NO

Example

01 : MSGBOX : ”Return Value” : ##Factorial

QUERY BOX

This action is used to Display a confirmation box to the user and ask for a Yes/No response.

Syntax

QUERY BOX : <Message Expression> : <GreyBack Flag> : <EscAsYes>

Where,

< Message Expression > is the message which is displayed inside the box. This can be an expression.

< GreyBack Flag > indicates if the background window is to be greyed out during message display. It takes two values, i.e., YES/NO

< Escape as Yes > is a flag which indicates the response when the user presses the ESC key. This can be specified as YES/NO. A YES value for this flag indicates that the response should be treated as YES on pressing of an ESC key.

LOG

During expression evaluation, intermediated values of the e xpression can be passed to c alculator win d ow a nd a log file ‘ t dlfunc.log’ insi d e the application director y . This is very much hel p ful f o r debuggi n g the expression. By defa u lt, logging is ena b led inside the function .

Syntax

LOG : < Expression>

Where,

< Expression > is the expression whose value needs to be passed to the calculator window.

Example

While findi n g the factorial of a numbe r , intermediated valu e s a re outpu t ted to the ‘Calculator’ win d ow using LOG a ction

[Function : FactorialOf]

Parameter : InputNumber : Nu m ber

Returns : Number

Variable : Counter : Number Variable : Factorial : Numb e r

1 : SET : Counter : 1

2 : SET : Factor i al : 1

3 : WHILE : ##Counter <= ##I n putNumber

4 : SET : Factori a l : ##Factorial * # # Counter

5 : SET : Counter : ##Counter + 1

5a : LOG : # # Factorial

6 : ENDWHI L E

7 : RETURN : ##Factorial

SET LOG ON

While debugging a Function, sometimes it is required to conditionally Log the values of an expression. If logging is stopped, then logging can be re-started based on the condition Action SET LOG ON. This Action does not require any parameter.

Syntax

SET LOG ON

SET LOG OFF

This Action is used in conjunction with SET LOG ON. Log can be stopped by the Action SET LOG OFF. This Action does not require any parameter.

Syntax

SET LOG OFF

SET FILE LOG ON

This Action is similar to SET LOG ON. SET FILE LOG ON is used to conditionally Log the values of an expression to log file ‘tdlfunc.log’. This Action does not require any parameter.

Syntax

SET FILE LOG ON

SET FILE LOG OFF

This Action is used in conjunction with SET FILE LOG ON . Logging the file ‘tdlfunc.log’ can be stopped by the Action SET LOG OFF. This Action does not require any parameter.

Syntax

SET FILE LOG OFF

Progress Bar Actions

Sometimes, a Function may take some time to complete the task. It is always better to indicate the user whether the task is occurring, how long the task might take and how much work has already been done. One way of indicating the amount of progress is to use an animated image. This can be achieved by using the following Actions:

START PROGRESS

SHOW PROGRESS

END PROGRESS

Action - S T AR T PROG R ESS

This action sets up the Progress Bar by mentioning the total number of steps involved in the task. In addition to this, the Title, SubTitle and Subject of the Progress Bar can also be given as parameters.

Syntax

START PROGRESS : <Number of steps> :< Title> [:< Sub Title> :< Subject>]

Where,

< Number of steps > denotes the whole task quantified as a number.

< Title >, < Sub Title > and < Subject > Shows the Title, Sub Title and Subject of the Progress Bar, respectively.

Example

START PROGRESS : ##TotalSteps : ”TDS Migration”:@@CmpMailName : ”MigrationgVouchers..”

Action - SHOW PROGRESS

This ac t ion shows the current st a tus o f the task to the user .

Syntax

SHOW PROGR E SS : <Number of Steps Completed>

Where,

< Number of Steps Completed > is a number which denotes the amount of work completed.

Example:

SHOW PROGR E SS : ##Counter

Action - E ND PROGR E SS

When a task is completed, the Progress Bar can be stopped by using the Action END PROGRESS. This Action does not take any parameter.

Syntax

END PROGRE S S

Actions – For Object and C o ntext Manipul a tion

As already seen in previous sections, functions can operate on 3 object contexts, i.e., Requestor, Current Object and Target object context. When a function is invoked, the target object context will be the same as the current object context of the caller, i.e., the target object will be set to the current object.

Here, we will discuss the various actions for manipulation of Object and Context.

Action - NEW OBJECT

This action creates a new object from object specification and sets it as the target object. It takes only Primary Object as the Parameter.

Syntax

NEW OBJECT : <ObjType>:<ObjIdValue>

Where,

< ObjType > is the type of the object to be created, and

< ObjIdValue > is the unique identifier of the object. If this is an existing object in DB, then the further manipulations are performed on that object, else it creates a new object altogether.

Example

01 : NEW OBJECT : Stock Item : ”My Stock Item”

This creates a new object in memory for Stock Item and sets it as the target object. Later, by using other methods of this, the target object can be set and saved to the Tally DB.

Action - INSERT COLLECTION OBJECT

This action inserts the new object of the type specified in the collection and makes it as the current target object. This object is inserted into the collection at the end. This Action takes only Secondary Collection as the parameter.

Syntax

INSERT COLLECTION OBJECT : <CollectionName>

Where,

< CollectionName > is the name of the Secondary Collection.

Example

01 : INSERT COLLECTION OBJECT : L edger Entries

This insets a new object ‘Ledger Entries’ in memory under Voucher, and sets it as the target object. Later, by using other methods of this, the target object can be set and saved to Tally DB.

Action - SET VALUE

This action sets the value of a method for the target object. The value formula is evaluated with respect to the current object context. This can use the new method formula syntax. Using this, it is possible to access any value from the current object.

Syntax

SET VALUE : <Method Name>[: <Value Formula>]

Where,

< Method Name > is the name of the method, and

< Value Formula > is the value which needs to be set to the method. It is optional. If the second parameter is not specified, it searches for the same method in the context object and the value is set based on it. If the source method name is same as in Target Object, then the Source Object method name is optional.

Example: 1

01 : SET VA L UE : Ledger Name : $ LedgerName

OR

01 : SET VA L UE : Ledger Name

These statements set the values of ‘Ledger Entries’ Object from the current Object context.

Example: 2

02 : WALK COL L ECTION : Vouchers of My Objects

03 : NEW OBJECT : Voucher

;; Since the methods Date, VoucherTypeName are same in the source object and target object, they are not specified again as SET VALUE : DATE : $Date.

04 : SET VALUE : D ate

05 : SET VALUE : V oucherTypeName

Example: 3

[Function : Sample Function]

Object : Ledger : "P a rty 1"

01 : NEW O B JECT : Ledger : "P a rty 2"

;; absence of Value expression will assume that same method to be copied from source

02 : SET V A LUE : Parent

03 : ACCEPT ALTER

‘Party 1’ is a ledger under the Group ‘North Debtors’ and Party 2 is a Ledger under the Group ‘South Debtors’. After the execution of this function, Party 2 will also come under the Group ‘South Debtors’.

Action - RESET VALUE

This action sets the value of the method using the Value Formula. If Value Formula is not specified, it sets the existing value to null.

Syntax

RESET VALUE : MethodName [: Va l ue Formula]

Where,

< Method Name > is the name of the method, and

< Value Formula > is an optional parameter and if it is used, it will reset the value of the method.

Example

01 : SET VALUE : Ledger Name : $LedgerName

02 : RESET VALUE : Ledger Name : “New Value”

In this code snippet, RESET VALUE resets the value of the method ‘Ledger Name’

Action - CREATE TARGET/ACCEPT CREATE

It accepts the Target Object to the Company Data base, i.e., it saves the target object to the database. This creates a new object in the database if it does not exist, else results in an error.

Syntax

CREATE TA R GET/ACCEPT CREATE

Action - S A V E T ARGET/ A CCEPT

It accepts the Target Object to the Company Tally DB. If another object exists in the Tally DB with the same identifier, then the object is altered, else a new object is created.

Syntax

SAVE TARG E T/ACCEPT

Action - A L TER T ARGET/AC C EPT A L TER

It allows altering an exiting object in the Tally Data Base. If the object does not exist, it results in an error.

Syntax

ALTER TARG E T/ACCEPT ALTER

Action - SET OBJECT

It sets the current object with the Object Specification. If no object specification is given, the target object will be set as the current object. Only Secondary Object can be used along with this Action.

Syntax

SET OBJECT [:<Object Spec>]

Where,

< Object Spec > is the name of the Secondary Object.

Example:

[Function : Sample Function]

Object : Ledger : "My Test Ledger"

01 : LOG : $Name

02 : SET O B JECT : BillAllocat i ons[1]

03 : LOG : $Name

04 : SET O B JECT : ..

05 : LOG : $Name

Initially, the context object is Ledger, so $Name gives the name of the Ledger. By Using ‘ SET OBJECT ’, the current Object is changed to first Bill allocation. So, the second $Name is giving the Bill name. The Fourth line changes the current Object back to ‘Ledger’ using dotted notation.

Action - SET T ARGET

This action sets the t arget o bject with the Obj e ct S pecificatio n . If no object specificati o n is given , then the current object will be set a s the ta r get objec t .

Syntax

SET TARGET : <Object Spec>

Where,

<Object S p e c> is the n ame of the Object.

Example:

01 : SET T A RGET : Group

This sets the ‘Group’ Object as the Target Object. Later, by using other methods of this, the target object can be set and saved to the Tally DB.

Usage of Object manipulation Actions

Duplicating all payment Vouchers

[Function : DuplicatePaymentV o uchers]

;;Process for each Payment Voucher

01 : WALK C OLLECTION : My Vou c hers

;; Create new Voucher Object as Target Object

02 : NEW O B JECT : Voucher

;;For New Object, set methods from the First Object of the Walk Collection, i.e., from the Current Object

03 : SET V A LUE : Date : $Date

04 : SET VALUE : VoucherTypeName : $ VoucherTypeName

05 : S ET VALUE : Narration : $Narrat i on + " Duplicated"

;; Walk over Ledger Entries of the current Object

05a : WALK COLLE C TION : LedgerEntries

;;Insert Collection Object to the Target Object and make it the present Target Object

06 : INSERT COLL E CTION OBJECT : Ledg e r Entries

;;Set the Values of the Target Object’s Method from Current Objects Methods

07 : SET VALUE : Ledger Name : $Ledg e rName

08 : SET V A LUE : IsDeemedPosi t ive : $IsDeemedPosi t ive

09 : SET VALUE : Amount : $Amount

;;Set the Voucher Object as Target, (which is 1 level up in the hierarchy) as Voucher is already having

;;Object specification

10 : SET TARGET : ..

11 : END W A LK

;;Save the Duplicated Voucher to the DB.

12 : CREATE TARGET

13 : ENDWA L K

14 : RETU R N

Calling a Function

A Function can be invoked in two ways:

1. By using a “CALL” action - This is mainly used when the function does not return a value. It only performs a certain functionality.

2. By Using the prefix $$ with the function name within a value expression - This is used when a return value is expected from the function execution. This value is used in the value expression of the calling program.

Using the Action ‘CALL’

Action ‘CALL’ can be used to call a function. It can be invoked from a Key, Menu Item or a Button.

Syntax

CALL : <Function Name> [: <Parameter List>]

Where,

< Function Name > is the name of a user defined function.

< Parameter List > is the list of parameters accepted by the function.

Example

Calling the Function as a procedure with CALL action

[#Menu : G ateway of Tally]

Button : Call Function

[Button : Call Function]

Key : A l t + F9

Title : C all Function”

Call : D i spStaturoryRpts

Using th e Symbol Prefix ‘$$’

A Function can be called and executed by prefixing it with the symbol ‘$$’. This can be used inside a value expression or as a value for the ‘Set As’ attribute of the field. The value returned from the function is used.

Syntax

$$FunctionName : <Parameter List>

Where,

<Fun c tion Name> is the name of a us e r defined function.

<Paramet e r List> is the list of p arameters accept e d by the f u nction.

Note :

During Tally Startup, Tally executes a function with the name “TallyMAIN”.

Internal functions always override if both exist in the same name.

Example

Calling t he User Defined Function at Field using $$

[Field : Call Function]

Use : Number F ield

Set as : $$Facto r ialOf:#InputFld

Call an action at line level

To call an action at a line level, associate a key with the line which calls the appropriate action.

Example: 1

You can select or deselect a line using the action Toggle Selection.

[Line: TSPL Ledger Line]

Key : Toggle Selection

[Key: Toggle Selection]

Key    :  Space

Action : Toggle Selection

In the above example, when space bar is pressed on the line TSPLLedgerLine , it calls the action Toggle selection .

Example: 2

To call a function on selecting a line. Along with selecting a line, call a function.

[Line: TSPL Ledger Line]

Key : TSPLSelectFunc

[Key: TSPLSelectFunc]

Key        : Space

Actionlist : TSPL Toggle Selection, TSPLFunc

[Key: TSPL Toggle Selection]

Key    : Space

Action : Toggle Selection

[Key: TSPLFunc]

Key    : Space

Action : Call : TSPLFunc

[Function: TSPLFunc]

000: Set : SVSelectedLines: $$NumItems:NumSelectedLines

005: Log: ##SVSelectedLines

[System: Variables]

Variable: SVSelectedLines: Number

[Collection: NumSelectedLines]

Datasource: Report : Selected

;; In Release 6.0 and later, use the action ActionEx instead of ActionList

[Key: TSPLSelectFunc]

Key      : Space

Action Ex: Toggle: Toggle Selection

Action Ex: Func: Call: TSPLFunc

In the above example, when space bar is pressed on the line, it executes the action Toggle Selection and then the function TSPLFunc to set the variables to the number of selected lines.

Example: Convert Numbers to Roman Numerals

You can convert the Arabic numerals (1, 2, 3, and so on) to Roman numerals by writing a user defined function as shown below. For example, if the input is 1, then the user defined function returns I, which is a Roman numeral.  The following code snippet is to convert any number between 1 and 50 to the corresponding Roman numeral.

[Function: ConvertNumber]

;; This code snippet converts only the numbers between 1 and 50 to the corresponding Roman numeral. You need to add appropriate validations to exclude values below 1 and above 50.

Parameter : InputValue      : Number

Variable     : VarReturnVal  : String:  ""

01 : While :##InputValue = 50

05 :   Set:VarReturnVal:##VarReturnVal+"L"

10 :   Set:InputValue:##InputValue-50

15 : End While

20 : While :##InputValue >= 40

25 :   Set :VarReturnVal:##VarReturnVal+"XL"

30 :   Set:InputValue:##InputValue-40

35 : End While

40 : While:##InputValue >= 10

45 :   Set:VarReturnVal: ##VarReturnVal+"X"

50 :   Set:InputValue:##InputValue-10

55 : End While

60 : While:##InputValue >= 9

65 :   Set:VarReturnVal:##VarReturnVal +"IX"

70 :   Set:InputValue:##InputValue-9

75 : End While

80 : While:##InputValue >= 5

85 :   Set:VarReturnVal:##VarReturnVal+"V"

90 :   Set:InputValue:##InputValue-5

95 : End While

100: While:##InputValue >= 4

105:   Set:VarReturnVal:##VarReturnVal+"IV"

110:   Set:InputValue:##InputValue-4

115: End While

120: While:##InputValue >= 1

125:   Set:VarReturnVal:##VarReturnVal+"I"

130:   Set:InputValue:##InputValue-1;

135: End While

140: Return: ##VarReturnValFunction

Execution – Object Context

We all are aware that in TDL, any function, method or formula gets evaluated in the current object context. All the platform defined functions will be executed with the current object and requestor context.

With the introduction of User Defined Functions, another type of context is introduced. This is known as the target context.

Target Object Context

Target Context mainly refers to the object context which can be set inside the function, which allows the function to perform manipulation operations for that object, i.e., alteration and creation. The object context of the caller and target object context can be different. It will now be possible to obtain the values from the caller object context and alter the values of the target object with those values.

The User has the option to override the context within the function later or use the same context being passed. He can change the current and target object at any point and also switch target and current object to each other.

$$TgtObject is used to evaluate the value of an expression in the context of the target object.

Parameter Evaluation Context

It is important to note that the parameter values which are passed to the functions are always evaluated in the context of the caller. Parameter specification within the functions is just to determine the data type, order and number of parameters. These are basically the place-holders for values passed from caller object context. The target object context or context switch within the function does not affect the initial values of the parameters. Later, within the function, these values can be altered just like ordinary variables.

Return Value Evaluation

We have already discussed above that function can return a value. This can be specified by the function by indicating the data type of the value it returns, no specification assumed as a void function (a function which does not return a value). Each statement (Actions discussed in the next section) used can return a value. This depends on the individual action. Some actions may not return a value. The data type of the return value is also predefined by the action. Return value of the last action executed can be extracted using an internal function ‘$$LastResult’. Any error messages generated from the system can be obtained with $$LastError. This can only be used to determine the result of the intermediate statements used within the function. The final value which is to be returned from the function has to be explicitly given using the RETURN construct discussed in the previous section.