You need to sign in to do that
Don't have an account?
RaoS
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
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
All Answers