[SalesForce] DML Exception while inserting share records

I am facing trouble while inserting 1 share record. When I am loggin to Admin Profile, it is working fine without any DML exception, but when I am login to Sales profile, then I am facing such exception.

I checked with DescriveSObject isCreatable() method, I can see that Sales profile has create access on Package_Share__c object.

This is the list I am inserting:

packageShareInsertList : (Package__Share:{UserOrGroupId=00540000000zHI5AAM, ParentId=a1I560000004iRIEAY, RowCause=PKG_Sales_Team__c, AccessLevel=Edit})

This is th DML Exception I am facing:

04:21:17.791 (3215279840)|FATAL_ERROR|System.AssertException:
Assertion Failed: System.DmlException: Insert failed. First exception
on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, invalid row
cause: []:

But Row cause has valid value that I am sure. If it was a wrong value, then other profiles would have thrown the same issue.

Please advise.

Best Answer

I believe you are having the issues because the user you are trying does not have "Modify all data" permission.

According to the documentation LINK

Apex managed sharing enables developers to pro-grammatically manipulate sharing to support their application’s behavior through Apex or the SOAP API. This type of sharing is similar to Force.com managed sharing. Only users with “Modify All Data” permission can add or change Apex managed sharing on a record. Apex managed sharing is maintained across record owner changes.

I came across another answer where this was successfully achieved via changing class's sharing model as "without sharing". Please try doing that as well.

Link to the Answer


EDIT

I believe you received error using with sharing because a user requires at-least read access to the record for which the share is to be created. LINK

If a trigger changes the owner of a record, the running user must have read access to the new owner’s user record if the trigger is started through the following:

Related Topic