You need to sign in to do that
Don't have an account?
Insert failed. First exception on row 0; first error: DUPLICATE_VALUE
I have a trigger calling an @future method that insert records to the OpportunityShare object and occasionally I will recieve this error:
System.DmlException: Insert failed. First exception on
row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown>
duplicates value on record with id: <unknown>
I am having a very difficult time isolating this issue and was curious if anyone has seen anything like this before? I have tried to replicate this error with the code below but it works everytime.
Opportunity opp = [select Id, Name from Opportunity limit 1];
User u = [select Id from User limit 1];
List<OpportunityShare> sharesToInsert = new List<OpportunityShare>();
sharesToInsert.add(new OpportunityShare(OpportunityId = opp.Id,OpportunityAccessLevel = 'Read',UserOrGroupId = u.Id));
insert sharesToInsert;
List<OpportunityShare> sharesToInsert1 = new List<OpportunityShare>();
sharesToInsert1.add(new OpportunityShare(OpportunityId = opp.Id,OpportunityAccessLevel = 'Read',UserOrGroupId = u.Id));
sharesToInsert1.add(new OpportunityShare(OpportunityId = opp.Id,OpportunityAccessLevel = 'Read',UserOrGroupId = u.Id));
insert sharesToInsert1;
I'm stumped.
Thanks,
Jason
Your code below gives a compile error, "Field is not writeable: OpportunityShare.OpportunityId".
If you are trying to define a custom sharing reason, you can only do that on custom objects, not standard objects. The only way data gets written to the OpportunityShare table is when you share an oppty via the UI.
Holy smokes! You are absolutely right. In my dev org the code above does not compile but I must be an l337 apex hacker because in our production and sandbox orgs the code above complies and executes fine with no errrors.
This concerns me a little bit has we have built some business processes and system automation around this ability.