• Mrkenny
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies

Hi all, 

 

I wrote this apex trigger but it looks like it needs to be bulkified. I keep getting an error when I try to deploy and I was told that I might need to combine some of my queries while using maps. I am not yet that fluent in apex yet so please if you have any suggestions or you kow what I should do can you please look at my code and tell me where I can do that and also actualy show me what you did so that I can use that as reference. This is a trigger to grant a partner sharing access to an end user acount. Thank you.

 

 

 

trigger PartnerSharing onAccount (afterinsert, beforeupdate){

   

    set<id> account_id = new set<id>();

    set<id> account_id1 = new set<id>();

    set<id> account_to = new set<id>();

    set<id> account_to1 = new set<id>();

    set<id> account_from;

    set<id> account_from1;

    map<id,set<id>> ShareFromTo = new map<id,set<id>>();

    map<id,set<id>> ShareFromTo1 = new map<id,set<id>>();

    map<id, AccountShare> matm = new map<id, AccountShare>();

    map<id, AccountShare> matm1 = new map<id, AccountShare>();

    map<id, map<id, AccountShare>> mmatm = new map<id, map<id, AccountShare>>();

    map<id, map<id, AccountShare>> mmatm1 = new map<id, map<id, AccountShare>>();

    map<id, AccountShare> current_matm = new map<id, AccountShare>();

    map<id, AccountShare> current_matm1 = new map<id, AccountShare>();

    map<id, map<id, AccountShare>> current_mmatm = new map<id, map<id, AccountShare>>();

    map<id, map<id, AccountShare>> current_mmatm1 = new map<id, map<id, AccountShare>>();

    map<id, Group> UserRoleMapByRoleID = new map<id, Group>();

    map<id, Group> UserRoleMapByRoleID1 = new map<id, Group>();

    map<id, map<id, Group>> UserRoleMapByRoleIDbyAccountId = new map<id, map<id, Group>>();  

    map<id, map<id, Group>> UserRoleMapByRoleIDbyAccountId1 = new map<id, map<id, Group>>();  

    integer i=0; 

    UserRole urole = new UserRole();

    UserRole urole1 = new UserRole();

    set<id> UserRoleIDs = new set<id>();

    set<id> UserRoleIDs1 = new set<id>();

    // // Create a new list of sharing objects for AccountShare

    list<AccountShare> listnewAccountShare = new list<AccountShare>();

    list<AccountShare> listnewAccountShare2 = new list<AccountShare>();

    AccountShare newAccountShare;

    String roletoggle;

    list<Group> urolelist = new list<Group>();

    

 

    for (Account acc: trigger.new){

    //check trigger old map if is update trigger so this only runs if the field has been changed

            

     /*

     you can remove after insert

     because you cant create the pratner record until after the record is saved

     instead create a validation rule that you can't check the auth support button on a new record

     then do 

     in your for(trigger.new)

     */

    

    if(acc.Authorized_Support_Partner__c != trigger.oldmap.get(acc.id).Authorized_Support_Partner__c){

    if (acc.Authorized_Support_Partner__c == true)

        {

            account_Id.add(acc.id);

    }

    else

    {

        account_id1.add(acc.id);

    }  

    }

   

        }

    //System.debug(Limits.getLimitScriptStatements());   

    if(account_Id.size()>0)

    {

   list<Partner> p1 = new list<Partner>();

   list<Partner> p1_clone = new list<Partner>();

   list<Partner> plist = [Select p.Role, p.AccountToId, p.AccountFromId From Partner p where p.AccountFromId in: account_id];

   for(Partner pc1:plist)

   {

   Partner p1_new = new Partner();

   p1_new.AccountToId = pc1.AccountToId;

   p1_new.AccountFromId = pc1.AccountFromId;

  

   p1_new.Role = 'Channel Partner - Auth Support';

   roletoggle = 'Channel Partner - Auth Support';

   p1_clone.add(p1_new);

   //Apex Class with parameter as list of accountid,role variable ex:partenrsharingclass.clonerecord(account_id,togglingrole);

   

   }

   insert p1_clone; //New ID of Partner Record FOR FURTHER AccountShare you have to Query again  in Partner

   delete plist;

    }

    if(account_Id1.size()>0)

    {

   list<Partner> p1 = new list<Partner>();

   list<Partner> p1_clone1 = new list<Partner>();

   list<Partner> plist1 = [Select p.Role, p.AccountToId, p.AccountFromId From Partner p where p.AccountFromId in: account_id1];

   for(Partner pc11:plist1)

   {

   Partner p1_new1 = new Partner();

   p1_new1.AccountToId = pc11.AccountToId;

   p1_new1.AccountFromId = pc11.AccountFromId;

  

   p1_new1.Role = 'Channel Partner';

   roletoggle = 'Channel Partner';

   p1_clone1.add(p1_new1);

   //Apex Class with parameter as list of accountid,role variable ex:partenrsharingclass.clonerecord(account_id,togglingrole);

   

   }

   insert p1_clone1; 

   delete plist1;

    }

   list<Partner> p = [Select p.Role, p.AccountToId, p.AccountFromId From Partner p where p.AccountFromId in: account_id and role= :roletoggle];

   for(Partner pc: p)

    {

      if(!account_to.contains(pc.accountToId))

        {

            account_to.add(pc.accountToId);

        }

        account_from = new set<id>();

        if(!ShareFromTo.containsKey(pc.accountToId))

        {

            account_from.add(pc.accountToId);

            ShareFromTo.put(pc.accountFromId, account_from);

        }

        else

        {

            

            for(id sfi:ShareFromTo.get(pc.accountToId)){

                if(pc.accountFromId!=sfi){

                    account_from = ShareFromTo.get(pc.accountFromId);

                    account_from.add(pc.accountToId);

                    ShareFromTo.put(pc.accountFromId, account_from);

                }

            }

        }

    }

  

  

    list<Partner> p1 = [Select p.Role, p.AccountToId, p.AccountFromId From Partner p where p.AccountFromId in: account_id1 and role = :roletoggle];

    for(Partner pc1: p1)

    {

      if(!account_to1.contains(pc1.accountToId))

        {

            account_to1.add(pc1.accountToId);

        }

        account_from1 = new set<id>();

        if(!ShareFromTo1.containsKey(pc1.accountToId))

        {

            account_from1.add(pc1.accountToId);

            ShareFromTo1.put(pc1.accountFromId, account_from1);

        }

        else

        {

            

            for(id sfi:ShareFromTo1.get(pc1.accountToId)){

                if(pc1.accountFromId!=sfi){

                    account_from1 = ShareFromTo1.get(pc1.accountFromId);

                    account_from1.add(pc1.accountToId);

                    ShareFromTo1.put(pc1.accountFromId, account_from);

                    //system.debug('ShareFromTo==========> ');

                }

            }

        }

    }

    

    

     list<UserRole> ur1 = [Select u.PortalType, u.PortalRole, u.PortalAccountOwnerId, 

                            u.PortalAccountId, u.Name, u.Id, u.ContactAccessForAccountOwner, 

                            u.CaseAccessForAccountOwner 

                            From UserRole

                            where u.PortalAccountId in :account_to1];

                            

                  

    for(UserRole eur1:ur1)

    {

        UserRoleIds1.add(eur1.id);

    }

         

    list<Group> UserGroups1 = new list<Group>();

    

    UserGroups1 = [Select g.Type, g.RelatedId, g.Id, g.DeveloperName From Group g where g.relatedID in :UserRoleIds1  and g.type = 'Role'];

    map<id, Group> UserGroupsMap1 = new map<id, Group>();

    for (Group gm1: UserGroups1)

    {

        if(!UserGroupsMap1.containsKey(gm1.RelatedId))

        {

       UserGroupsMap1.put(gm1.RelatedId, gm1);

        }

    }

      

   

    for(UserRole uri1:ur1)

    {

        if(!UserRoleMapByRoleIDbyAccountId1.containsKey(uri1.PortalAccountId))

        {

            if(UserGroupsMap1.containsKey(uri1.id)){

                UserRoleMapByRoleID1.put(uri1.id, UserGroupsMap1.get(uri1.id));

                UserRoleMapByRoleIDbyAccountId1.put(uri1.PortalAccountId, UserRoleMapByRoleID1);

            }else{

                //system.debug('Group does Not Exist====================else');

            }

        }

        else

        {

            UserRoleMapByRoleID1 = UserRoleMapByRoleIDbyAccountId1.get(uri1.PortalAccountId);

            if(!UserRoleMapByRoleID1.containsKey(uri1.id))

            {

                if(UserGroupsMap1.containsKey(uri1.id))

                {

                    UserRoleMapByRoleID1.put(uri1.id, UserGroupsMap1.get(uri1.id));

                    UserRoleMapByRoleIDbyAccountId1.put(uri1.PortalAccountId, UserRoleMapByRoleID1);

                }else

                {

                //system.debug('Group does Not Exist');

                }

            }

            else

            {

                //system.debug('User role id already in Map ' + uri1.id);

            }

        }

    }

    

     //list<AccountShare> ls1 = [Select a.UserOrGroupId, a.RowCause, a.OpportunityAccessLevel, a.Id, a.CaseAccessLevel, a.AccountId, a.AccountAccessLevel From AccountShare a where a.AccountId in :account_Id1 limit 50000];

    //system.debug(ls1);

    

    for(AccountShare atm1: [Select a.UserOrGroupId, a.RowCause, a.OpportunityAccessLevel, a.Id, a.CaseAccessLevel, a.AccountId, a.AccountAccessLevel From AccountShare a where a.AccountId in :account_Id1])

    

    {

        if(mmatm1.containsKey(atm1.AccountId))

        {

            current_matm1 = mmatm1.get(atm1.AccountId);

            if(!current_matm1.containsKey(atm1.UserOrGroupId))

            {

                current_matm1.put(atm1.UserOrGroupId, atm1);

                mmatm1.put(atm1.AccountID,current_matm1);

            }

        }

        else

        {

           current_matm1.put(atm1.UserOrGroupId, atm1);

             mmatm1.put(atm1.AccountID,current_matm1);

        }

    }

    

    

    

    

    list<UserRole> ur = [Select u.PortalType, u.PortalRole, u.PortalAccountOwnerId, 

                            u.PortalAccountId, u.Name, u.Id, u.ContactAccessForAccountOwner, 

                            u.CaseAccessForAccountOwner 

                            From UserRole

                            where u.PortalAccountId in :account_to limit 50000];

    for(UserRole eur:ur)

    {

        UserRoleIds.add(eur.id);

    }

    list<Group> UserGroups = new list<Group>();

    

    UserGroups = [Select g.Type, g.RelatedId, g.Id, g.DeveloperName From Group g where g.relatedID in :UserRoleIds  and g.type = 'Role'];

    map<id, Group> UserGroupsMap = new map<id, Group>();

    for (Group gm: UserGroups)

    {

        if(!UserGroupsMap.containsKey(gm.RelatedId))

        {

            UserGroupsMap.put(gm.RelatedId, gm);

        }

    }

    System.debug(Limits.getLimitScriptStatements());

    for(UserRole uri:ur)

    {

        if(!UserRoleMapByRoleIDbyAccountId.containsKey(uri.PortalAccountId))

        {

            if(UserGroupsMap.containsKey(uri.id)){

                UserRoleMapByRoleID.put(uri.id, UserGroupsMap.get(uri.id));

                UserRoleMapByRoleIDbyAccountId.put(uri.PortalAccountId, UserRoleMapByRoleID);

            }else{

                //system.debug('Group does Not Exist');

            }

        }

        else

        {

            UserRoleMapByRoleID = UserRoleMapByRoleIDbyAccountId.get(uri.PortalAccountId);

            if(!UserRoleMapByRoleID.containsKey(uri.id))

            {

                if(UserGroupsMap.containsKey(uri.id))

                {

                    UserRoleMapByRoleID.put(uri.id, UserGroupsMap.get(uri.id));

                    UserRoleMapByRoleIDbyAccountId.put(uri.PortalAccountId, UserRoleMapByRoleID);

                }else

                {

                //system.debug('Group does Not Exist');

                }

            }

            else

            {

                //system.debug('User role id already in Map: ' + uri.id);

            }

        }

    }

    

    //list<AccountShare> ls = [Select a.UserOrGroupId, a.RowCause, a.OpportunityAccessLevel, a.Id, a.CaseAccessLevel, a.AccountId, a.AccountAccessLevel From AccountShare a where a.AccountId in :account_Id limit 50000];

    //system.debug(ls);

    

    for(AccountShare atm: [Select a.UserOrGroupId, a.RowCause, a.OpportunityAccessLevel, a.Id, a.CaseAccessLevel, a.AccountId, a.AccountAccessLevel From AccountShare a where a.AccountId in :account_Id])

    {

        if(mmatm.containsKey(atm.AccountId))

        {

            current_matm = mmatm.get(atm.AccountId);

            if(!current_matm.containsKey(atm.UserOrGroupId))

            {

                current_matm.put(atm.UserOrGroupId, atm);

                mmatm.put(atm.AccountID,current_matm);

            }

        }

        else

        {

  current_matm.put(atm.UserOrGroupId, atm);

             mmatm.put(atm.AccountID,current_matm);

        }

    }

    

       

    

}

