Functions - Sample Code
1. Calculate Days: Function to calculate the number
of days between two given dates. The function returns an integer value.
Function Definition:
int Calculations.CalculateDays(date sdate, date edate)
{
days = (((input.edate - input.sdate) / 86400000)).toLong();
return days;
}
|
where,
int - type of data returned by this function is of type int.
Calculations - is the namespace under which the Function is created.
CalculateDays - is the function name.
sdate - argument of type date.
edate - argument of type date.
Function Call:
The function CalculateDays is called in the on add -> on success
script, which updates a numeric form field Number_of_days with the
value returned by the function.
on add
{
on success
{
input.Number_of_days = thisapp.Calculations.CalculateDays(input.Start_Date, input.End_Date);
}
}
|
2. Date Format : Function to get the month,
year and date of a given date. The function returns the input date as a string
in the format "day/month/year", for example 12/2/2008
Function Definition
string dateformat.getAustralian(date inputdate)
{
month = inputdate.getMonth();
day = inputdate.getDay();
year = inputdate.getYear();
outputstr = day + "/" + month + "/" + year;
return outputstr;
}
|
3. isLeapYear() : Function to check if the
given year is a leap year. The function returns a boolean value 'true', if the
given year is a leap year.
Function Definition
bool isLeapYear(int year)
{
leapyear = false;
if ((input.year % 4) == 0)
{
leapyear = true;
if (((input.year % 100) == 0) && ((input.year % 100) != 0))
{
leapyear = false;
}
}
return leapyear;
}
|
4. nextLeapYear() : Function to return the
next leap year based on a given year. Here, the function calls another function
isLeapYear() .
Function Definition
int nextLearYear(int year)
{
if (thisapp.isLeapYear((input.year + 1)))
{
return (input.year + 1);
}
else if (thisapp.isLeapYear((input.year + 2)))
{
return (input.year + 2);
}
else if (thisapp.isLeapYear((input.year + 3)))
{
return (input.year + 3);
}
else
{
return (input.year + 4);
}
}
|
5. Update Income & Expense
Let us take the example of an Income and Expense Application, which has
the following three forms:
- Income form to enter the income details
- Expense form to enter the expense details
- Amount in Hand form which holds a single record to display the difference
between the total income and total expense, at any point of time.
Now, whenever an income/expense is added, modified or deleted the total income
and total expense needs to be updated, to calculate the Amount in hand
at any point of time. To do this, you will have to write the same set of statements
in the on add, on edit and on delete success script. By defining the statements
as a function, you just need to write the code in one place, and call it whereever
necessary. The complete script for this application (.ds file) with the function
definition and function call is given here.
Function Definition: The functions named updateincome and updateexpense
are defined, as given in the code below:
functions
{
void updateincome()
{
x = 0;
for each rec in Income
{
x = (x + rec.Amount_of_Income);
}
aih = Amount_In_Hand [ID != 0];
aih.Total_Income = x;
}
void updateexpense()
{
x = 0;
for each rec in Expense
{
x = (x + rec.Expense_Amount);
}
aih = Amount_In_Hand [ID != 0];
aih.Total_Expense = x;
}
}
|
Function Call: The functions named updateincome and updateexpense
is called and executed from Form action -> on add, on edit and on delete
scripts of the Income form and Expense form.
6. Email Notification: Function to send
mail to selected records in a view.
Function Definition
void Email.EmailNotification(string toaddress)
{
sendmail
(
To : input.toaddress
From : zoho.adminuserid
Subject : "Subject of the email"
Message : "Your message"
)
}
|
Custom Action
The function Email Notification is configured as Custom Action in
the View definition. The value of the EmailId field in the form is
passed as the argument value.
custom actions
(
"Send Mail" : Email.EmailNotification(toaddress = EmailId)
)
|
7. Update Field Value: Function to update
the value of Travel_Status field in the SampleForm to "Confirmed",
for selected records in the view.
Function Definition
void test.ConfirmTrip(int id) { rec = SampleForm [ID == input.id]; rec.Travel_Status = "Confirmed"; }
|
Custom Action
The function ConfirmTrip is configured as Custom Action in the View
definition. The value of the ID field of the selected records will
be passed as the argument value.
custom actions ( "Confirm" : test.ConfirmTrip(id = ID) )
|
Note: |
Change the form and field names based on your requirement. |
|