function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shanky77Shanky77 

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

 

SFDCDevQASFDCDevQA

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) {
 if(System.Trigger.IsDelete)
        {
        for (Opportunity Opps : trigger.old)
            if (Opps.Probability >= 95)
          
                {
                Opps.addError('Error: You cannot delete this Opportunity.  Please contact a system Administrator for assistance');
                }
        }          
}

 

Thanks,

Amanda