Thursday, 3 January 2013

Working With Triggers:-


How to create a Trigger:-

Setup--> App setup--> Any object -->Triggers

Use of Trigger:- Triggers will do automate the Process.

When the Trigger will fire:- When we did DML operations in Records , on that time based on Event 
                                         Triggers will fire.

Here we have Some Events:-

Before Insert
Before Update
Before Delete
After Insert
After Update
After Delete

Example:-

Here we Have a scenario , In contact object we have a Email field, create one field 
domain__c in contact, When we create/update a record in Contact object ,
The Domain Name of Email field should update with domain__c field in same record.

trigger DomainName on Contact (before insert,before update)
{
   if(Trigger.isinsert){
       List<String> allEmailStrings=new List<String>();
       if(Trigger.isbefore){
           for(Contact allconnew:Trigger.new){
               if(allconnew.Email!=null){
                   //List<Contact> conEmailCompare=[select id,name,email from Contact where email=:allconnew.email];  
                   String strEmail = allconnew.email;
                   String strSubEmail = strEmail.substring(strEmail.indexOf('@'),strEmail.length());
                   System.debug('Final String:========> '+ strSubEmail);
                   String strActString = strSubEmail.substring(1,strSubEmail.indexOf('.'));
                   System.debug('Final String:========> '+ strActString);
                   allconnew.domain__c=strActString;
               }
            }
        }
   }
   if(Trigger.isupdate){
        List<String> allEmailStrings=new List<String>();
        if(Trigger.isbefore){
              for(Contact allconnew:Trigger.new){
                  if(allconnew.Email!=null){
                       String strEmail = allconnew.email;
                       String strSubEmail = strEmail.substring(strEmail.indexOf('@'),strEmail.length());
                       String strActString = strSubEmail.substring(1,strSubEmail.indexOf('.'));
                       System.debug('Final String:========> '+ strActString);
                       allconnew.domain__c=strActString;
                       System.debug('transfered=========>'+allconnew.domain__c);
                   }
               }
          }
    }
     
  }


Example2:-

 We take another scnario , In contact there is a lookup field called spouse on to contact.(self lookup).
When we click on look-up field in child record, the parent record spouse field should be child record name.

Here if a contact record name='Daniel'.
And spouse in the record of daniel is 'Mary'.
if i click mary lookup feild the daniel should be the spouse in the mary record.
it means the lookup value should change vice versa.


trigger MatchSpouse on Contact (after insert, before update) {
    List<Contact> spouses = new List<Contact>();
    Contact spouse;
    for (Contact c : Trigger.new){
        if (c.Spouse__c != null){
            spouse = [select id,Spouse__c from Contact where Id=: c.Spouse__c ];
            if(spouse.Spouse__c != c.id){
                spouse.Spouse__c = c.id;
                spouses.add(spouse);
            }
        }
    }
    if(spouses.size()>0 && staticFlag.a ){
        staticFlag.a = false;
        update spouses;
    }
}



1 comment:

  1. Its a good code and very usefull.....but will you explain this code breifly by using comments line by line what operations are going.

    ReplyDelete