Apex Managed Sharing – Comprehensive Guide to Sharing Records

I have an org that was recently updated to API 32, which broke some triggers that create Sharing Records. This code worked previously to the update to API 32.

public void OnAfterObjectInsert(List<Object__c> insertRecords) {
  List<Object__Share> newShares = new List<Object__Share>();
  List<AccountShare> clientShares = [SELECT Id,AccountId,RowCause,UserOrGroupId FROM AccountShare WHERE AccountId = :insertRecords[0].Client__c];
  for (AccountShare cs : clientShares) {
    if(cs.UserOrGroupId != Userinfo.getUserId()) {
      Object__Share curShare    = new Object__Share();
      curShare.ParentId         = insertRecords[0].id;
      curShare.RowCause         = 'Test__c'; //or Schema.Object__Share.RowCause.Test__c;
      curShare.UserOrGroupId    = cs.UserOrGroupId;
      curShare.AccessLevel      = 'Edit';
      newShares.add(curShare);
    }
  }
  insert newShares;
}

This is returning a DML exception. The reported error is "invalid row cause." Using Manual as the RowCause works, and interestingly inserting a record with workbench.developerforce.com with Test__c as a RowCause works as well. I've also set the api of the relevant class to 32, which had no effect. Any help is appreciated.

Best Answer

I faced and same issue today and my solution may work in this case.

What I did is, removed "with sharing" from class and worked fine. I think when we use with sharing, user doesn't have access to add the manual sharing.

Related Topic