I am having issues deploying i keep getting System.LimitException: Too many query rows: 50001. Can someone help me outn with this it is urgent. Here is my trigger below

 

trigger PartnerSharing on Account (after insert, before update){
    
    set<id> account_id = new set<id>();
    set<id> account_id1 = new set<id>();
    set<id> account_to = new set<id>();
    set<id> account_to1 = new set<id>();
    set<id> account_from;
    set<id> account_from1;
    map<id,set<id>> ShareFromTo = new map<id,set<id>>();
    map<id,set<id>> ShareFromTo1 = new map<id,set<id>>();
    map<id, AccountShare> matm = new map<id, AccountShare>();
    map<id, AccountShare> matm1 = new map<id, AccountShare>();
    map<id, map<id, AccountShare>> mmatm = new map<id, map<id, AccountShare>>();
    map<id, map<id, AccountShare>> mmatm1 = new map<id, map<id, AccountShare>>();
    map<id, AccountShare> current_matm = new map<id, AccountShare>();
    map<id, AccountShare> current_matm1 = new map<id, AccountShare>();
    map<id, map<id, AccountShare>> current_mmatm = new map<id, map<id, AccountShare>>();
    map<id, map<id, AccountShare>> current_mmatm1 = new map<id, map<id, AccountShare>>();
    map<id, Group> UserRoleMapByRoleID = new map<id, Group>();
    map<id, Group> UserRoleMapByRoleID1 = new map<id, Group>();
    map<id, map<id, Group>> UserRoleMapByRoleIDbyAccountId = new map<id, map<id, Group>>();  
    map<id, map<id, Group>> UserRoleMapByRoleIDbyAccountId1 = new map<id, map<id, Group>>();  
    integer i=0;
    UserRole urole = new UserRole();
    UserRole urole1 = new UserRole();
    set<id> UserRoleIDs = new set<id>();
    set<id> UserRoleIDs1 = new set<id>();
    // // Create a new list of sharing objects for AccountShare
    list<AccountShare> listnewAccountShare = new list<AccountShare>();
    list<AccountShare> listnewAccountShare2 = new list<AccountShare>();
    AccountShare newAccountShare;
    String roletoggle;
    list<Group> urolelist = new list<Group>();
        for (Account acc: trigger.new){
            //check trigger old map if is update trigger so this only runs if the field has been changed
                if (acc.Authorized_Support_Partner__c == true)
                {
                account_Id.add(acc.id);
            }
            else
            {
                  account_id1.add(acc.id);
            }        
        }
       
    if(account_Id.size()>0)
    {
   list<Partner> p1 = new list<Partner>();
   list<Partner> p1_clone = new list<Partner>();
   list<Partner> plist = [Select p.Role, p.AccountToId, p.AccountFromId From Partner p where p.AccountFromId in: account_id];
   for(Partner pc1:plist)
   {
       Partner p1_new = new Partner();
       p1_new.AccountToId = pc1.AccountToId;
       p1_new.AccountFromId = pc1.AccountFromId;
   /*if(pc1.Role=='Channel Partner')
       {
       p1_new.Role='Channel Partner - Auth Support';
       roletoggle = 'Channel Partner - Auth Support';
       }
       else
       {
       p1_new.Role='Channel Partner';
       roletoggle='Channel Partner';
       }
       */
       p1_new.Role = 'Channel Partner - Auth Support';
       roletoggle = 'Channel Partner - Auth Support';
       p1_clone.add(p1_new);
       //Apex Class with parameter as list of accountid,role variable ex:partenrsharingclass.clonerecord(account_id,togglingrole);
       
   }
   insert p1_clone; //New ID of Partner Record FOR FURTHER AccountShare you have to Query again  in Partner
   delete plist;
    }
    if(account_Id1.size()>0)
    {
   list<Partner> p1 = new list<Partner>();
   list<Partner> p1_clone1 = new list<Partner>();
   list<Partner> plist1 = [Select p.Role, p.AccountToId, p.AccountFromId From Partner p where p.AccountFromId in: account_id1];
   for(Partner pc11:plist1)
   {
       Partner p1_new1 = new Partner();
       p1_new1.AccountToId = pc11.AccountToId;
       p1_new1.AccountFromId = pc11.AccountFromId;
   /*if(pc1.Role=='Channel Partner')
       {
       p1_new.Role='Channel Partner - Auth Support';
       roletoggle = 'Channel Partner - Auth Support';
       }
       else{
       p1_new.Role='Channel Partner';
       roletoggle='Channel Partner';
       }
       */
       p1_new1.Role = 'Channel Partner';
       roletoggle = 'Channel Partner';
       p1_clone1.add(p1_new1);
       //Apex Class with parameter as list of accountid,role variable ex:partenrsharingclass.clonerecord(account_id,togglingrole);
       
   }
   insert p1_clone1; //New ID of Partner Record FOR FURTHER AccountShare you have to Query again  in Partner
   delete plist1;
    }
   list<Partner> p = [Select p.Role, p.AccountToId, p.AccountFromId From Partner p where p.AccountFromId in: account_id and role= :roletoggle];
   for(Partner pc: p)
    {
      if(!account_to.contains(pc.accountToId))
        {
            account_to.add(pc.accountToId);
        }
        account_from = new set<id>();
        if(!ShareFromTo.containsKey(pc.accountToId))
        {
            account_from.add(pc.accountToId);
            ShareFromTo.put(pc.accountFromId, account_from);
        }
        else
        {
            
            for(id sfi:ShareFromTo.get(pc.accountToId)){
                if(pc.accountFromId!=sfi){
                    account_from = ShareFromTo.get(pc.accountFromId);
                    account_from.add(pc.accountToId);
                    ShareFromTo.put(pc.accountFromId, account_from);
                }
            }
        }
    }
 
 
    list<Partner> p1 = [Select p.Role, p.AccountToId, p.AccountFromId From Partner p where p.AccountFromId in: account_id1 and role = :roletoggle];
    for(Partner pc1: p1)
    {
      if(!account_to1.contains(pc1.accountToId))
        {
            account_to1.add(pc1.accountToId);
        }
        account_from1 = new set<id>();
        if(!ShareFromTo1.containsKey(pc1.accountToId))
        {
            account_from1.add(pc1.accountToId);
            ShareFromTo1.put(pc1.accountFromId, account_from1);
        }
        else
        {
            
            for(id sfi:ShareFromTo1.get(pc1.accountToId)){
                if(pc1.accountFromId!=sfi){
                    account_from1 = ShareFromTo1.get(pc1.accountFromId);
                    account_from1.add(pc1.accountToId);
                    
                    ShareFromTo1.put(pc1.accountFromId, account_from);
                    system.debug('ShareFromTo==========> ');
                }
            }
        }
    }
    
    
     list<UserRole> ur1 = [Select u.PortalType, u.PortalRole, u.PortalAccountOwnerId,
                            u.PortalAccountId, u.Name, u.Id, u.ContactAccessForAccountOwner,
                            u.CaseAccessForAccountOwner
                            From UserRole u
                            where u.PortalAccountId in :account_to1];
                            
                  
    for(UserRole eur1:ur1)
    {
        UserRoleIds1.add(eur1.id);
    }
         
    list<Group> UserGroups1 = new list<Group>();
    
    UserGroups1 = [Select g.Type, g.RelatedId, g.Id, g.DeveloperName From Group g where g.relatedID in :UserRoleIds1  and g.type = 'Role'];
    map<id, Group> UserGroupsMap1 = new map<id, Group>();
    for (Group gm1: UserGroups1)
    {
        if(!UserGroupsMap1.containsKey(gm1.RelatedId))
        {
               UserGroupsMap1.put(gm1.RelatedId, gm1);
        }
    }
      
    
    for(UserRole uri1:ur1)
    {
        if(!UserRoleMapByRoleIDbyAccountId1.containsKey(uri1.PortalAccountId))
        {
            if(UserGroupsMap1.containsKey(uri1.id)){
                UserRoleMapByRoleID1.put(uri1.id, UserGroupsMap1.get(uri1.id));
                UserRoleMapByRoleIDbyAccountId1.put(uri1.PortalAccountId, UserRoleMapByRoleID1);
            }else{
                system.debug('Group does Not Exist====================else');
            }
        }
        else
        {
            UserRoleMapByRoleID1 = UserRoleMapByRoleIDbyAccountId1.get(uri1.PortalAccountId);
            if(!UserRoleMapByRoleID1.containsKey(uri1.id))
            {
                if(UserGroupsMap1.containsKey(uri1.id))
                {
                    UserRoleMapByRoleID1.put(uri1.id, UserGroupsMap1.get(uri1.id));
                    UserRoleMapByRoleIDbyAccountId1.put(uri1.PortalAccountId, UserRoleMapByRoleID1);
                }else
                {
                    system.debug('Group does Not Exist');
                }
            }
            else
            {
                system.debug('User role id already in Map ' + uri1.id);
            }
        }
    }
    
     list<AccountShare> ls1 = [Select a.UserOrGroupId, a.RowCause, a.OpportunityAccessLevel, a.Id, a.CaseAccessLevel, a.AccountId, a.AccountAccessLevel From AccountShare a where a.AccountId in :account_Id1 limit 50000];
    //system.debug(ls1);
    
    for(AccountShare atm1: ls1)
    {
        if(mmatm1.containsKey(atm1.AccountId))
        {
            current_matm1 = mmatm1.get(atm1.AccountId);
            if(!current_matm1.containsKey(atm1.UserOrGroupId))
            {
                current_matm1.put(atm1.UserOrGroupId, atm1);
                mmatm1.put(atm1.AccountID,current_matm1);
            }
        }
        else
        {
               current_matm1.put(atm1.UserOrGroupId, atm1);
             mmatm1.put(atm1.AccountID,current_matm1);
        }
    }
    
    
    
    
    list<UserRole> ur = [Select u.PortalType, u.PortalRole, u.PortalAccountOwnerId,
                            u.PortalAccountId, u.Name, u.Id, u.ContactAccessForAccountOwner,
                            u.CaseAccessForAccountOwner
                            From UserRole u
                            where u.PortalAccountId in :account_to limit 50000];
    for(UserRole eur:ur)
    {
        UserRoleIds.add(eur.id);
    }
    list<Group> UserGroups = new list<Group>();
    
    UserGroups = [Select g.Type, g.RelatedId, g.Id, g.DeveloperName From Group g where g.relatedID in :UserRoleIds  and g.type = 'Role'];
    map<id, Group> UserGroupsMap = new map<id, Group>();
    for (Group gm: UserGroups)
    {
        if(!UserGroupsMap.containsKey(gm.RelatedId))
        {
            UserGroupsMap.put(gm.RelatedId, gm);
        }
    }
    
    for(UserRole uri:ur)
    {
        if(!UserRoleMapByRoleIDbyAccountId.containsKey(uri.PortalAccountId))
        {
            if(UserGroupsMap.containsKey(uri.id)){
                UserRoleMapByRoleID.put(uri.id, UserGroupsMap.get(uri.id));
                UserRoleMapByRoleIDbyAccountId.put(uri.PortalAccountId, UserRoleMapByRoleID);
            }else{
                system.debug('Group does Not Exist');
            }
        }
        else
        {
            UserRoleMapByRoleID = UserRoleMapByRoleIDbyAccountId.get(uri.PortalAccountId);
            if(!UserRoleMapByRoleID.containsKey(uri.id))
            {
                if(UserGroupsMap.containsKey(uri.id))
                {
                    UserRoleMapByRoleID.put(uri.id, UserGroupsMap.get(uri.id));
                    UserRoleMapByRoleIDbyAccountId.put(uri.PortalAccountId, UserRoleMapByRoleID);
                }else
                {
                system.debug('Group does Not Exist');
                }
            }
            else
            {
                system.debug('User role id already in Map: ' + uri.id);
            }
        }
    }
    
    list<AccountShare> ls = [Select a.UserOrGroupId, a.RowCause, a.OpportunityAccessLevel, a.Id, a.CaseAccessLevel, a.AccountId, a.AccountAccessLevel From AccountShare a where a.AccountId in :account_Id limit 50000];
    //system.debug(ls);
    
    for(AccountShare atm: ls)
    {
        if(mmatm.containsKey(atm.AccountId))
        {
            current_matm = mmatm.get(atm.AccountId);
            if(!current_matm.containsKey(atm.UserOrGroupId))
            {
                current_matm.put(atm.UserOrGroupId, atm);
                mmatm.put(atm.AccountID,current_matm);
            }
        }
        else
        {
              current_matm.put(atm.UserOrGroupId, atm);
             mmatm.put(atm.AccountID,current_matm);
        }
    }
    
    for(id aid:account_Id)
    {
        account_from = ShareFromTo.get(aid);
        for(id PartnerID : account_from)
        {
            if(UserRoleMapByRoleIDbyAccountId.containsKey(PartnerID))
            {
                //Set the user or group being granted access
                UserRoleMapByRoleID = UserRoleMapByRoleIDbyAccountId.get(PartnerID);
                urolelist = UserRoleMapByRoleID.values();
                for(Group eachUserRole:urolelist)
                {
                    if(mmatm.containsKey(aid))
                    {
                        current_matm = mmatm.get(aid);
                        //Set the access level
                        if(!current_matm.containsKey(eachUserRole.id))
                        {
                            //Instantiate the Sharing objects
                            newAccountShare = new AccountShare();
                            newAccountShare.AccountAccessLevel = 'Read';
                            newAccountShare.AccountId = aid;
                            newAccountShare.OpportunityAccessLevel = 'None';
                            newAccountShare.CaseAccessLevel='Edit';
                            newAccountShare.UserOrGroupId = eachUserRole.id;
                            listnewAccountShare.add(newAccountShare);
                        }
                        else
                        {      
                            
                            system.debug('ShareExists');
                        }
                    }
                    else
                    {
                        
                            newAccountShare = new AccountShare();
                            newAccountShare.AccountAccessLevel = 'Read';
                            newAccountShare.AccountId = aid;
                            newAccountShare.OpportunityAccessLevel = 'None';
                            newAccountShare.CaseAccessLevel='Edit';
                            newAccountShare.UserOrGroupId = eachUserRole.id;
                            listnewAccountShare.add(newAccountShare);
                    }
                    
                }
            }
        }
    }
    
    
    system.debug(newAccountShare);
   
   
   
   
   // map<id, Group> UserRoleMapByRoleID1 = new map<id, Group>();
    list<Group> urolelist1 = new list<Group>();
   for(Account acc1:Trigger.new)
    {
    
    if(acc1.Authorized_Support_Partner__c ==false)
    {
      for(id aid1:account_id1)
      {
        account_from = ShareFromTo.get(aid1);
        if(account_from1 != null)
        {
        for(id PartnerID1 : account_from1)
        {
            if(UserRoleMapByRoleIDbyAccountId1.containsKey(PartnerID1))
            {
                //Set the user or group being granted access
                UserRoleMapByRoleID1 = UserRoleMapByRoleIDbyAccountId1.get(PartnerID1);
                urolelist1 = UserRoleMapByRoleID1.values();
                for(Group eachUserRole1:urolelist1)
                {
                    if(mmatm1.containsKey(aid1))
                    {
                        current_matm1 = mmatm1.get(aid1);
                        //Set the access level
                        if(!current_matm1.containsKey(eachUserRole1.id))
                        {
                            System.debug('INSIDE IF');
                        }
                        else
                        {      
                            System.debug('USER EXISTS');
                            AccountShare newAccountShare2 = [select id from AccountShare where AccountId = :aid1 and UserOrGroupId =: eachUserRole1.id];
                            listnewAccountShare2.add(newAccountShare2);
                            system.debug('ShareExists');
                        }
                    }
                    else
                    {
                        
                     }
                    
                }
            }
        }
        }
    }
       
       if(listnewAccountShare2.size()>0)
       {
       delete listnewAccountShare2;
       }
    }
    else
    {
       list<Database.Saveresult> lsr = Database.insert(listnewAccountShare,false);
       
    // Insert sharing records and capture save result
    // The false parameter allows for partial processing if multiple records are passed
    // into the operation
   
    // Create counter
     i=0;
    // Process the save results
    
    for(Database.SaveResult sr : lsr){
        if(!sr.isSuccess()){
            // Get the first save result error
            Database.Error[] err = sr.getErrors();
            for(Database.Error err1:err){
                system.debug(err1);
            }
        }else{
            system.debug(sr);
        }
         
        i++;
     }
    }
    
    }
    
}

