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
MrkennyMrkenny 

Need help t bulkify my trigger

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);

        }

    }

    

       

    

}

Onttu Lindeman 12Onttu Lindeman 12
Is this meant to for a partner user to be granted access to the account they have been activated as a partner user too?
So user A is activated as a partner to Account A, this writes a rule granting them permssion to account. Currently my partners are gettting automatic access tho thier account when activated, on the role partner user. Not exec or manager roles.