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
bsil bsilbsil bsil 

System.ListException: Before Insert or Upsert list must not have two identically equal elements

I am getting the above error in the fllowing code how can i avoid this error..

Can any body suggest me how to fix this error

Apex Code:

       list<Lead_Referral__c> conlist=new list<Lead_Referral__c>();
       
        conlist = [select id, OwnerId,Contact_Name__c from Lead_Referral__c where id =: 'a06K0000009qFoT' or id =: 'a06K000000A4Yn9' or id                         =:'a06K0000009qG3s'];       
        Task LFUActivity = new Task();
        list<Task> LFUActlist = new list<Task>();
          
for(integer j=0;j<conlist.size();j++){
    LFUActivity.ownerId = conlist[j].OwnerId;
                LFUActivity.ActivityDate = system.today();
                LFUActivity.Priority = 'low';
                LFUActivity.Status = 'Completed';
                LFUActivity.Subject = 'JELD-WEN Lead Status Update Request Sent';
                LFUActivity.Type = 'Automated Follow Up';
                LFUActivity.WhatId = conlist[j].id;
                LFUActivity.WhoId = conlist[j].Contact_Name__c;
                LFUActivity.description = 'body1';
                LFUActlist.add(LFUActivity);
            }
           
             insert LFUActlist;


Thanks In Advance..
Tushar sharmaTushar sharma
In your list inserting  two same record for upsert use Set for remove the duplicacy
bsil bsilbsil bsil
thanks for replay..


LFUActivity.WhatId = conlist[j].id;

WhatId is different for all records..

Virendra ChouhanVirendra Chouhan
Hello

Just try to initialization of LFUActivity into the for loop.
i.e
list<Task> LFUActlist = new list<Task>();
          
for(integer j=0;j<conlist.size();j++){
  Task LFUActivity = new Task();
    LFUActivity.ownerId = conlist[j].OwnerId;
                LFUActivity.ActivityDate = system.today();
                LFUActivity.Priority = 'low';
                LFUActivity.Status = 'Completed';
                LFUActivity.Subject = 'JELD-WEN Lead Status Update Request Sent';
                LFUActivity.Type = 'Automated Follow Up';
                LFUActivity.WhatId = conlist[j].id;
                LFUActivity.WhoId = conlist[j].Contact_Name__c;
                LFUActivity.description = 'body1';
                LFUActlist.add(LFUActivity);
            }
           
             insert LFUActlist;
you  got this error becasue in list the same record is inserted twise.


Regards
Virendra