In this is post , I have given very simple examples for learning salesforce Apex Triggers concepts.
Apex Triggers saves lots of time involving in automating complex businuess process
Examples
- Use case 1:
when account is inserted, automatically contact also created for that account.
{
Contact cont = new Contact( );
}
- Explanation:
Above one is a simple trigger to insert contact when you create account . the text which is mentioned in green color is trigger name. And text which is mentioned in red color is event name.
Trigger. New is a context variable. which returns a list of new records of the sobjects which we are going to insert into the database.
- Use Case 2:
- Create a trigger on opportunity that throws
error if Amount is less than 5000
{
{
}
}
- Explanation:
Above one is a simple trigger which fires if Opportunity values is less than 5000 k. This is trigger triggers every time when record is created and updated
- Use Case 3:
- If a New record is created, Amount should not be less than 5k If an Existing record is being updated
, Amount should not be less than 3k
{
{
}
}
4. Use Case 4:
- Create a contact record on New Opportunity Insert
{
Contact c = new Contact( );
{
}
}
AIDE:
AIDE (Advanced Intrusion Detection Environment) is a file and directory integrity checker
It creates a database from the regular expression rules that it finds from the config file(s). Once this database is
Installation Procedure:
1. Install AIDE package on CentOS/RHEL:
# yum install -y aide
2. Check and adjust aide configuration file to fulfill your needs:
# vim /etc/aide. conf
3. Initialize AIDE database - it will scan all the files in folders that were included in the config file and save their hash as well as attributes info
Once initialized we may see below line
### AIDE database at /var/lib/aide/aide. db . new . gz initialized.
4. we need to modify newly created database with mv command
5. Check the database before making any changes
6. Make any changes Ex : touch /usr/sbin/mytestfile.txt( just relating any file to test)
7. You may consider keeping golden copy of AIDE database (default is set to /var/lib/aide/aide. db . gz ) is secure and read-only location. It will allow you to compare current system integrity to the golden copy.
To check what changed run:
# aide -C
A popular host-based intrusion detection system on Linux is tripwire . This software can keep track of many different filesystem data points in order to detect whether unauthorized changes have occurred.
In this article, we will discuss how to install and configure tripwire and Commands for installation
Install Tripwire.
# yum install tripwire -y
Creating site and local keyfile pass-phrases
# tripwire -setup-keyfiles
Initializing Tripwire Database
# tripwire --init
Modifying Tripwire policy file
# vi /etc /tripwire /twpol.txt
Once modifying all the files, update the tripwire policy file.
# tripwire --update-policy --secure-mode low /etc/tripwire /twpol.txt
Checking for any changes in files/directories
# tripwire --check --interactive
It will ask you to enter passwords
Now let us add a new file called Anil.
# touch Anil
Now check this file with tripwire ? check ? interactive command. You may find the file Anil under the Added section in the result
# tripwire --check --interactive
OR
# tripwire --check
It will not ask you to enter password
Viewing the tripwire report file
All tripwire report files having extension . twr are stored in /var/lib/tripwire /report/ directory. These are not text files, so you can? t view them using any editor. First convert them using the following command to human readable format.
# twprint --print-report --twrfile /var/lib/tripwire/report/server.ostechnix.com-20130510-124159.twr > /tmp/twrreport.txt
Now open the file using any editor.
# vi /tmp/twrreport.txt
Viewing tripwire configuration and policy file locations
To view the policy file locations enter the following command.
#twadmin --print-polfile
To view the configuration files enter the following command.
# twadmin --print-cfgfile
Scheduling Tripwire Check
You may find a cron file tripwire -check might be created automatically in the /etc/cron . daily/ directory. If it isn ? t created, open your crontab file and add lines as shown below. The following example will execute the tripwire daily at 5 am.
# vi /etc/crontab
# Tripwire Monitor process
00 5 * * * /usr/sbin/tripwire --check
OR
0 7 * * * /usr/sbin/tripwire --check > /mnt/tripwire
0 8 * * * /bin/mail -s "Tripwire" amullapudi@ciphercloud.com < /tmp/tripwire
It will check system at 7 AM of everyday, everyweek , every month, every year and it will send an email on same .
========================================================================
Let’s consider the following for easy understanding of tutorial
DB DB Name: admindb
DB UserName : postgres
DB Backup:
Backup: $ pg_dump -U postgres admindb -f admindb.sql
Config DB Restore:
Restore: $ psql -U postgres -d admindb -f admindb.sql
Backup a single postgres table:
Example: test.sql and test1.sql from admindb
pg_dump --table test -U postgres admindb -f test.sql
pg_dump --table test1 -U postgres admindb -f test1.sql
Restore a single table to config DB( admindb ):
Introduction to Triggers:
Apex Trigger is an action which gets fired on the particular event. In salesforce trigger is apex code that executes before or after the below types of operations.
- Insert
- Update
- Delete
Undelete
Triggers will run before object records are inserted, updated, deleted into the database or after records are inserted, updated, deleted and restored.
- Apex Triggers can be classified into two types:
- Before triggers can be used to update or validate record values before they are saved to the database.
- After triggers can be used to access field values that are set by the database, and to effect changes in other records.
- Events in triggers:
- Before Insert, Before Update, Before Delete
- After Insert, After Update, After
, After Undeletedelete
- When to use “before”
“after” triggers:vs
- Syntax to create
:sample trigger - Use below syntax to create
.trigger trigger ObjectName (<eveon s>){nt
// Code Goes Here :)
}
- Trigger Context Variables:
- All tri
gge rs define implicit variables that allow users to access run time context. These variables are contained in the ystem.trigger class.S - T
igger.Newr - T
igger.oldr - T
igger.NewMapr - T
r igg er. OldMap - T
r igg er.i sAfter - T
r igg er.is Before - T
r igg er.is Insert - T
r igg er.is Update - T
r igg er.is Delete - T
r igg er.isUn delete.
========================================================================