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
Arun 2Arun 2 

Insert account share with Rowcause value dynamically.

Hi Team,

We are trying to do apexsharing in triggers and webservices for Account Object.We can able to set sharing ID(userId or group Id),but if we set Rowcause is apart from manual its throughing error.
We are getting ​ this exception : CheckRowcause: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: RowCause (cannot insert sharing row with this cause): [RowCause]: Trigger.CheckRowcause: line 14, column 1 .

I have gone through lot of salesforce use cases docuements for this issue. I am properly using my sharing model and also I am using the latest API version i.e 35.0.

Kindly suggest the solution for the same.

Thanks 
Arun.R

 
sfdcsushilsfdcsushil
I hope rowcause value that you are using is valid and is not null
Arun 2Arun 2
Yes. We are setting up the Rowcause value.PFB code FYR..

trigger CheckRowcause on Account (after insert) {
    List<AccountShare> shares = new List<AccountShare>();
          User u = [Select id from user where Id =:'00528000000c7WG'];
    for(Account a : Trigger.new){
    
          AccountShare aShare           = new AccountShare();
          aShare.AccountId              = a.Id;
          aShare.UserOrGroupId          = u.Id;
          aShare.RowCause =Schema.Accountshare.RowCause.owner;
          aShare.AccountAccessLevel     = 'Edit';
          aShare.OpportunityAccessLevel = 'Edit';
          shares.add(aShare);
    }
insert shares;
}
Andee Weir 20Andee Weir 20
Dont set the RowCause before the insert it will then default to Manual