• MikeBennett
  • NEWBIE
  • 0 Points
  • Member since 2012

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

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