function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kamal RehmaniKamal Rehmani 

Bulk trigger

Hi everyone,
I have just started learning apex, pls help .
I need to write a bulk trigger When Account ‘active’ field is updated, status field should be set to Active for all the contacts related to Account.




 
sandeep@Salesforcesandeep@Salesforce
Hi Kamal,

There two ways for this 
1. You can simply create a formula field on contact object refering to Account. 
2. If you really want to write trigger then bulk trigger would some thing like below:

Trigger updateContacts on Account ( before Update)
{

List<Contact> ListofCont = new List<Contact>();
  for( Account a : [select yourfield, (Select id from contacts )  from Account where id in Trigger.New ])
{
    for(contact c : a.contacts )
    {
           // if your criteria meet then make changes in c and inlcude in ListofCont 
     }
}
If(ListofCont.size()>0)
 update ListofCont ;

}

Thanks 
Sandeep Singha;
http://www.codespokes.com/
V_PV_P
Trigger updateStatusOnContacts on Account ( after Update)
{
 set<Id> accId = new set<Id>();
List<Contact> ListofConts = new List<Contact>();

 for(Account acc:Trigger.new){
    if(acc.active__c)
     accId.add(acc.Id);
 }    

for( Contact cont : [Select id, status_c from contact where id in:accId ]) {    
      a.status_c ='Active';    
      ListofConts.add(cont);
}

If(ListofConts.size()>0)
 update ListofConts ;
}

Hope this can help you .