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
Vasu@blrVasu@blr 

System.LimitException: Too many script statements: 200001

My code is hittting governor limits incase of multiple records update or insert.

Please help me to reduce number of script statements in this code,

 

Sometimes eventhoug we are not updating anything regarding to account, this trigger is calling,

we have rollup summary fields for this filed from another object is that the reason?

rigger Behr_InsertAccountTeamMember_US on Account (after insert,after update) {
 List<AccountTeamMember> lstAcTeam = new List<AccountTeamMember>();

List<UserRole> lstrole=new List<Userrole>();
map<ID,set<ID>> usermap=new map<ID,set<ID>>();
map<Id,Id> maprole=new map<Id,Id>();
map<Id,Id> accmap=new map<Id,Id>();
map<Id,Id> mapownerrole=new map<Id,Id>();
List<User> ownerrolelst=new List<User>();
set<ID> setOwner=new set<Id>();
List<User> userList=new List<User>();
lstrole=[select id,ParentRoleId from UserRole];
set<ID> Parentlst=new set<ID>();
map<Id,Id> userproId=new Map<Id,Id>();
set<Id> TeamMember=new Set<Id>();
List<AccountShare> lstAccShare =new List<AccountShare>();
List<AccountShare> lstUpdateAccShare = new List<AccountShare>();

set<Id> accid=new set<Id>();

for(UserRole ur:lstrole){
maprole.put(ur.id,ur.ParentRoleId);

}
Profile p=[select Id from Profile where name='Standard User Level 2'];
//Profile p=[select Id from Profile where name='System Administrator'];

userList=[select id ,Userroleid,ProfileId from user where IsActive=true ];
for(User u1:userList){
userproId.put(u1.id,u1.ProfileId);
if(usermap.get(u1.userroleid)!=null){

usermap.get(u1.userroleId).add(u1.id);

}
else{
set<Id> setuid=new set<ID>();
setuid.add(u1.id);
usermap.put(u1.userroleid,setuid);

}
}

for(Account acc:trigger.new){
    accmap.put(acc.id,acc.ownerid);
    setOwner.add(acc.OwnerId);
    accid.add(acc.id);
}

ownerrolelst=[select id ,userroleid from user where id in:setOwner ];
Userrole adminrole1=[select id from Userrole where Name='System Administrator'];
system.debug('OOOO'+adminrole1.id);
Userrole chinarole=[select id from Userrole where Name='China Users'];
Userrole ITDEVrole=[select id from Userrole where Name='IT DEV'];
Userrole ProMktgadmin=[select id from Userrole where Name='Pro Mktg Administrator'];
Userrole ProMktguser=[select id from Userrole where Name='Pro Marketing User'];
 Userrole ProSalesSVP=[select id from Userrole where Name='Pro Sales SVP'];
for(User u:ownerrolelst){
mapownerrole.put(u.id,u.UserRoleId);

}
for(Id acccid:accmap.keyset()){
if((Trigger.isInsert ||(Trigger.isupdate && Trigger.newmap.get(acccid).Ownerid!=Trigger.oldmap.get(acccid).OwnerId))&& userproId.get(accmap.get(acccid))==p.id){
//Parentlst.add(mapownerrole.get(accmap.get(acccid)));
id role=mapownerrole.get(accmap.get(acccid));
Id ParentRoleNext=maprole.get(mapownerrole.get(accmap.get(acccid)));
system.debug('PPPPPPPPPPPPPPPP'+role);

if( role!=adminrole1.id && role!=chinarole.id && role!=ITDEVrole.id && role!=ProMktgadmin.id && role!=ProMktguser.id){

while(ParentRoleNext!=ProSalesSVP.id    ){
system.debug('within if block');
if(maprole.containskey(ParentRoleNext))
system.debug('PPPPPPPPPPPPPPPP'+role);
Parentlst.add(ParentRoleNext);
system.debug('IIIIIIII'+Parentlst);
ParentRoleNext=maprole.get(ParentRoleNext);
}
system.debug('@@@@@@'+Parentlst);
for(Id pid: Parentlst){
for(id uid1:usermap.get(pid)){
AccountTeamMember tm = new AccountTeamMember();
                tm.UserId =uid1;
                tm.AccountId = acccid;
                tm.TeamMemberRole = 'Sales Manager';

lstAcTeam.add(tm);
TeamMember.add(uid1);
}

}

}
AccountTeamMember tm1 = new AccountTeamMember();
                tm1.UserId =accmap.get(acccid);
                tm1.AccountId = acccid;
                tm1.TeamMemberRole = 'Sales Rep';
                lstAcTeam.add(tm1);
  // TeamMember.add(accmap.get(acccid));             
}
}
if(lstAcTeam.size()>0){
insert lstAcTeam;


 lstAccShare = [Select Id, AccountId, OpportunityAccessLevel, AccountAccessLevel,ContactAccessLevel, UserOrGroupId ,
                        CaseAccessLevel From AccountShare where AccountId IN: accid
                          AND UserOrGroupId IN: TeamMember] ;


         }       

 for (AccountShare ash:lstAccShare )
            {
            ash.AccountAccessLevel='Edit';
            ash.ContactAccessLevel='Edit';
            ash.OpportunityAccessLevel='Edit';
            ash.CaseAccessLevel ='Edit';
            
            lstUpdateAccShare.add(ash);
            } 
         update lstUpdateAccShare;

                        

}

 

