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
Sanu VermaSanu Verma 

when i try to create task and events in same opportunity in test class duplicate id error occured.

In a test class i tried to create task and events in same opportunity, but when i tried to do so duplicate id error occured.
Here is my code:
Opportunity opp1 = UtilityTest.getOpportunity(acc.Id);
            insert opp1;
            
            Task[] tasks = new Task[]{
                new Task(Subject = 'Call',Ownerid=userInfo.getUserId(),
                         Status = 'In Progress', WhatId = opp1.Id,
                         Priority = 'Normal'),
                    new Task(Subject = 'Call',Ownerid=userInfo.getUserId(),
                             Status = 'In Progress', WhatId = opp1.Id,
                             Priority = 'Normal')
                    };
            insert tasks;
             
            Event[] evs = new Event[]{
                new Event(Subject = 'Call',Ownerid=userInfo.getUserId(), WhatId = opp1.Id,
                          StartDateTime = date.today()+1,EndDateTime = Date.today()+2),
                    
                    new Event(Subject = 'Call',Ownerid=userInfo.getUserId(), WhatId = opp1.Id,
                              StartDateTime = date.today()+1,EndDateTime = Date.today()+2) 
                    };
			   
				insert evs;       
			 
            update opp1;

 
Sohan Raj GuptaSohan Raj Gupta
Hi Sanu,

I tested your code and its working correct. I think some problem in your class UtilityTest.

Check below code for your reference. And again you face same issue then please share UtilityTest class to me.
 
@isTest
public class testOpportunity
{
    public static testMethod void testOpportunityData(){
        Account acc = new Account();
        acc.Name= 'Test' ;
        insert acc;
        
        //Opportunity opp1 = UtilityTest.getOpportunity(acc.Id);
        Opportunity opp1 = new Opportunity();
        opp1.Account = acc;
        opp1.Name = 'test';
        opp1.CloseDate = System.today();
        opp1.StageName = 'Qualification';
            insert opp1;
            
            Task[] tasks = new Task[]{
                new Task(Subject = 'Call',Ownerid=userInfo.getUserId(),
                         Status = 'In Progress', WhatId = opp1.Id,
                         Priority = 'Normal'),
                    new Task(Subject = 'Call',Ownerid=userInfo.getUserId(),
                             Status = 'In Progress', WhatId = opp1.Id,
                             Priority = 'Normal')
                    };
            insert tasks;
             
            Event[] evs = new Event[]{
                new Event(Subject = 'Call',Ownerid=userInfo.getUserId(), WhatId = opp1.Id,
                          StartDateTime = date.today()+1,EndDateTime = Date.today()+2),
                    
                    new Event(Subject = 'Call',Ownerid=userInfo.getUserId(), WhatId = opp1.Id,
                              StartDateTime = date.today()+1,EndDateTime = Date.today()+2) 
                    };
			   
				insert evs;       
			 
            update opp1;
    }
}

Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta 
Sanu VermaSanu Verma
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, EventTrigger: execution of AfterInsert

caused by: System.ListException: Duplicate id in list: 00629000006fhxBAAQ
every time this error occured and this is my Event Trigger
in this method where i want to create task and events in opp".deleteMaxDateCollection".
trigger EventTrigger on Event(after insert,after update, after undelete, after delete){

 if(Trigger.isDelete){
        LatestCollectionActivityOnOpportunity.deleteMaxDateCollection(null,Trigger.new);
    }
}