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
Harshada Kadu 9Harshada Kadu 9 

Trigger to delete accountshare records

I have 2 triggers written named Account_Share and Delete_Share.
I am pushing data from external site into salesforce.
So,in Salesforce in Account object,one standard field named ownerID(Lookup User )  and one custom field named owner1__c (Lookup User ) should get updated whenever changes are done in the site.
But this is not happening.Even though changes are made in the site,same is not getting updated in salesforce, rather it  throws an error as follows:

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":"00528000005HQRPAA4","Terms_Id__c":"a042800000oV27IAAS","Default_Currency__c":"US DOLLARS","Industry":"Jewelry","Website":"<><>","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

Account_Share Trigger:
trigger Account_Share on Account (after insert,after update) {

 List<AccountShare> jobShares = new List<AccountShare>();
 for(Account a :{

if (a.owner1__c != null) {

   AccountShare accountRecord = new AccountShare();
   accountRecord.AccountId= a.Id;
   System.Debug('************* Salesforce Account ID ******* '+a.Id);
   accountRecord.UserOrGroupId = a.owner1__c;
   System.Debug('************* Salesforce Custom Owner: Account Executive ******* '+ a.owner1__c);
   accountRecord.AccountAccessLevel= 'edit';
   System.debug('Account Acess: '+  accountRecord.AccountAccessLevel);
   System.debug('Oppurtunity Acess: '+  accountRecord.OpportunityAccessLevel);
   System.debug('Acc_share_trigger Account Record: '+ accountRecord);

 /** Insert all of the newly created Share records and capture save result **/
 Database.SaveResult[] jobShareInsertResult = Database.insert(jobShares,false);
 System.debug('Insert all of the newly created Share records: '+ jobShareInsertResult);

Delete Share Trigger 
trigger Delete_Share on Account (before update) {

set<Id> AccountIDs = new Set<Id>();
for(Account a:{
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])


for(Account a :{
Account oldAccount = Trigger.OldMap.get(;
if ((a.owner1__c != oldAccount.owner1__c) && (a.ownerId != oldAccount.owner1__c)){
   AccountShare accountRecord = accountShareMap.get(string.valueOf(oldAccount.owner1__c)); 
    if (accountRecord  != null)

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

 delete jobShares;

Srinivasa Chary TaduriSrinivasa Chary Taduri
Add the highlighted one in the query.

select AccountId, UserOrGroupId from AccountShare where AccountId in :AccountIds and AccountAccessLevel != 'All'
hs dsd 19hs dsd 19
Gold is the epitome of splendor and sheen. Not only is gold beautiful, it's also practical and utilized in a number of methods! It does not tarnish or rust without difficulty to buy (, which has made it useful for lots applications inclusive of getting used inside electric circuitry.