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
MikeBennettMikeBennett 

Field update based on Opportunity Owner Role

Hoping to get some help here;  we wrote an Apex Trigger to populate a field in Opportunties with a calculation based on the record Owner's role.  It works fine for new or updated records but I can only update the existing records 50 at a time because of the SOQL limits.  Does anyone know a better way to write this code so it isn't using SOQL querries or is there a way to update mass records without hitting those limits?

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
trigger OppDepartmentUpdate on Opportunity (before insert, before update)
{
 
List<Opportunity> oppList = new List<Opportunity>();
//Opportunity[] assetList = Trigger.new;
 
  for(Opportunity opp : Trigger.New) {
   System.debug('start'); 
   String ownerId = opp.ownerId;
   List<User> users = [select UserRoleId from User where Id= :ownerId];
   String userRole;
   for(User u : users){
       System.debug('userRoleId:');
       System.debug(u.UserRoleId);
       List<UserRole> userRoles = [select Name from UserRole where Id= :u.UserRoleId];
       for(UserRole ur : userRoles){
           System.debug('userRole:');
           System.debug(ur.Name);
           userRole = ur.Name;
       }
   }
   
   System.debug(opp.Owner.UserRole);
       if((userRole=='Director-Sales')
       ||(userRole=='Sales Manager')
       ||(userRole=='Sales Generalist-AE')
       ||(userRole=='Sales Generalist-ISR')
       ||(userRole=='Sales Generalist-SE'))
    {
             opp.Department__c = 'Sales';
             oppList.add(opp);
       }
      else{
      
             opp.Department__c = 'Support';
             oppList.add(opp);
     }
  }
  //if(oppList.size() > 0 ) {
    // Database.update(oppList, false);
   //}
}

 

kcpluspluskcplusplus

Your doing a query inside of a loop, which is never a best practice. Rather than doing that, you might try doing a after insert, after update trigger that collects all of the new or updated record id's, than loops through them and do the query in the loop to avoid hitting that limit. 

 

--KC

RishiKaliaRishiKalia

Hi Mike,

 

KC is right. Try to modify your code and remove SOQL from the loops. For eg :

 

 List<Opportunity> oppList = new List<Opportunity>();
 List<string> ownerId = new list<string>();
 list<String> userRole = new list<string>() ;
 
 for(Opportunity opp1 : Trigger.New) {
    ownerId.add(opp1.ownerId);
 }
 
 List<User> users = [select UserRoleId from User where Id in: ownerId];
 
 for(User u : users){
    userRole.add(u.UserRoleId);
 }
 List<UserRole> userRoles = [select Name from UserRole where Id in: UserRole];
 
  for(Opportunity opp : Trigger.New) {
   for(User u : users){
       for(UserRole ur : userRoles){
           userRole = ur.Name;
       }
   }
  

I hope this will help you.

 

Rishi kalia