In the last few chapters, we started with the basics of Force.com. We saw how to create database objects, we created page layouts and tabs. Business logic is the brain of the application; it make sense of the data and also responds to the button clicks of UI.
With Force.com providing an easy way to handle the data and the visual UI, the developers can focus most of their energy and time in developing amazing business logic. As always, Force.com provides us with easy-to-use, point-and click-options to develop business logic.
In this chapter, we connect the data to the view using the controller. We will be seeing:
As mentioned in the previous chapter, we won't be covering Apex in detail, but only giving an overview. Let's start creating some robust business logic for the application.
Force.com provides some basic controls for the validation of data, automating a process, triggering an e-mail alert after a particular duration, and auditing and tracking important fields such as status, and so on.
Force.com provides us with two ways of creating a rule; the simplest way is setting the fields to values, for example, Field X equals value Y or Field A does not equal value B as shown in the following screenshot:
We can also write complex rules using formulae, which offer flexibility to use advance logic such as AND, OR, IF, and so on. The following screenshot shows the setting up of the validation rules:
There are different types of rules used in different areas. They are as follows:
The rules that are used in any of the above have a common syntax. A rule syntax consists of the following components:
Please refer to the cheat sheet of Force.com formulae for the different functions and operators provided by Force.com http://media.developerforce.com/cheatsheets/SF_formulas_developer_cs-2Pg_HR.pdf
Validation rules are attached to fields. They are executed when a record is created or updated. We define the validation rule and the condition for execution of the rule along with an error message. If the evaluation of error condition results in a true value, the record is not saved, and the error message is generated.
When we create a general field, some validation rules are set by default due to the property of the field, for example, an E-mail field checks for the pattern of @
and .
(dot) in the entered string, the number field checks for numbers entered in the string.
For custom validations, we use validation rules. Validation rules are permanently bound by the field and are universal. Every time the field is inserted it validates the field value with the validation rule.
For example, the following validation rule checks for the Social Security Number of the customer and throws an error if it is incorrect.
Validation rules are used to preserve the data quality of the data. We can use validation rules to prevent data loss in the record. We can use the formula isChange
to determine if the value in a certain field is changed and the user profile.
The general library wishes to store the social security number and a valid credit card number for the subscription. How do they ensure that the right pattern is entered in the respective fields?
To create a validation rule perform the following steps:
The formula uses basic functions, which are provided by Salesforce.com. These functions are similar to the Excel-based functions. For a complete list of functions please visit The Formula Quick reference guide at: https://na3.salesforce.com/help/doc/en/salesforce_formulas_cheatsheet.pdf
$User.City
ObjectType
.A formula is similar to a spread sheet formula field that is executed at run time. The formula can make use of existing data to create a new set of data.
Here's an example of a formula that can be used in a custom formula field on a media object. It calculates the number of days after the due date is over (assuming, we have all the fields in media object):
If(TODAY ()>BookIssueDate__c,TODAY()-BookIssueDate__c,0);
The general library has a fixed pattern in returning the media items. The members are allowed to borrow the media items for only 15 days. They want to reduce the typing work for the entry clerk and wish to auto calculate the return date of the book.
The return date will be calculated on the CustomerMedia object that will record the transaction of book issue. To create the Return Date formula:
Issue_Date__c +15
in the formula editor as shown in the following screenshot:Issue_Date__c+15
is the date field used to store the issue date for the media. We can also use the Insert Field drop-down list to insert this field. Make sure to click on Check Syntax button to compile the formula and check for errors.Every time a new record is created with the Issue Date, the Return Date field will automatically calculate and store the return date for it.
One of the most powerful features of the Force.com formula are the cross object formula fields. In cross object formula fields we can navigate up to five levels of child-parent-grandparent relationship. Formula fields are calculated on the run time according to the formula stored in the fields.
Record owner and activity fields cannot be used in a cross object formula fields. Let us create the cross object formula field for the customer object.
The general library wishes to calculate the Penalty Amount of the media resource when it is checked out late. Write a formula field that can calculate the amount on the Customer-Media object.
To create a formula field first go to the field creation dialog box on the object that we saw in Chapter 2, Creating a Database on Force.com, Force.com data-types section. Select the Formula field in the list of fields. It will open the formula creation wizard.
The formula quick reference guide on Force.com provides an effective cheat sheet for the formulae. It can be visited at https://na3.salesforce.com/help/doc/en/salesforce_formulas_cheatsheet.pdf
(TODAY() - Return_date__c )* Media__r.Loss_Fine__c
to calculate the fine as shown in the following screenshot:Roll-up summary fields are used to aggregate child information on the master object. The roll-up summary uses special functions such as SUM, MIN, MAX, and COUNT.
In the roll-up summary field, unlike the formula field, the field is placed on the master object. The values are aggregated in the object. The values are stored in the field and calculated periodically.
The general library wishes to keep a track of the total media items issued to the customer. They want the total number of items issued in a single field called Media Issued against the member record.
The media that are issued against the customer are stored in the CustomerMedia object; we will be storing their count in the Customer object.
This covers the basic formulae and the logic that help us simplify the work. In the following section we will be discussing how to automate the business processes using workflows and approval process. We will understand the use of workflow and approval process and distribute the workload to people.