0

Can anyone help me out .I have a trigger on Opportunitylineitem which gets fired on every profile .Now the trigger should not get fired for few of the profiles like

'System Administrator' && 'Custom Marketing Users 10K 25K' && 'Customer Service User' && 'Fulfillment User' Where should i add these user profiles in my code.Any Suggestion plz.

Here is my code :

trigger oli_multiple_products_before_insert on OpportunityLineItem (before insert) {

    for (OpportunityLineItem oli : Trigger.new) {
        if (Trigger.isInsert) {

            Integer line_Count = [SELECT COUNT()
                                    FROM OpportunityLineItem o
                                    WHERE o.OpportunityId = :oli.OpportunityId
                                    AND o.PriceBookEntryId = :oli.PriceBookEntryId  ];

            if (line_Count > 0) {
                oli.addError('A Product can not be added more than once to the Opportunity.');
         }                    
        }
    }
}
  • 1
    Segregate your code in another apex class. Create a list of profile IDs that you have listed. Set the static boolean variable checking whether the user's profile ID in list or not. – Shailesh Patil Jan 7 '15 at 9:40
  • But don't use IDs which change from org to org and are very difficult to test. Instead use the profile names. And for good measure, put them in a custom setting so you can change what profiles the trigger fires for using config instead of code. – Dominic Jan 7 '15 at 9:52
  • @ Shailesh Patil:Can u help me out with an Example – nikkey Jan 7 '15 at 9:59
  • @nikkey : Please refer example given by Sravan below. – Shailesh Patil Jan 7 '15 at 10:26
  • @Sravan Alaparthi:Thanks for your reply.But im trigger still gets fired for the profiles.When the product is added more than once and throws the add error message.As per my sceanrio it should not throw the Error message when the product is added more than once. – nikkey Jan 8 '15 at 9:18
2

You can get the profile id of the logged in user by userinfo.getProfileId().

Just write a if condition checking making sure logged in users profile id is not equal to to the profile ids mentioned by you. For example:

    for (OpportunityLineItem oli : Trigger.new) {
            if (Trigger.isInsert) {
        //add if condition here     
     if(userinfo.getProfileId() != System Administrator-id){
//you can use && condition to add multiple profiles
                Integer line_Count = [SELECT COUNT()
                                        FROM OpportunityLineItem o
                                        WHERE o.OpportunityId = :oli.OpportunityId
                                        AND o.PriceBookEntryId = :oli.PriceBookEntryId  ];

                if (line_Count > 0) {
                    oli.addError('A Product can not be added more than once to the Opportunity.');
             }                    
    }
            }
        }
        }

For removing hard-coding of ids you can write a query on profile object and get ids.

EX: [SELECT ID FROM Profile WHERE NAME IN :Your_list_of_profiles]

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.