3

I got the following exception on a batch job:

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>

It fails SOMETIMES when inserting AccountShare objects.

    // Delete existing shares for these accts/users
    Set<ID> acctIDs = new Set<ID>(salesRelColl.getAccountIDs());
    Set<ID> userIDs = new Set<ID>(salesRelColl.getUserIDs());
    EntitlementManager.deleteSharePermissionsForAccounts(acctIDs, userIDs);
    String relatedListsAccessLevel = EntitlementManager.ACCESS_LEVEL_EDIT;
    Map<ID,Set<ID>> acctUserMapForCptyShares = new Map<ID,Set<ID>>();

    List<AccountShare> shares = new List<AccountShare>();
    List<AccountShare> sharesOwner = new List<AccountShare>();

    List<Id> accountIDs = new List<Id>();
    Map<Id, Account> accountOwnerMap = new Map<Id,Account>();

    for ( Sales_Relationship__c salesRel : salesRelColl.getList() )
    {
      accountIDs.add(salesRel.Account__c);
    }

    AccountSelector accountSel = new AccountSelector();
    AccountCollection accountCol = accountSel.whereIDIn(accountIDs).execute();

    for(Account acct : accountCol.getList())
    {
      accountOwnerMap.put(acct.Id,acct);
    }

    for ( Sales_Relationship__c salesRel : salesRelColl.getList() )
    {

      ID userID = salesRel.Employee__r.User__c;
      if ( userID == null )
      {
        continue;
      }
      // exclude any non-current
      if ( !salesRel.Current__c )
      {
        continue;
      }

      if (accountOwnerMap.get(salesRel.Account__c) != null)
      {
        if (accountOwnerMap.get(salesRel.Account__c).OwnerId != userID)
        {
          ID acctID = salesRel.Account__c;
          String groupType = ROW_CAUSE_SALESPERSON;
          ID parentID = acctID;
          ID userOrGroupID = userID;
          String accessLevel = EntitlementManager.ACCESS_LEVEL_EDIT;
          String rowCause = groupType;

          AccountShare share = EntitlementManager.buildShareForAccount(parentID,
                    userOrGroupID,accessLevel,
                    relatedListsAccessLevel,
                    rowCause,groupType);

          shares.add(share);

          if ( !acctUserMapForCptyShares.containsKey(acctID) )
          {
            acctUserMapForCptyShares.put(acctID,new Set<ID>());
          }
          acctUserMapForCptyShares.get(acctID).add(userID);
        }
        else
        {
          AccountShare share = EntitlementManager.buildShareForAccount(salesRel.Account__c,userID,EntitlementManager.ACCESS_LEVEL_EDIT,
                    relatedListsAccessLevel,
                    ROW_CAUSE_SALESPERSON,ROW_CAUSE_SALESPERSON);
          sharesOwner.add(share);
        }
      }
    }

    if ( shares.size() > 0 )
    {
      // FAILS HERE
      insert shares;

    }

The fact that is something that does not happen all the time had made this bug not easy to reproduce.

2
  • Did you take a look at this post here : salesforce.stackexchange.com/questions/6063/…
    – Rao
    Apr 8, 2014 at 20:25
  • @rao That does not seem to be the case here. That happens during deployment and more metadata related. The issue I describe happens while inserting share records in a batch. Also it does not happen all the time, which make it hard to reproduce/fix. Apr 9, 2014 at 12:21

2 Answers 2

3

Found the issue, with some help from @eyescream in the chatroom.

After some detective work I've found that this is caused by 2 batches trying to insert the same Share at the same time.

Even one of the jobs fail, the other continues and creates the share.

To be sure he suggested to use Database.insert(shares,false), which continues inserting all values from the shares list even if one of them fails.

0

I think I ran into this error before when I tried giving access to someone who already had access to the record. Say you the 'Salesperson' is already the Owner.

I'm about 95% sure your issue deals with the combination of sharing rules and Owner.

Put a try/catch around your insert shares; and debug the message it gives. Usually it gives a little more info also.

2
  • Tried to reproduce what you say on a different trigger. First error: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] Is a different error when you try to add a share to the owner. Apr 9, 2014 at 13:52
  • Ahh ok. Must have been something else because I know I recently got that error... I just can't remember from what exactly.
    – dphil
    Apr 9, 2014 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .