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
VNVN 

Doubt in Trigger

Hi,
 
We have a requirement where we need to populate a field in Accounts page. The field will have the count of total number of events completed for this account. The trigger should get executed whenever an event is created or updated.
 
My doubt is: Is it possible to update a field in Accounts page by executing a trigger on events. If Yes, then how?
Please share your ideas on how to achieve this.
 
Thanks in advance
Priya Nair
HarmpieHarmpie
Yes, it is possible to update a field on the Account object (almost any object for that matter) with a trigger on Event.
 
How? Add the trigger to the Event object and have the trigger code manipulate the related Account.
 
For example (this is some old, unused test-code but it will give you an idea)
Code:
trigger ActivityCopyToAccount on Event (after insert, after update, after undelete) {
 
 for (Integer i = 0; i < Trigger.new.size(); i++) {
  if (Trigger.new[i] != null && Trigger.new[i].AccountId != null && 
   (
    Trigger.new[i].Meeting_Actions__c !='' || 
    Trigger.new[i].Meeting_Deliverables__c !='' || 
    Trigger.new[i].Meeting_Information__c !='' ||
    Trigger.new[i].Meeting_Other__c !='' || 
    Trigger.new[i].Meeting_Support_requested__c !='' || 
    Trigger.new[i].Meeting_Topics__c !=''
   )) {
   Account a = [select id, 
        Meeting_Actions__c, 
        Meeting_Deliverables__c, 
        Meeting_Information__c, 
        Meeting_Other__c, 
        Meeting_Support_requested__c, 
        Meeting_Topics__c  
      from Account where id = :Trigger.new[i].AccountId];
      
   a.Meeting_Actions__c = Trigger.new[i].Meeting_Actions__c;
   a.Meeting_Deliverables__c = Trigger.new[i].Meeting_Deliverables__c;
   a.Meeting_Information__c = Trigger.new[i].Meeting_Information__c;
   a.Meeting_Other__c = Trigger.new[i].Meeting_Other__c;
   a.Meeting_Support_requested__c = Trigger.new[i].Meeting_Support_requested__c;
   a.Meeting_Topics__c = Trigger.new[i].Meeting_Topics__c;
   update a; 
  } 
 } 
}

 
VNVN
Thanks a lot for clarifying my doubt.  Will try doing this.