Trigger is supposed to manage partner authorization and access to manage support on behalf of their end users

Trigger functionality: When a partner is added to the partner related list in an account the account is marked as an Authorized support partner which is a checkbox feild I created. After the partner is manually entered in the partner related list in the account and the Authorized support partner feild is checked the partner should automatically get sharing access to the account here is my code below can someone help me out please:

 

 

trigger PartnerSharing onAccount (afterinsert, afterupdate){

set<id> account_id = new set<id>();

set<id> account_to = new set<id>();

map<id,id> ShareFromTo = new map<id,id>();

map<id, AccountTeamMember> matm = newmap<id, AccountTeamMember>();

map<id, map<id, AccountTeamMember>> mmatm = new map<id, map<id, AccountTeamMember>>();

map<id, AccountTeamMember> current_matm = newmap<id, AccountTeamMember>();

map<id, map<id, AccountTeamMember>> current_mmatm = new map<id, map<id, AccountTeamMember>>();

map<id, UserRole> UserRoleMapByRoleID = new map<id, UserRole>();

map<id, map<id, UserRole>>  UserRoleMapByRoleIDbyAccountId = new map<id, map<id, UserRole>>();  

integer i=0; 

UserRole urole = newUserRole();

list<AccountShare> listnewAccountShare = new list<AccountShare>();

AccountShare newAccountShare;

list<UserRole> urolelist = new list<UserRole>();

for (Account acc: trigger.new){

if (acc.Authorized_Support_Partner__c == true){

account_Id.add(acc.id);

system.debug('Added this ID to Account_ID ' + acc.id);

}

system.debug(account_Id);

system.debug('fire');

 

list<Partner> p = [Select p.Role, p.AccountToId, p.AccountFromId From Partner p where p.AccountToId in: account_id and role = 'Channel Partner - Auth Support'];

 

 

for(Partner pc: p){

if(!account_to.contains(pc.accountToId)){

account_to.add(pc.accountToId);

system.debug('Account to id is: ');

}

if(!ShareFromTo.containsKey(pc.accountFromId)){

ShareFromTo.put(pc.accountFromId, pc.accountToId);

system.debug('Account From and Account to are: ');

}else{

if(pc.accountToId!=ShareFromTo.get(pc.accountFromId)){

ShareFromTo.put(pc.accountFromId, pc.accountToId);

system.debug('ShareFromTo: ');

}

}

}

 

list<UserRole> ur = [Select u.PortalType, u.PortalRole, u.PortalAccountOwnerId, 

u.PortalAccountId, u.Name, u.Id, u.ContactAccessForAccountOwner, 

u.CaseAccessForAccountOwner 

From UserRole

where u.PortalAccountOwnerId in :account_to];

for(UserRole uri:ur){

system.debug('Get user role id' + uri.id);

if(!UserRoleMapByRoleIDbyAccountId.containsKey(uri.PortalAccountId)){

UserRoleMapByRoleID.put(uri.id, uri);

UserRoleMapByRoleIDbyAccountId.put(uri.PortalAccountId, UserRoleMapByRoleID);

}else{

UserRoleMapByRoleID = UserRoleMapByRoleIDbyAccountId.get(uri.PortalAccountId);

if(!UserRoleMapByRoleID.containsKey(uri.id)){

UserRoleMapByRoleID.put(uri.id, uri);

UserRoleMapByRoleIDbyAccountId.put(uri.PortalAccountId, UserRoleMapByRoleID);

}

}

 

list<AccountTeamMember> ls = [Select a.UserId, a.TeamMemberRole, a.AccountId, a.AccountAccessLevel From AccountTeamMember a where a.AccountId in :account_Id ];

 

for(AccountTeamMember atm: ls){

if(mmatm.containsKey(atm.AccountId)){

current_matm = mmatm.get(atm.AccountId);

if(!current_matm.containsKey(atm.UserId)){

current_matm.put(atm.UserId, atm);

mmatm.put(atm.AccountID,current_matm);

}

}else{

current_matm.put(atm.UserId, atm);

mmatm.put(atm.AccountID,current_matm);

}

}

 

for(id aid:account_Id){

if(UserRoleMapByRoleIDbyAccountId.containsKey(uri.PortalAccountId)){

UserRoleMapByRoleID = UserRoleMapByRoleIDbyAccountId.get(aid);

urolelist = UserRoleMapByRoleID.Values();

for(UserRole eachUserRole:urolelist){

if(mmatm.containsKey(aid)){

current_matm = mmatm.get(aid);

if(!current_matm.containsKey(eachUserRole.id)){

newAccountShare = new AccountShare();

    newAccountShare.AccountAccessLevel = 'Read';

    newAccountShare.AccountId = aid;

    newAccountShare.OpportunityAccessLevel = 'Private';

    newAccountShare.CaseAccessLevel='Edit';

    newAccountShare.UserOrGroupId = eachUserRole.id;

    listnewAccountShare.add(newAccountShare);

}else{

system.debug('ShareExists');

}

}else{

 

            newAccountShare = new AccountShare();

    newAccountShare.AccountAccessLevel = 'Read';

    newAccountShare.AccountId = aid;

    newAccountShare.OpportunityAccessLevel = 'Private';

    newAccountShare.CaseAccessLevel='Edit';

    newAccountShare.UserOrGroupId = eachUserRole.id;

    listnewAccountShare.add(newAccountShare);

}

 

}

}

}

 

system.debug(newAccountShare);

   list<Database.Saveresult> lsr = Database.insert(listnewAccountShare,false);

  // Create counter 

i=0;

// Process the save results 

  

    for(Database.SaveResult sr : lsr){

        if(!sr.isSuccess()){

            // Get the first save result error 

            Database.Error err = sr.getErrors()[0];

            // Check if the error is related to a trivial access level 

            // Access levels equal or more permissive than the object's default  

            // access level are not allowed.  

            // These sharing records are not required and thus an insert exception is  

            // acceptable.  

            if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  

                                        &&  err.getMessage().contains('AccessLevel'))){

                // Throw an error when the error is not related to trivial access level. 

               trigger.newMap.get(sr.Id).

               addError('Unable to grant sharing access due to following exception: ' + err.getMessage());

            }

        } 

        i++;

    }

}

}

