Friday, March 15, 2013

Triggers on Account and Contact Objects

Trigger on Account:

trigger asccounttrig on Account (after insert) {
    /*
    Account ac = trigger.new[0];
    System.debug('-------->'+ac);
 
    //insertcontactfromtrigger.insertcon(ac.id);
 
     Contact obj = new Contact();
        obj.accountid = ac.id;    
        obj.lastname = ac.name;
        obj.phone= ac.phone;
        insert obj;
      */

      //Bulk Trigger on Accounts inserted from Debug log
   List<Account> lst =new List<Account>();
   for(Account acc : trigger.new){
       lst.add(acc);
                                                 }  
 
   System.debug('---------> inserted accounts'+lst);
   List<Contact> lstcon =new List<Contact>();
   for(account a:lst){
       System.debug('---------->'+a);
       Contact c = new Contact();
       c.lastname = a.name;
       c.accountid = a.id;
       c.phone = a.phone;
       lstcon.add(c);
                            }
       insert lstcon;
                                                                    }

Trigger on Contact:

trigger contactTrig on Contact (before insert, before update, after insert, after update) {
   
    Contact c = Trigger.new[0];
    System.debug('-------------->Before inserting into db--'+c);
   
    if(Trigger.isBefore && Trigger.isInsert){
    System.debug('------------->before isnert');
        if(c.MobilePhone != null && c.email == null){
            c.addError('Please enter your email id.. ');
                                                                            }
            c.LeadSource = 'Web';
                                                                }
   
    if(trigger.isbefore && Trigger.isUpdate){
           System.debug('----------->New record'+Trigger.new[0]);
           System.debug('----------->Old record'+Trigger.old[0]);    
                                                                }
   
    if(Trigger.isAFter && (Trigger.isInsert|| Trigger.isUpdate)){
        System.debug('------------>after insert');
        classfromtrigger obj =new classfromtrigger();
        obj.updaterec(c.id);       
                                                                                         }  
                                                                                                       }

Apex Class for above Contact trigger:

public class classfromtrigger{

    public void updaterec(string cid){
        System.debug('----------->Method called from Apex Trigger');
        System.debug('----------->Insrted Contact id is:'+cid);
       
        Contact c =[Select id, name, mobilephone from Contact where id=:cid];
        System.debug('------------> Insrted record '+c);
        if(c.mobilephone == null){
        c.mobilephone='123456789';
        update c;
                                              }
                                                     }
                                        }

Trigger on Custom Object:


trigger primarytrig on Test1__c (before insert,before update) {

//Test1__c t = trigger.new[0];
List<Test1__c> lst = new List<Test1__c>();
for(Test1__c t : trigger.new){
if(t.Primary__c == true){

Test1__c oldrec= [Select id, primary__c from Test1__c where primary__c = true];
oldrec.Primary__c = false;
lst.add(oldrec);
}
}
update lst;
}

No comments:

Post a Comment