• Luke Siecinski
  • NEWBIE
  • 10 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
I am an Admin with no real Apex experience and need help to determine what exactly is happening in this Apex Trigger. I belive it is a trigger to share an Account record when some custom Shared With fields are populated. Trying to determine if we can do away with this trigger and utilize something simpler like Account Teams. Thanks!
 
public class ShareWithTriggerHandler {
    public static void afterInsert(Account[] accounts){
    List<AccountShare> sharesToadd = new List<AccountShare>();
    List<AccountShare> sharesToadd2 = new List<AccountShare>();
    List<AccountShare> sharesToadd3 = new List<AccountShare>();
    List<AccountShare> sharesToadd4 = new List<AccountShare>();
  AccountToContactSync acs = new AccountToContactSync();
  acs.UpdateSharedWith(accounts);
        for(Account acclist: [SELECT Id,Name,Shared_With2__c,Shared_With3__c,Shared_With4__c,Shared_With__c FROM Account WHERE Id IN : accounts])
            {
                if(acclist.Shared_With2__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With2__c);
                AccountShare accShare2  = new AccountShare();
                accShare2.AccountId = acclist.id;
                accShare2.UserOrGroupId = acclist.Shared_With2__c;
                accShare2.AccountAccessLevel = 'Edit';
                accShare2.OpportunityAccessLevel = 'Read';
                accShare2.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd2.add(accShare2);
                // Database.insert(accShr,false);
                }
                if(acclist.Shared_With3__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With3__c);
                AccountShare accShare3  = new AccountShare();
                accShare3.AccountId = acclist.id;
                accShare3.UserOrGroupId = acclist.Shared_With3__c;
                accShare3.AccountAccessLevel = 'Edit';
                accShare3.OpportunityAccessLevel = 'Read';
                accShare3.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd3.add(accShare3);
                }
                if(acclist.Shared_With4__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With4__c);
                AccountShare accShare4  = new AccountShare();
                accShare4.AccountId = acclist.id;
                accShare4.UserOrGroupId = acclist.Shared_With4__c;
                accShare4.AccountAccessLevel = 'Edit';
                accShare4.OpportunityAccessLevel = 'Read';
                accShare4.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd4.add(accShare4);
                }
                if(acclist.Shared_With__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With__c);
                AccountShare accShare  = new AccountShare();
                accShare.AccountId = acclist.id;
                accShare.UserOrGroupId = acclist.Shared_With__c;
                accShare.AccountAccessLevel = 'Edit';
                accShare.OpportunityAccessLevel = 'Read';
                accShare.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd.add(accShare);
                }
                system.debug('!!!!'+acclist);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd, false);
            }
            if(!sharesToadd2.isEmpty())
            {
            Database.insert(sharesToadd2, false);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd3, false);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd4, false);
            }

        }
    public static void afterUpdate(Account[] accounts,Map<Id,Account> oldMap)
        {
        List<AccountShare> sharesToDelete = new List<AccountShare>();
    System.debug('******BEGIN UpdateSharedWith******');
    AccountToContactSync acs = new AccountToContactSync();
    acs.UpdateSharedWith(accounts);
    System.debug('******END UpdateSharedWith******');
        for(AccountShare acclist1: [SELECT Id FROM AccountShare WHERE AccountId IN :trigger.oldMap.keyset() AND RowCause = 'Manual'])
            {
            sharesToDelete.add(acclist1);
            // List<AccountShare> sharesToDelete = [SELECT Id FROM AccountShare WHERE AccountId IN :trigger.oldMap.keyset() AND RowCause = 'Manual'];
            }
        if(!sharesToDelete.isEmpty())
            {
            Database.Delete(sharesToDelete, false);
            }
        List<AccountShare> sharesToadd = new List<AccountShare>();
        List<AccountShare> sharesToadd2 = new List<AccountShare>();
        List<AccountShare> sharesToadd3 = new List<AccountShare>();
        List<AccountShare> sharesToadd4 = new List<AccountShare>();
        for(Account acclist: [SELECT Id,Name,Shared_With2__c,Shared_With3__c,Shared_With4__c,Shared_With__c FROM Account WHERE Id IN : accounts])
            {
            if(acclist.Shared_With2__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With2__c);
                AccountShare accShare2  = new AccountShare();
                accShare2.AccountId = acclist.id;
                accShare2.UserOrGroupId = acclist.Shared_With2__c;
                accShare2.AccountAccessLevel = 'Edit';
                accShare2.OpportunityAccessLevel = 'Read';
                accShare2.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd2.add(accShare2);
                // Database.insert(accShr,false);
                }
                if(acclist.Shared_With3__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With3__c);
                AccountShare accShare3  = new AccountShare();
                accShare3.AccountId = acclist.id;
                accShare3.UserOrGroupId = acclist.Shared_With3__c;
                accShare3.AccountAccessLevel = 'Edit';
                accShare3.OpportunityAccessLevel = 'Read';
                accShare3.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd3.add(accShare3);
                }
                if(acclist.Shared_With4__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With4__c);
                AccountShare accShare4  = new AccountShare();
                accShare4.AccountId = acclist.id;
                accShare4.UserOrGroupId = acclist.Shared_With4__c;
                accShare4.AccountAccessLevel = 'Edit';
                accShare4.OpportunityAccessLevel = 'Read';
                accShare4.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd4.add(accShare4);
                }
                if(acclist.Shared_With__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With__c);
                AccountShare accShare  = new AccountShare();
                accShare.AccountId = acclist.id;
                accShare.UserOrGroupId = acclist.Shared_With__c;
                accShare.AccountAccessLevel = 'Edit';
                accShare.OpportunityAccessLevel = 'Read';
                accShare.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd.add(accShare);
                }
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd, false);
            }
            if(!sharesToadd2.isEmpty())
            {
            Database.insert(sharesToadd2, false);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd3, false);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd4, false);
            }

        }
    
  /*  public static void manualShareEdit(Id recordId, Id userOrGroupId)
        {
        AccountShare accShr  = new AccountShare();
        accShr.AccountId = recordId;
        accShr.UserOrGroupId = userOrGroupId;
        accShr.AccountAccessLevel = 'Edit';
        accShr.OpportunityAccessLevel = 'Edit';
        accShr.RowCause = Schema.AccountShare.RowCause.Manual;
        Database.insert(accShr,false);
        }    */
    }

 
