You need to sign in to do that
Don't have an account?
APEXNewb
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.'); } } |
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.
Ok sorry to sound like an idiot but how why would I skip the body of the trigger?
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().
Thanks, will do.
Appreciate the help.