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
RaoSRaoS 

Trigger giving error when creating a recurrence on a Task

Hi ,
I have created a trigger on Task that updates last activity date whenever a task is created on a Lead / contact / Opp. 

The trigger works fine when I create a single task or recurrence when the recurrence date is only for one day(Recurrence Start & Recurrence End are on same day). But when the recurrence is created for two or more days (Recurrence Start & Recurrence End are on different days Ex: Start = 01/01/2019 & End=01/02/2019) then it is throwing an error.  

Here is the error message

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger UpdateLastActivityDate caused an unexpected exception, contact your administrator: UpdateLastActivityDate: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 003c000001G7hkTAAR: Trigger.UpdateLastActivityDate: line 37, column 1

Here is the Trigger Code along with lines mentioned error in bold
trigger UpdateLastActivityDate on Task (after insert) {

    //updating Last_sales_activity__c on lead, contact and opportunity for new activities
    
     if( trigger.isAfter && trigger.isInsert  )
    {
        List<Contact> lstContactsToUpdate=new List<contact>();
        List<Lead> lstLeadsToUpdate=new List<Lead>();
        List<Opportunity> lstOpportunitiesToUpdate=new List<Opportunity>();
        
        Profile p = [ Select Id, Name from Profile where Id =: UserInfo.getProfileId() ];
        if( p.Name.equalsIgnoreCase( 'Standard Sales User' ) )
        {    
            for( Task t : trigger.new )
        {    system.debug('t whatId..'+t.WhatId);
             system.debug('t whoId..'+t.WhoId);
              if( t.WhoId != null && String.valueOf( t.WhoId ).startsWith( '003' ))//003 =contact
                {
                    contact c=new contact(Id=t.WhoId);
                    c.Last_sales_activity__c=system.today();
                    lstContactsToUpdate.add(c);
                }else if( t.WhatId != null && String.valueOf( t.WhatId ).startsWith( '006' ))//006 =opportunity
                {system.debug('t----006---');
                    Opportunity c=new Opportunity(Id=t.WhatId);
                    c.Last_sales_activity__c=system.today();
                    lstOpportunitiesToUpdate.add(c);
                }else if( t.WhoId != null && String.valueOf( t.WhoId ).startsWith( '00Q' ))//00Q =Lead
                {
                    Lead c=new Lead(Id=t.WhoId);
                    c.Last_sales_activity__c=system.today();
                    lstLeadsToUpdate.add(c); 
                }
            
            
        }
            if(lstContactsToUpdate.size()>0){
                update lstContactsToUpdate;
            }
            if(lstOpportunitiesToUpdate.size()>0){
                update lstOpportunitiesToUpdate;
            }
            if(lstLeadsToUpdate.size()>0){
                update lstLeadsToUpdate;
            }
            
        }
        
   }

 }

I could not figure what I am missing here.

Let me know what changes I need to do to overcome this error.

Thanks
Rao
Best Answer chosen by RaoS
Abdul KhatriAbdul Khatri
Please try the below code
 
trigger UpdateLastActivityDate on Task (after insert) {
     
    if( trigger.isAfter && trigger.isInsert  )
    {
        Set<Contact> setContactsToUpdate=new Set<contact>();
        Set<Lead> setLeadsToUpdate=new Set<Lead>();
        Set<Opportunity> setOpportunitiesToUpdate=new Set<Opportunity>();
        
        Profile p = [ Select Id, Name from Profile where Id =: UserInfo.getProfileId()];
        
        if( !p.Name.equalsIgnoreCase('Standard Sales User') ) return;
          
        for( Task t : trigger.new )
        {    

            if( t.WhoId != null && t.WhoId.getSObjectType() == Contact.sObjectType )
            {
                contact c=new contact(Id=t.WhoId);
                c.Last_sales_activity__c=system.today();
                setContactsToUpdate.add(c);
            }
            
            if( t.WhatId != null && t.WhatId.getSObjectType() == Opportunity.sObjectType)
            {
             	Opportunity c=new Opportunity(Id=t.WhatId);
             	c.Last_sales_activity__c=system.today();
             	setOpportunitiesToUpdate.add(c);
            }
            
            if( t.WhoId != null && t.WhoId.getSObjectType() == Lead.sObjectType )
            {
                Lead c=new Lead(Id=t.WhoId);
                c.Last_sales_activity__c=system.today();
                setLeadsToUpdate.add(c); 
            }
            
            
        }
            if(setContactsToUpdate.size()>0){
                update new List<Contact>(setContactsToUpdate);
            }
            if(setOpportunitiesToUpdate.size()>0){
                update new List<Opportunity>(setOpportunitiesToUpdate);
            }
            if(setLeadsToUpdate.size()>0){
                update new List<Lead>(setLeadsToUpdate);
            }
            
        }

}

 

All Answers

Abdul KhatriAbdul Khatri
Please try the below code
 
trigger UpdateLastActivityDate on Task (after insert) {
     
    if( trigger.isAfter && trigger.isInsert  )
    {
        Set<Contact> setContactsToUpdate=new Set<contact>();
        Set<Lead> setLeadsToUpdate=new Set<Lead>();
        Set<Opportunity> setOpportunitiesToUpdate=new Set<Opportunity>();
        
        Profile p = [ Select Id, Name from Profile where Id =: UserInfo.getProfileId()];
        
        if( !p.Name.equalsIgnoreCase('Standard Sales User') ) return;
          
        for( Task t : trigger.new )
        {    

            if( t.WhoId != null && t.WhoId.getSObjectType() == Contact.sObjectType )
            {
                contact c=new contact(Id=t.WhoId);
                c.Last_sales_activity__c=system.today();
                setContactsToUpdate.add(c);
            }
            
            if( t.WhatId != null && t.WhatId.getSObjectType() == Opportunity.sObjectType)
            {
             	Opportunity c=new Opportunity(Id=t.WhatId);
             	c.Last_sales_activity__c=system.today();
             	setOpportunitiesToUpdate.add(c);
            }
            
            if( t.WhoId != null && t.WhoId.getSObjectType() == Lead.sObjectType )
            {
                Lead c=new Lead(Id=t.WhoId);
                c.Last_sales_activity__c=system.today();
                setLeadsToUpdate.add(c); 
            }
            
            
        }
            if(setContactsToUpdate.size()>0){
                update new List<Contact>(setContactsToUpdate);
            }
            if(setOpportunitiesToUpdate.size()>0){
                update new List<Opportunity>(setOpportunitiesToUpdate);
            }
            if(setLeadsToUpdate.size()>0){
                update new List<Lead>(setLeadsToUpdate);
            }
            
        }

}

 
This was selected as the best answer
RaoSRaoS
Thank  you Abdul.  It worked.
Abdul KhatriAbdul Khatri
Can you please be kind marking the answer as the best. Thanks