• APEXNewb
  • NEWBIE
  • 0 Points
  • Member since 2012

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

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

  }
  
}

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

  }
  
}