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
APEXNewbAPEXNewb 

trigger on oppty should not run for specific users

First - I'm still stumbling my way around APEX so bear with me.

I'm trying to write a trigger that prevents our users from adding a specific product to opptys.

The trick here is that the sysadmin (me) and the product manager should be excluded and therefore allowed to add these products.  
So far, this works in general but I can't seem to get the exclude to work.

Ideas?  TIA

 

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
trigger OpportunityOEM on Opportunity (after update) {



  Map<ID, Opportunity> opstotest = new Map<ID, Opportunity>();
  
   // Only Product Managers can use this product
   for(Opportunity op: trigger.new)


  {
    {
      opstotest.put(op.id, op);  
    }
  }
  if(opstotest.size()==0) return;
    
  
  // Grab all product line items
  List<OpportunityLineItem> olis = [Select ID, OpportunityID from OpportunityLineItem where PriceBookEntry.Product2.Family='OEM' 
  ];

  
  Set<ID> opstofail = new Set<ID>();
  for(OpportunityLineItem oli: olis)
  {
    opstofail.add(oli.OpportunityID);
  }
  
  for(ID optofail: opstofail)
  {
  opstotest.get(optofail).addError('OEM products must be approved by Product Mgmt.');
  
  

  }
  
}

dipu2dipu2

Try using UserInfo to get information about the user and skip the body of the trigger.

You could use any of these methods from UserInfo to identiy the user.

 

getUserId StringReturns the context user's ID
getUserName StringReturns the context user's login name
getUserRoleId StringReturns the context user's role ID
getUserType StringReturns the context user's type
APEXNewbAPEXNewb

Ok sorry to sound like an idiot but how why would I skip the body of the trigger?

dipu2dipu2

Check this post. The code in the question has your answer. Do not worry about the problem in that post for now.

http://boards.developerforce.com/t5/Apex-Code-Development/Trigger-running-twice-because-of-workflow/m-p/545107/highlight/false#M99244

 

Or just read the following code snippet. It will give you the idea. At the beginning of the trigger put the following lines. You probably will change the getProfileId() to getUserName() or getUserType().

 

Id currentUserProfileId = UserInfo.getProfileId();
 
  //Exclude all System Admins from creating this task
  if ('00e30000000dSKN' == currentUserProfileId))
    return;

 

APEXNewbAPEXNewb

Thanks, will do.

Appreciate the help.