Sunday, December 22, 2019

Basic Trigger examples


Basic Trigger examples


 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.
}

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.
}

 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.
}

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.”);
}
}
}

when the leads are inserted into the data base it would add Doctor prefixed for all lead names. This is applicable for both inserting and updating the lead records


trigger PrefixDoctor on Lead (before insert,before update)
{

 List<Lead> leadList = trigger.new;

 for(Lead l: leadList)

 {

 l.firstname = 'Dr.'+ l.firstname;

 }


 }


The following trigger adds the Opportunity Owner into the sales team automatically whenever the Opportunity is created.

trigger OppTeam on Opportunity (after insert) {
 List<OpportunityShare> sharesToCreate = new List<OpportunityShare>();

 List<OpportunityTeamMember> oppteam = new List<OpportunityTeamMember

 > ();

 OpportunityShare oshare = new OpportunityShare();

 oshare.OpportunityAccessLevel = 'Edit';

 oshare.OpportunityId = trigger.new”0”.Id;

 oshare.UserOrGroupId = trigger.new”0”.createdbyid;

 sharesToCreate.add(oshare);

 OpportunityTeamMember ot = new OpportunityTeamMember();

 ot.OpportunityId = trigger.new”0”.Id;

 ot.UserId = trigger.new”0”.OwnerId;

 ot.TeamMemberRole = 'Account Manager';

 oppteam.add(ot);

 if(Oppteam!=null && Oppteam.size()>0)

 {

 insert Oppteam;

 }

 if(sharesToCreate!=null && sharesToCreate.size()>0)

 {

 list<Database.SaveResult> sr = Database.insert(sharesToCreate,false);

}


 }

Create “Sales Rep” field with data type (Text) on the Account Object. When we create the Account record, the Account Owner will be automatically added to Sales Rep field. When we update the Account owner of the record, then also the Sales Rep will be automatically updated.

triggerUpdateSalesRep on Account (Before insert,Before Update){
Set<Id>setAccOwner=new Set<Id>();
for(Account Acc: trigger.new)
{
setAccOwner.add(Acc.OwnerId);
}
Map<Id,User>User_map = new Map<Id,User>(“select Name from User where id
in:setAccOwner”);
for(Account Acc: Trigger.new)
{
User usr=User_map.get(Acc.OwnerId);
Acc.sales_Rep1__c=usr.Name;
}
}

No comments:

Post a Comment