Best Answer chosen by Admin (Salesforce Developers) 
SRKSRK

First think that i notic in you code is that you are query a object in list & then by using for loop you put the values in map


like line number 22 ,30, etc. in place of that you can use Map query

Ex:-

Map<Id,UserRole> maprole = new Map<id,UserRole>([select id,ParentRoleId from UserRole]);

 

& u can use it like red part is aded extra  (line no 66)

Id ParentRoleNext=maprole.get(mapownerrole.get(accmap.get(acccid))).ParentRoleId;

 

 

Using map query you can reduse lot of script statement

All Answers

liron169liron169
I didn't read all the code,
but take in mind that the System.debug call also count as line script.

So after you done your testing you can remove them to reduce the number of script line
liron169liron169

3 additional points thatn might help:

 

1. The codes:


if(maprole.containskey(ParentRoleNext))
system.debug('PPPPPPPPPPPPPPPP'+role);
Parentlst.add(ParentRoleNext)

I don't know if that is the porpuse, but the 3 line excecuted in every case.
the IF affecting only the Syste.debug line


2. I think you need to restart the list Parentlst after each iteration.

if( role!=adminrole1.id && role!=chinarole.id && role!=ITDEVrole.id && role!=ProMktgadmin.id && role!=ProMktguser.id)
...
Parentlst.clear();

otherwise this loop

for(Id pid: Parentlst)

running on all the data that every added to this list

 

 

3. at the 3th nested loop you can try:


for(Id pid: Parentlst)
{
    for(id uid1:usermap.get(pid))
    {
        /*AccountTeamMember tm = new AccountTeamMember();
        tm.UserId =uid1;
        tm.AccountId = acccid;
        tm.TeamMemberRole = 'Sales Manager';
        lstAcTeam.add(tm);*/
        lstAcTeam.add(new AccountTeamMember(UserId =uid1,
                        AccountId = acccid,
                        TeamMemberRole = 'Sales Manager'));
        TeamMember.add(uid1);
    }
}

SRKSRK

First think that i notic in you code is that you are query a object in list & then by using for loop you put the values in map


like line number 22 ,30, etc. in place of that you can use Map query

Ex:-

Map<Id,UserRole> maprole = new Map<id,UserRole>([select id,ParentRoleId from UserRole]);

 

& u can use it like red part is aded extra  (line no 66)

Id ParentRoleNext=maprole.get(mapownerrole.get(accmap.get(acccid))).ParentRoleId;

 

 

Using map query you can reduse lot of script statement

This was selected as the best answer
Vasu@blrVasu@blr

Thank you, your tips help me to reduce my script statements