+ Start a Discussion
Jim MontgomeryJim Montgomery 

trigger not firing on before update. After Insert works fine

Here is my code. Will work on insert, but not on befroe update.
trigger SoftwareOwnerEventRollup on Event (after insert, before update, after delete) {
   
    
    if(Trigger.isAfter && (Trigger.isInsert || Trigger.isDelete)){
    Event T = [select Id,subject, WhatId from event where Id = :trigger.new[0].Id];   
    if(trigger.new[0].Id!=null){
    if(T.whatID!=null){
    String WhatId = t.WhatId;
    
    if(Whatid.startswith('001')  || Whatid.startswith('006')){
    
        
        Account a = [select id,software_Owner_meeting_demo__c,Software_Owner__C from Account where id =: trigger.new[0].AccountId];
        Id SoftwareOwner = a.software_Owner__c;
        if(softwareOwner != NULL){
        user u = [select id, custodial_rep__c from user where ID =:SoftwareOwner]; 
    Id LoggedUserId;
       Id CustodialRep = u.custodial_rep__c;
       if(custodialRep!=null){
       LoggedUserId = u.custodial_rep__c;
       }
       else  {
       LoggedUserId = a.software_owner__c;
       }
         
        Integer i = [select count() from Event where AccountId =: trigger.new[0].AccountId and OwnerId = :LoggedUserId and ActivityDate >= 2018-01-01];
        
        a.Software_Owner_meeting_demo__c=i;
        update a;
        
        
        }
        }
        
  }
  }
  }
  
  if (trigger.isBefore && trigger.isUpdate){
  
      For(Event T : trigger.new){
      if(T.WhatID!=null){
    
    String WhatId = t.WhatId;
    
    if(Whatid.startswith('001') || Whatid.startswith('006')){      
     
    
        Account a = [select id,software_Owner_meeting_demo__c,Software_Owner__C from Account where id =: trigger.new[0].AccountId];
        Id SoftwareOwner = a.software_Owner__c;
        if(softwareOwner != NULL){
        user u = [select id, custodial_rep__c from user where ID =:SoftwareOwner]; 
    Id LoggedUserId;
       Id CustodialRep = u.custodial_rep__c;
       if(custodialRep!=null){
       LoggedUserId = u.custodial_rep__c;
       }
       else  {
       LoggedUserId = a.software_owner__c;
       }
         
        Integer i = [select count() from Event where AccountId =: trigger.new[0].AccountId and OwnerId = :LoggedUserId and ActivityDate >= 2018-01-01];
        
        a.Software_Owner_meeting_demo__c=i;
        
        
        
        }
        }
        
  
  }
   }
   }
   }
Best Answer chosen by Jim Montgomery
Rahul.MishraRahul.Mishra
Hi,

There are two things you have to do.
1. Since you are updating the Account record related to Event, in such case trigger should be After Update, we are never allowed to perform DML in before triggers, it is useful only when you have to update Event it self.
2. You forgot to write Update a;, you have to perform DMl in update block also to update the Account record when Event is updated.

Thanks,
Rahul

Mark my answer as best if it does solves your problem.

All Answers

Rahul.MishraRahul.Mishra
Hi,

There are two things you have to do.
1. Since you are updating the Account record related to Event, in such case trigger should be After Update, we are never allowed to perform DML in before triggers, it is useful only when you have to update Event it self.
2. You forgot to write Update a;, you have to perform DMl in update block also to update the Account record when Event is updated.

Thanks,
Rahul

Mark my answer as best if it does solves your problem.
This was selected as the best answer
Jim MontgomeryJim Montgomery
Thank you!