Trigger is supposed to manage partner authorization and access to manage support on behalf of their end users

Trigger functionality: When a partner is added to the partner related list in an account the account is marked as an Authorized support partner which is a checkbox feild I created. After the partner is manually entered in the partner related list in the account and the Authorized support partner feild is checked the partner should automatically get sharing access to the account here is my code below can someone help me out please:

 

 

trigger PartnerSharing onAccount (afterinsert, afterupdate){

set<id> account_id = new set<id>();

set<id> account_to = new set<id>();

map<id,id> ShareFromTo = new map<id,id>();

map<id, AccountTeamMember> matm = newmap<id, AccountTeamMember>();

map<id, map<id, AccountTeamMember>> mmatm = new map<id, map<id, AccountTeamMember>>();

map<id, AccountTeamMember> current_matm = newmap<id, AccountTeamMember>();

map<id, map<id, AccountTeamMember>> current_mmatm = new map<id, map<id, AccountTeamMember>>();

map<id, UserRole> UserRoleMapByRoleID = new map<id, UserRole>();

map<id, map<id, UserRole>>  UserRoleMapByRoleIDbyAccountId = new map<id, map<id, UserRole>>();  

integer i=0; 

UserRole urole = newUserRole();

list<AccountShare> listnewAccountShare = new list<AccountShare>();

AccountShare newAccountShare;

list<UserRole> urolelist = new list<UserRole>();

for (Account acc: trigger.new){

if (acc.Authorized_Support_Partner__c == true){

account_Id.add(acc.id);

system.debug('Added this ID to Account_ID ' + acc.id);

}

system.debug(account_Id);

system.debug('fire');

 

list<Partner> p = [Select p.Role, p.AccountToId, p.AccountFromId From Partner p where p.AccountToId in: account_id and role = 'Channel Partner - Auth Support'];

 

 

for(Partner pc: p){

if(!account_to.contains(pc.accountToId)){

account_to.add(pc.accountToId);

system.debug('Account to id is: ');

}

if(!ShareFromTo.containsKey(pc.accountFromId)){

ShareFromTo.put(pc.accountFromId, pc.accountToId);

system.debug('Account From and Account to are: ');

}else{

if(pc.accountToId!=ShareFromTo.get(pc.accountFromId)){

ShareFromTo.put(pc.accountFromId, pc.accountToId);

system.debug('ShareFromTo: ');

}

}

}

 

list<UserRole> ur = [Select u.PortalType, u.PortalRole, u.PortalAccountOwnerId, 

u.PortalAccountId, u.Name, u.Id, u.ContactAccessForAccountOwner, 

u.CaseAccessForAccountOwner 

From UserRole

where u.PortalAccountOwnerId in :account_to];

for(UserRole uri:ur){

system.debug('Get user role id' + uri.id);

if(!UserRoleMapByRoleIDbyAccountId.containsKey(uri.PortalAccountId)){

UserRoleMapByRoleID.put(uri.id, uri);

UserRoleMapByRoleIDbyAccountId.put(uri.PortalAccountId, UserRoleMapByRoleID);

}else{

UserRoleMapByRoleID = UserRoleMapByRoleIDbyAccountId.get(uri.PortalAccountId);

if(!UserRoleMapByRoleID.containsKey(uri.id)){

UserRoleMapByRoleID.put(uri.id, uri);

UserRoleMapByRoleIDbyAccountId.put(uri.PortalAccountId, UserRoleMapByRoleID);

}

}

 

list<AccountTeamMember> ls = [Select a.UserId, a.TeamMemberRole, a.AccountId, a.AccountAccessLevel From AccountTeamMember a where a.AccountId in :account_Id ];

 

for(AccountTeamMember atm: ls){

if(mmatm.containsKey(atm.AccountId)){

current_matm = mmatm.get(atm.AccountId);

if(!current_matm.containsKey(atm.UserId)){

current_matm.put(atm.UserId, atm);

mmatm.put(atm.AccountID,current_matm);

}

}else{

current_matm.put(atm.UserId, atm);

mmatm.put(atm.AccountID,current_matm);

}

}

 

for(id aid:account_Id){

if(UserRoleMapByRoleIDbyAccountId.containsKey(uri.PortalAccountId)){

UserRoleMapByRoleID = UserRoleMapByRoleIDbyAccountId.get(aid);

urolelist = UserRoleMapByRoleID.Values();

for(UserRole eachUserRole:urolelist){

if(mmatm.containsKey(aid)){

current_matm = mmatm.get(aid);

if(!current_matm.containsKey(eachUserRole.id)){

newAccountShare = new AccountShare();

    newAccountShare.AccountAccessLevel = 'Read';

    newAccountShare.AccountId = aid;

    newAccountShare.OpportunityAccessLevel = 'Private';

    newAccountShare.CaseAccessLevel='Edit';

    newAccountShare.UserOrGroupId = eachUserRole.id;

    listnewAccountShare.add(newAccountShare);

}else{

system.debug('ShareExists');

}

}else{

 

            newAccountShare = new AccountShare();

    newAccountShare.AccountAccessLevel = 'Read';

    newAccountShare.AccountId = aid;

    newAccountShare.OpportunityAccessLevel = 'Private';

    newAccountShare.CaseAccessLevel='Edit';

    newAccountShare.UserOrGroupId = eachUserRole.id;

    listnewAccountShare.add(newAccountShare);

}

 

}

}

}

 

system.debug(newAccountShare);

   list<Database.Saveresult> lsr = Database.insert(listnewAccountShare,false);

  // Create counter 

i=0;

// Process the save results 

  

    for(Database.SaveResult sr : lsr){

        if(!sr.isSuccess()){

            // Get the first save result error 

            Database.Error err = sr.getErrors()[0];

            // Check if the error is related to a trivial access level 

            // Access levels equal or more permissive than the object's default  

            // access level are not allowed.  

            // These sharing records are not required and thus an insert exception is  

            // acceptable.  

            if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  

                                        &&  err.getMessage().contains('AccessLevel'))){

                // Throw an error when the error is not related to trivial access level. 

               trigger.newMap.get(sr.Id).

               addError('Unable to grant sharing access due to following exception: ' + err.getMessage());

            }

        } 

        i++;

    }

}

}