I am an Admin with no real Apex experience and need help to determine what exactly is happening in this Apex Trigger. I belive it is a trigger to share an Account record when some custom Shared With fields are populated. Trying to determine if we can do away with this trigger and utilize something simpler like Account Teams. Thanks!
 
public class ShareWithTriggerHandler {
    public static void afterInsert(Account[] accounts){
    List<AccountShare> sharesToadd = new List<AccountShare>();
    List<AccountShare> sharesToadd2 = new List<AccountShare>();
    List<AccountShare> sharesToadd3 = new List<AccountShare>();
    List<AccountShare> sharesToadd4 = new List<AccountShare>();
  AccountToContactSync acs = new AccountToContactSync();
  acs.UpdateSharedWith(accounts);
        for(Account acclist: [SELECT Id,Name,Shared_With2__c,Shared_With3__c,Shared_With4__c,Shared_With__c FROM Account WHERE Id IN : accounts])
            {
                if(acclist.Shared_With2__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With2__c);
                AccountShare accShare2  = new AccountShare();
                accShare2.AccountId = acclist.id;
                accShare2.UserOrGroupId = acclist.Shared_With2__c;
                accShare2.AccountAccessLevel = 'Edit';
                accShare2.OpportunityAccessLevel = 'Read';
                accShare2.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd2.add(accShare2);
                // Database.insert(accShr,false);
                }
                if(acclist.Shared_With3__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With3__c);
                AccountShare accShare3  = new AccountShare();
                accShare3.AccountId = acclist.id;
                accShare3.UserOrGroupId = acclist.Shared_With3__c;
                accShare3.AccountAccessLevel = 'Edit';
                accShare3.OpportunityAccessLevel = 'Read';
                accShare3.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd3.add(accShare3);
                }
                if(acclist.Shared_With4__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With4__c);
                AccountShare accShare4  = new AccountShare();
                accShare4.AccountId = acclist.id;
                accShare4.UserOrGroupId = acclist.Shared_With4__c;
                accShare4.AccountAccessLevel = 'Edit';
                accShare4.OpportunityAccessLevel = 'Read';
                accShare4.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd4.add(accShare4);
                }
                if(acclist.Shared_With__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With__c);
                AccountShare accShare  = new AccountShare();
                accShare.AccountId = acclist.id;
                accShare.UserOrGroupId = acclist.Shared_With__c;
                accShare.AccountAccessLevel = 'Edit';
                accShare.OpportunityAccessLevel = 'Read';
                accShare.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd.add(accShare);
                }
                system.debug('!!!!'+acclist);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd, false);
            }
            if(!sharesToadd2.isEmpty())
            {
            Database.insert(sharesToadd2, false);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd3, false);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd4, false);
            }

        }
    public static void afterUpdate(Account[] accounts,Map<Id,Account> oldMap)
        {
        List<AccountShare> sharesToDelete = new List<AccountShare>();
    System.debug('******BEGIN UpdateSharedWith******');
    AccountToContactSync acs = new AccountToContactSync();
    acs.UpdateSharedWith(accounts);
    System.debug('******END UpdateSharedWith******');
        for(AccountShare acclist1: [SELECT Id FROM AccountShare WHERE AccountId IN :trigger.oldMap.keyset() AND RowCause = 'Manual'])
            {
            sharesToDelete.add(acclist1);
            // List<AccountShare> sharesToDelete = [SELECT Id FROM AccountShare WHERE AccountId IN :trigger.oldMap.keyset() AND RowCause = 'Manual'];
            }
        if(!sharesToDelete.isEmpty())
            {
            Database.Delete(sharesToDelete, false);
            }
        List<AccountShare> sharesToadd = new List<AccountShare>();
        List<AccountShare> sharesToadd2 = new List<AccountShare>();
        List<AccountShare> sharesToadd3 = new List<AccountShare>();
        List<AccountShare> sharesToadd4 = new List<AccountShare>();
        for(Account acclist: [SELECT Id,Name,Shared_With2__c,Shared_With3__c,Shared_With4__c,Shared_With__c FROM Account WHERE Id IN : accounts])
            {
            if(acclist.Shared_With2__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With2__c);
                AccountShare accShare2  = new AccountShare();
                accShare2.AccountId = acclist.id;
                accShare2.UserOrGroupId = acclist.Shared_With2__c;
                accShare2.AccountAccessLevel = 'Edit';
                accShare2.OpportunityAccessLevel = 'Read';
                accShare2.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd2.add(accShare2);
                // Database.insert(accShr,false);
                }
                if(acclist.Shared_With3__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With3__c);
                AccountShare accShare3  = new AccountShare();
                accShare3.AccountId = acclist.id;
                accShare3.UserOrGroupId = acclist.Shared_With3__c;
                accShare3.AccountAccessLevel = 'Edit';
                accShare3.OpportunityAccessLevel = 'Read';
                accShare3.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd3.add(accShare3);
                }
                if(acclist.Shared_With4__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With4__c);
                AccountShare accShare4  = new AccountShare();
                accShare4.AccountId = acclist.id;
                accShare4.UserOrGroupId = acclist.Shared_With4__c;
                accShare4.AccountAccessLevel = 'Edit';
                accShare4.OpportunityAccessLevel = 'Read';
                accShare4.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd4.add(accShare4);
                }
                if(acclist.Shared_With__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With__c);
                AccountShare accShare  = new AccountShare();
                accShare.AccountId = acclist.id;
                accShare.UserOrGroupId = acclist.Shared_With__c;
                accShare.AccountAccessLevel = 'Edit';
                accShare.OpportunityAccessLevel = 'Read';
                accShare.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd.add(accShare);
                }
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd, false);
            }
            if(!sharesToadd2.isEmpty())
            {
            Database.insert(sharesToadd2, false);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd3, false);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd4, false);
            }

        }
    
  /*  public static void manualShareEdit(Id recordId, Id userOrGroupId)
        {
        AccountShare accShr  = new AccountShare();
        accShr.AccountId = recordId;
        accShr.UserOrGroupId = userOrGroupId;
        accShr.AccountAccessLevel = 'Edit';
        accShr.OpportunityAccessLevel = 'Edit';
        accShr.RowCause = Schema.AccountShare.RowCause.Manual;
        Database.insert(accShr,false);
        }    */
    }