+ Start a Discussion

Is it possible to put user's profile check in Trigger ?

Is it possible to put user's profile check in Trigger as we do in Validation rule(eg $Profile.Name ='System Administrator')
You could do something like this (using the System.UserInfo namespace):

public String userProfile{get;set;} public Profile profile; this.profile = [Select p.Name From Profile p where p.Id = :UserInfo.getProfileId()]; if(profile != null) userProfile = profile.Name; // Then do some validation here...



I'm currently trying to do this and am not quite following your answer.  My current trigger checks the opportunity probability percentage and denies deletion based on probability.  We want to add an exception that if the profile of the user trying to delete is the System Admin then it would not cause the trigger to fire.  Can you help me understand a little better how I could add that exception into my code?


trigger CannontDeleteOpportunity95 on Opportunity (before delete) {
        for (Opportunity Opps : trigger.old)
            if (Opps.Probability >= 95)
                Opps.addError('Error: You cannot delete this Opportunity.  Please contact a system Administrator for assistance');