Auto-population of data, data correction, formula computation, field validation, and custom error message
Requirement
View Solution using Client Script
The Context
ABC is a leading Insurance company in India. The company uses Zoho CRM for managing their customer relationships, and all the details of their customers are saved in the Zoho CRM. The insurance agent saves details about the policyholders into their CRM account. To add data validations, to add custom error messages, to do complex calculations, and to do data correction the company has come up with a list of requirements.
Requirement
The policyholder information is saved in a custom module named Policyholder. Date of Birth, Age, Category, first name, and last name are the custom fields in this module.
When the Date of Birth of the policyholder is entered, then auto-populate the field Age.
If the calculated age is more than 60, then auto-populate the field Category as Senior Citizen otherwise populate as Citizen.
If the calculated age is more than 80, then display error message "The policyholder should not be aged more than 80".
The first name and last name entered should be in the upper case irrespective of the case used while entering the details.
Go To Solution
Solution
Below are the factors that define the configurations for a Client Script.
The module for which the Client Script should run.
In this case, the module is Policyholder, all these requirements are based on field events and the scripts should get executed on the create page. The first three requirements are based on the field Date of Birth and the last one is based on First Name and Last Name. We can handle these requirements using the below Client Scripts.
1. Client Script with field event for Date of Birth
This script is to calculate and populate the field Age based on the value entered in the field Date of birth, populate the field Category based on the calculated age and to display custom error message when the age is above 80. Create a new script by specifying the below values for name, description, event, page, and module. Refer to Creating a Client Script for more details.
Script:
functiongetAge(dateString)
{
var today = new  Date();
var birthDate = new  Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate()))
{
age--;
}
return age;
}
let age = getAge(value);
var age_field = ZDK.Page.getField("Age");
var category_field = await ZDK.Page.getField("Category");
age_field.setValue(age);
if (age > 80) {
age_field.showError("The policyholder age is more than 80");
} elseif (age > 60) {
category_field.setValue("Senior Citizen");
} else {
category_field.setValue("Citizen");
}
Here is how the Client Script works.
2. Client Script with field event for First Name
This script is to convert the value entered in the First Name field to upper case. Create a new script by specifying the below values for name, description, event, page, and module. Refer to Creating a Client Script for more details.
Script:
var first_name = ZDK.Page.getField("Name");
first_name.setValue(value.toUpperCase());
Here is how the Client Script works.
3. Client Script with field event for Last Name
This script is to convert the value entered in the Last Name field to upper case. Create a new script by specifying the below values for name, description, event, page, and module. Refer to Creating a Client Script for more details.
Script:
var last_name = ZDK.Page.getField("Last_Name");
last_name.setValue(value.toUpperCase());
Here is how the Client Script works.
Hint:
As all the requirements are on the same page (create page in this case), instead of creating three different Client Scripts with field events, you can create a single Client Script with onChange page event. If it is an onChange Page Event, the script will run for every change that happens on this page.
If it is an onChange Field Event, the script will run only when that specific field gets updated.
Script:
//To calculate Age and populate Age and Categoryif(field_name == "Date_of_Birth") {
functiongetAge(dateString) {
var today = newDate();
var birthDate = newDate(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if(m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
var age_field = ZDK.Page.getField("Age");
var field_dob = ZDK.Page.getField("Date_of_Birth");
let age = getAge(field_dob.getValue());
age_field.setValue(age);
var category_field = ZDK.Page.getField("Category");
if(age > 80) {
age_field.showError("The policyholder should not be aged more than 80");
} elseif(age > 60) {
category_field.setValue("Senior Citizen");
} else {
category_field.setValue("Citizen");
}
}
//To convert first name to upper caseelseif(field_name == "Name") {
var first_name = ZDK.Page.getField("Name");
first_name.setValue(first_name.getValue().toUpperCase());
}
//To convert last name to upper caseelseif(field_name == "Last_Name") {
var last_name = ZDK.Page.getField("Last_Name");
last_name.setValue(last_name.getValue().toUpperCase());
}