Trigger Example scenarios:
Trigger Examples 1. Populate contact description when user creates contact
trigger ContactBeforeInsert on Contact (before insert) {
// Trigger.New hold new version of Contacts
for(Contact contact: Trigger.new){
contact.Description = ‘Contact created successfully by using ContactBeforeInsert trigger’;
}
// No Need to write DML statement, trigger. New will be take care.
}
Trigger Examples 2. Populate contact description with modified user name when user updates contact.
trigger ContactBeforeUpdate on Contact (before update) {
// Trigger.New hold new version of Contacts
for(Contact contact: Trigger.new){
contact.Description = ‘Contact updated successfully by ‘+ userInfo.getUserName() ;
}
// No Need to write DML statement, trigger. New will be take care.
}
Trigger Examples 3. How to write a trigger to inject the above 2 scenarios in one trigger
trigger ContactBeforeInsertUpdate on Contact (before insert, before update) {
// Trigger.New hold new version of Contacts
for(Contact contact: Trigger.new){
if(trigger.isInsert){
contact.Description = ‘Contact created successfully by using ContactBeforeInsert trigger’;
}
If(trigger.isUpdate){
contact.Description = ‘Contact updated successfully by ‘+ userInfo.getUserName() ;
}
}
// No Need to write DML statement, trigger. New will be take care.
}
Trigger Examples 4. Throw an error whenever the user try to delete the contact which is not associated to account
trigger contactBeforeDelete on Contact(before delete){
for(Contact contact: trigger.old){
if(contact.accountId == null){
contact.addError(“Hey! You are not authorized to perform this action.”);
}
}
}
This is Simply superb and understandable in this way i hope to increase some difficult examples step by step with some scenarios from your experience, it will make this very high.
Hi,
Your tutorials is helpful a lot.Thanks for sharing this.
The examples of Triggers EX1 & Ex2 which you presented is can we achieve this by using workflow. Please let me know.
Thanks
Example 1,2 ,3 can be achieved by workflow, process builder.
example 4 can be achieved by validation rule.
how can 4 be achieved with validation rule??
Very Interesting For Begginers..please post some scenarios using trigger.newmap and trigger.oldmap to.
As the examples are update operations can’t they be done in workflow?
great job
I am requesting to send me the few more difecult exmple step by step to encrease my handling on triggers
very interesting…….simple understandable for beginners
..
Really Helpful tutorials.please post some scenarios using trigger.newmap and trigger.oldmap to.
Thanks,
Ronak
very easy and helpfull for Begginers.. thanks…
realy so superb example……. plz post more scenario .. some complex also
Thank you so much .this is very helpfull who are really want to Sales force Triggrs
Thank you so much .it is very help full to understand what trigger is in a simple way,but i have question can we use “description” in after insert,update trigger?it gives me error.
How can i find more number of examples like this