• J S
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
I have two custom fields country and state  are picklist fields in one custom object. Now I want to make state is dependent on country field. How can I achieve this by using apex class(extentions) and standard controller in VF Page ?
  • March 03, 2016
  • Like
  • 0
I have two custom fields country and state  are picklist fields in one custom object. Now I want to make state is dependent on country field. How can I achieve this by using apex class(extentions) and standard controller in VF Page ?
  • March 03, 2016
  • Like
  • 0
trigger  countNoOfContact on Contact (before insert,after insert,after update,after delete,after undelete) {
   
 
      Set<ID> setAccId=new Set<ID>();
       Set<ID> oldAccId = new Set<ID>();
   Map<Id,Integer> newContactMapping=new Map<Id,Integer>();
 
    if((trigger.IsInsert)||(trigger.IsUndelete))
    {
        
    for(Contact con1:trigger.new)
    {   
       if(con1.AccountId!=null)
       {
          setAccId.add(con1.AccountId);
           if(newContactMapping.containsKey(con1.AccountId)){
               newContactMapping.put(con1.AccountId,  newContactMapping.get(con1.AccountId)+1);
              
           }
           else
           {
              newContactMapping.put(con1.AccountId, 0);  
           }
          
       }
   
   } 
    }
   
    else if((trigger.IsDelete)){
       
      for(Contact con1:trigger.old)
    {   
       if(con1.AccountId!=null)
       {
          setAccId.add(con1.AccountId);
           if(newContactMapping.containsKey(con1.AccountId)){
               newContactMapping.put(con1.AccountId,  newContactMapping.get(con1.AccountId)+1);
              
           }
           else
           {
              newContactMapping.put(con1.AccountId, 0);  
           }
          
       }
   
   }   
       
   
    }
   
    else if(trigger.IsUpdate)
       
    {
      
        for(Contact con1:trigger.new)
        {
          
            if(con1.AccountId!=trigger.oldMap.get(con1.id).AccountId)
            setAccId.add(con1.AccountId);
            oldAccId.add(trigger.oldMap.get(con1.id).AccountId);
            if(con1.AccountId!=null)  
               
            {     
               
                if(newContactMapping.containsKey(con1.AccountId)){
               newContactMapping.put(con1.AccountId,  newContactMapping.get(con1.AccountId)+1);
              
           }
           else
           {
              newContactMapping.put(con1.AccountId, 0);  
           }
            }
           
           
        }
       
    
    }
           
       
       
       Map<ID,Account> mapToAccount=new Map<ID,Account>([select id,No_Of_Contacts__c,(select id from Contacts )from Account where id in:setAccId ]);
       Map<ID,Account> mapToAccount1=new Map<ID,Account>([select id,No_Of_Contacts__c,(select id from Contacts)from Account where id in:oldAccId ]);
   
   
   
                for(ID accId:mapToAccount.keySet())
                {
                    Account acc=mapToAccount.get(accId);
                    if(trigger.IsInsert){
                    acc.No_Of_Contacts__c=acc.Contacts.size()+ newContactMapping.get(accId);
                    }
                    else{
                         acc.No_Of_Contacts__c=acc.Contacts.size()- newContactMapping.get(accId);
                    }
                }      
                   
                   
                 update mapToAccount.values();

             for(ID accId:mapToAccount1.keySet())
                {
                    Account acc=mapToAccount1.get(accId);
                    if(trigger.IsInsert){
                    acc.No_Of_Contacts__c=acc.Contacts.size()+ newContactMapping.get(accId);
                    }
                    else{
                         acc.No_Of_Contacts__c=acc.Contacts.size()- newContactMapping.get(accId);
                    }
                }      
                   
                   
                 update mapToAccount1.values();   
                       
}