I have a requirement to provide a particular group of records to one user.But record sharing criteria depends on formula field.

So, I'm not able to share through sharing rules. So, I have created an apex to share opportunity access to the users.

But I'm facing this error.

TooManyLockFailure: Too many lock failure 200 Trying again later.

Apex Class

public class OpportunitySharing 
    Public static Boolean Sharingcheck=true;
    public static ID userid=[Select id from user where name='Chloe Rodriguez'].ID;
    public static void BangaloreTeamShareUpdateordelete(List<Opportunity> newopps,Map<Id,Opportunity> oldmapopps)
        List<Id> Sharedeletionlist = new List<Id>();
        List<OpportunityShare> Shareinsertlist = new List<OpportunityShare>();
        for(Opportunity oppid:newopps)
            if(oldmapopps.get(oppid.id).team__c == 'Bangalore Team' && 
               (oppid.Team__c <> 'Bangalore Team' || String.isBlank(oppid.Team__c)) )
            else if((oldmapopps.get(oppid.Id).team__c <> 'Bangalore Team' || String.isBlank(oldmapopps.get(oppid.id).team__c)) && 
                    oppid.Team__c == 'Bangalore Team')
                Opportunityshare oppshareid= new  Opportunityshare();
            if(Sharedeletionlist <> null && !Sharedeletionlist.isEmpty())
                delete[Select id from Opportunityshare where opportunityid IN :Sharedeletionlist AND RowCause='Manual' FOR UPDATE];
            If(Shareinsertlist <> null && !Shareinsertlist.isEmpty())
                insert Shareinsertlist;

Please guide where I missed it.

  • Why are you using an explicit FOR UPDATE on a delete DML here? – David Reed Feb 7 at 21:21
  • I thought records are locking because of that query.Already i have removed from my code – Pavan tej Feb 8 at 16:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.