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
sfdeveloper12sfdeveloper12 

Error introduced while sharing the records

Hi, 
I have written Account sharing and delete sharing record for custom field. Its giving me following error. Account has "Private" acess in OWD.

Account {"Party_Sea_ID__c":3618,"Customer_Number__c":"C0354","Name":"LUXURY BRAND HOLDINGS INC.","DBA_Name__c":"LUXURY BRAND HOLDINGS","Customer_Group_Id__c":"a0028000015lr6gAAA","OwnerId":"00528000005HQRPAA4","owner1__c":"00528000003iGolAAE","Terms_Id__c":"a042800000oV27IAAS","Default_Currency__c":"US DOLLARS","Industry":"Jewelry","Website":"www.luxurybrandholdings.com","No_of_Doors__c":14,"NumberOfEmployees":0,"Store_Size__c":0,"Contact_Person__c":"0","Rolex_Dealer__c":"Y","Status__c":1,"Sales_Code__c":"ASSET AND MEMO","RecordTypeId":"01228000000QbiR","Asset_Credit_Limit__c":1000000.0,"Memo_Credit_Limit__c":1500000.0,"Outstanding_A_R_Balance__c":0.0,"Outstanding_Memo_Balance__c":0.0,"BillingStreet":"9 ROSS SIMONS DRIVE - 2920 , , ","BillingPostalCode":"2920"} Delete_Share: execution of BeforeUpdate caused by: System.DmlException: Delete failed. First exception on row 0 with id 00r28000020W8ELAA0; first error: DELETE_FAILED, cannot delete owner or rule share rows, id=00r28000020W8EL: [] Trigger.Delete_Share: line 89, column 1


Code is as below.
trigger Delete_Share on Account (before update) {
    
/*set<Id> AccountIDs = new Set<Id>();
for(Account a: trigger.new){
    
    //System.debug('Account IDs: '+ a.id);
    
  AccountIDs.add(a.id);
}
List<AccountShare> jobShares = new List<AccountShare>();

map<String,AccountShare>accountShareMap = new Map<String,AccountShare>();

for(AccountShare accShare : [select AccountId, UserOrGroupId,AccountAccesslevel,OpportunityAccessLevel from AccountShare where AccountId in :AccountIds])
{
 accountShareMap.put(String.valueOf(accShare.UserOrGroupId),accShare);

}

 
// for(Account a1 : trigger.new){
// Account oldAccount = Trigger.OldMap.get(a1.id);
// if(jobShares.isEmpty() == false ){

 //delete jobShares;
 //}
//}

for(Account a : trigger.new){
Account oldAccount = Trigger.OldMap.get(a.id);
    
    system.debug('old account:' + oldaccount);
    
if ((a.owner1__c != oldAccount.owner1__c) && (a.ownerId != oldAccount.owner1__c)){
    
    System.debug('Account: '+ a);
    
   AccountShare accountRecord = accountShareMap.get(string.valueOf(oldAccount.owner1__c)); 
    if (accountRecord  != null){
  jobShares.add(accountRecord);
    }
  }
 }

if(jobShares.isEmpty() == false ){ 
    //if(!jobShares.isEmpty()){
    delete jobShares;
    System.debug('deleted successfully...');
 
 } */



set<Id> AccountIDs = new Set<Id>();
for(Account a: trigger.new){
AccountIDs.add(a.id);
}
List<AccountShare> jobShares = new List<AccountShare>();

map<String,AccountShare>accountShareMap = new Map<String,AccountShare>();

for(AccountShare accShare : [select AccountId, UserOrGroupId from AccountShare where AccountId in :AccountIds])
{
 accountShareMap.put(String.valueOf(accShare.UserOrGroupId),accShare);

}

 
// for(Account a1 : trigger.new){
// Account oldAccount = Trigger.OldMap.get(a1.id);
// if(jobShares.isEmpty() == false ){

 //delete jobShares;
 //}
//}

for(Account a : trigger.new){
Account oldAccount = Trigger.OldMap.get(a.id);
if (((a.owner1__c != oldAccount.owner1__c) && (a.ownerId != oldAccount.ownerId)) || ((a.owner1__c !=oldAccount.owner1__c)  && (a.OwnerId ==oldAccount.OwnerId))  || ((a.owner1__c == oldAccount.owner1__c)  && (a.OwnerId != oldAccount.OwnerId))){
 
   AccountShare accountRecord = accountShareMap.get(string.valueOf(oldAccount.owner1__c)); 
    if (accountRecord  != null)
    jobShares.add(accountRecord);
  }
 }

if(jobShares.isEmpty() == false ){

 delete jobShares;
 }
 }

Can anybody please tell me why is this happening? Its working fine when i change Owner field manually in the salesforce. But i am using SEA to Salesforce integration where all data gets inserted throgh SEA. Its give me above error while transferring the data.

Thanks & Regards,
Utkarsha

 
GauravGargGauravGarg
Hi Utkarsha,
We cannot delete Owner Sharing record, that is not allowed by salesforce.  Please follow below steps:
  • Create a after update trigger and add a future method call
  • Pass the list of user that need to be deleted from sharing rule. 
  • Perform the delete actiity in future call. 
Hope this helps. 

Thanks,
Gaurav
Skype: gaurav62990
Takes Freelance Jobs
sfdeveloper12sfdeveloper12
Hi Gaurav, Can you elaborate it more. I am using After insert and After update trigger to share record and before update trigger to delete. Dont know how to use future method. Is this ok if i try it on Account Share record? I am doing it for all the users not for specific. Thanks & Regards, Utkarsha Patil
GauravGargGauravGarg
Hi, 
Failture reason:
We are removing sharing record for owner, as this is before update. The record still not update with new Owner. 
Hence it is throwing. 

Solution:
1. Use after insert / update trigger call
2. Create a separate method within the Handler class with annotion @future.
3. Pass the list of user, need to remove sharing. 
4. Perform remove sharing logic in future. 

Hope this helps. 

Thanks,
Gaurav
sfdeveloper12sfdeveloper12
Thank you for the solution Gaurav. Thanks & Regards, Utkarsha Patil
GauravGargGauravGarg
Your welcome Utkarsha, please select the best answer to close this answer.