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
SFDC Admin 56SFDC Admin 56 

Before Insert or Upsert list must not have two identically equal elements()

Before Insert or Upsert list must not have two identically equal elements
When i am trying to update records from data loader, its giving me this error.Before Insert or Upsert list must not have two identically equal elements.
Below is my code and how can i change List to map?



trigger Sharingupdate on Account (after update) 
{
 Legal_Doc__Share PrimarysalesrepShr = new Legal_Doc__Share();{

    if(trigger.isUpdate)
    {
        Set<Id> setAcc = new Set<Id>();
        Set<String> setPrimSal= new Set<String>();
        for (Account a : Trigger.old )
        {
            setAcc.add(a.id);
            setPrimSal.add(a.Primary_Sales_Rep2__c);
        }
        
        List<Legal_Doc__c> legaldocu = [Select id From Legal_Doc__c where Accounts__c in : setAcc];
        List<Legal_Doc__Share> legdoc = [Select UserOrGroupId From Legal_Doc__Share Where UserOrGroupId in :setPrimSal and ParentId = :legaldocu ];
    
        if(legdoc!= null)
        {
            Delete legdoc;
        }
    } 
        
    List<Legal_Doc__Share> ListPrimarysalesrepShr = new List<Legal_Doc__Share>();
                
    Set<ID> setAccId = new Set<Id>();
    for (Account an : Trigger.new )
    {
        setAccId.add(an.id);
    }
    List<Legal_Doc__c> Listlegaldocum = [Select id,Accounts__c From Legal_Doc__c where Accounts__c in :setAccId];
    Map<Id,Legal_Doc__c> mapAccWiseLegalIDoc = new Map<Id,Legal_Doc__c>();           
    for(Legal_Doc__c leg : Listlegaldocum)
    {
        mapAccWiseLegalIDoc.put(leg.Accounts__c,leg);
    }
    
    for (Account an : Trigger.new )
    {
        
        If(mapAccWiseLegalIDoc.containsKey(an.Id))
        {
           
            PrimarysalesrepShr.ParentId = mapAccWiseLegalIDoc.get(an.Id).Id;
            PrimarysalesrepShr.UserOrGroupId = an.Primary_Sales_Rep2__c ;
            PrimarysalesrepShr.AccessLevel = 'edit';
            
            ListPrimarysalesrepShr.add(PrimarysalesrepShr);
        }
    }
    if (PrimarysalesrepShr.UserOrGroupId!=null){
    if(ListPrimarysalesrepShr.size() > 0 )
    {
        insert ListPrimarysalesrepShr ;
    }
}

}
}
Nayana KNayana K
trigger Sharingupdate on Account (after update) 
{
 Legal_Doc__Share PrimarysalesrepShr = new Legal_Doc__Share();

    if(trigger.isUpdate)
    {
        Set<Id> setAcc = new Set<Id>();
        Set<String> setPrimSal= new Set<String>();
        for (Account a : Trigger.old )
        {
            setAcc.add(a.id);
            setPrimSal.add(a.Primary_Sales_Rep2__c);
        }
        
        List<Legal_Doc__c> legaldocu = [Select id From Legal_Doc__c where Accounts__c in : setAcc];
        List<Legal_Doc__Share> legdoc = [Select UserOrGroupId From Legal_Doc__Share Where UserOrGroupId in :setPrimSal and ParentId = :legaldocu ];
    
        if(!legdoc.isEmpty())
        {
            Delete legdoc;
        }
    } 
        
    List<Legal_Doc__Share> ListPrimarysalesrepShr = new List<Legal_Doc__Share>();
                
    Set<ID> setAccId = new Set<Id>();
    for (Account an : Trigger.new )
    {
        setAccId.add(an.id);
    }
    List<Legal_Doc__c> Listlegaldocum = [Select id,Accounts__c From Legal_Doc__c where Accounts__c in :setAccId];
    Map<Id,Legal_Doc__c> mapAccWiseLegalIDoc = new Map<Id,Legal_Doc__c>();           
    for(Legal_Doc__c leg : Listlegaldocum)
    {
        mapAccWiseLegalIDoc.put(leg.Accounts__c,leg);
    }
    
    for (Account an : Trigger.new )
    {
        
        If(mapAccWiseLegalIDoc.containsKey(an.Id))
        {
            PrimarysalesrepShr = new Legal_Doc__Share();
           
            PrimarysalesrepShr.ParentId = mapAccWiseLegalIDoc.get(an.Id).Id;
            PrimarysalesrepShr.UserOrGroupId = an.Primary_Sales_Rep2__c ;
            PrimarysalesrepShr.AccessLevel = 'edit';
            
            ListPrimarysalesrepShr.add(PrimarysalesrepShr);
        }
    }
    if (PrimarysalesrepShr.UserOrGroupId!=null){
    if(!ListPrimarysalesrepShr.isEmpty())
    {
        insert ListPrimarysalesrepShr ;
    }
}

 
SFDC Admin 56SFDC Admin 56
Thank you for your response, but i want to change this list to Map  to avoid the error.

List<Legal_Doc__Share> ListPrimarysalesrepShr = new List<Legal_Doc__Share>();

insert ListPrimarysalesrepShr