1

Can anyone help me out .I have a trigger on Opportunitylineitem which gets fired on every profile .Now these are the profiles which are in the list

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

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

i tried adding the if(userinfo.getProfileId() != System Administrator-id) but the trigger gets fired again.I shall appreciate your help.

1 Answer 1

1

I try to limit my dml so. Create a utility class

public class ProfileTypeUtils {
    public static List<Profile> PROFILE_TYPES{
        get{
            if(PROFILE_TYPES == null){
                PROFILE_TYPES = [
                    Select id,name 
                    FROM Profile];
            }
            return PROFILE_TYPES;
        }
        private set;
    }
    public static Map<String,id>PROFILE_TYPE_BY_NAMES{
        get{
            if(PROFILE_TYPE_BY_NAMES == null){
                PROFILE_TYPE_BY_NAMES = new Map<String,Id>();
                for(Profile p : PROFILE_TYPES){
                    PROFILE_TYPE_BY_NAMES.put(p.name,p.id);
                }
            }
            return PROFILE_TYPE_BY_NAMES;
        }
        private set;
    }
 }

Call it in your trigger

Set<Id> profileTypeIDs = new Set<Id>{
    ProfileTypeUtils.PROFILE_TYPE_BY_NAMES.get('System Administrator'),
    ProfileTypeUtils.PROFILE_TYPE_BY_NAMES.get('Custom Marketing Users 10K 25K')
    ProfileTypeUtils.PROFILE_TYPE_BY_NAMES.get('Customer Service User')
    ProfileTypeUtils.PROFILE_TYPE_BY_NAMES.get('Fulfillment User')};

And adjust your If statement

2
  • :Thanks for your reply.I tried the above code.But the trigger is not restricting the profiles which are outside in the list.
    – nikkey
    Jan 9, 2015 at 9:22
  • What is the logic you did?
    – EricSSH
    Jan 9, 2015 at 17:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .