+ Start a Discussion
Jim MontgomeryJim Montgomery 

trigger on task not updating account when initiated from contact record

Here is my trigger code. Should count the number of events associated to the account the contact belongs to.
It is not updating the account.

trigger SoftwareOwnerEventRollupContact on Event (after insert, after update, after delete) {
    
    if(Trigger.isAfter && Trigger.isInsert){
    Event T = [select Id,subject, WhoId from event where Id = :trigger.new[0].Id];   
    if(trigger.new[0].Id!=null){
    if(T.whoID!=null){
    String WhoId = t.WhoId;
    
    if(Whoid.startswith('003')){
    
       Contact C = [select id, accountid from contact where id = :WhoId];
        
        Account a = [select id,software_Owner_meeting_demo__c,Software_Owner__C from Account where id =:C.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 = :C.AccountId and OwnerId = :LoggedUserId and ActivityDate >= 2018-01-01];
        
        a.Software_Owner_meeting_demo__c=i;
        update a;
        
        
        }
        }
        
  }
  }
  }
}
Alain CabonAlain Cabon

Integer i = [select count() from Event where AccountId = :C.AccountId and OwnerId = :LoggedUserId and ActivityDate >= 2018-01-01];

Calculation of the Account ID of an activity:  https://help.salesforce.com/articleView?id=000194177&type=1

Are you sure of the query above otherwise you can try:

Integer i = [select count() from Event where WhatId = :C.AccountId and OwnerId = :LoggedUserId and ActivityDate >= 2018-01-01];
 
Rahul.MishraRahul.Mishra
Hi Could you please try below code and let me know if it works for you.
trigger SoftwareOwnerEventRollupContact on Event (after insert, after update, after delete) {
    
    if(Trigger.isAfter && Trigger.isInsert){
        
        Event evntRecord = [select Id,subject, WhoId from Event where Id = :trigger.new[0].Id];   
        if(trigger.new[0].Id!=null || evntRecord != null){
            if(evntRecord.whoID!=null){
                String WhoId = evntRecord.WhoId;
                
                if(Whoid.startswith('003')){
                    
                    Contact con = [select id, AccountId from contact where id =:WhoId And AccountId != null];
                    
                    Account acc = [select id,software_Owner_meeting_demo__c,Software_Owner__C from Account where Id =:con.AccountId];
                    
                    
                    if(acc.Software_Owner__C != NULL){
                        
                        Id SoftwareOwner = acc.software_Owner__c;
                        user u = [select id, custodial_rep__c from user where ID =:SoftwareOwner]; 
                        
                        Id LoggedUserId;
                        Id CustodialRep;
                        
                        if(u.custodial_rep__c != null)
                            CustodialRep = u.custodial_rep__c;
                        
                        if(custodialRep!=null){
                            LoggedUserId = u.custodial_rep__c;
                        }
                        else  {
                            LoggedUserId = acc.software_owner__c;
                        }
                        
                        Integer i = [select count() from Event where WhoId =:con.AccountId and OwnerId =:LoggedUserId and ActivityDate >= 2018-01-01];
                        
                        if(i>0)
                            acc.Software_Owner_meeting_demo__c=i;
                        else 
                            acc.Software_Owner_meeting_demo__c=0;
                        update acc;
                        
                        
                    }
                }
                
            }
        }
    }
}


Mark my answer as best if it works for you.

Thanks,
Rahul​
Jim MontgomeryJim Montgomery
Still not updating account when added from a contact.