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
vinothvinoth 

Get Profile Name in Trigger.

I need to get current user profile name in trigger. I tried by using following way. Is there any possibility to get profile name directly without query.

 

trigger triggerName on CustomObject(after Insert){

Profile ProfileName = [select Name from profile where id = :userinfo.getProfileId()];
if(ProfileName.Name=='System Administrator'){

//code

}

}

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Unfortunately not, the UserInfo class gives you the details of the logged in user and that only has the id of the profile.

All Answers

bob_buzzardbob_buzzard

Unfortunately not, the UserInfo class gives you the details of the logged in user and that only has the id of the profile.

This was selected as the best answer
vinothvinoth

Is there any other option like system.profileName or session.ProfileName?

bob_buzzardbob_buzzard

Unforunately not. The UserInfo would be the holder for that type of information.

kevin Carotherskevin Carothers
I know I'm coming late to this party, but I solved this for my particular case by doing the following;

1.  define a formula field (named, say, "CurrentUserProfile) on an object  that has this code:      
      $UserProfile.Name

2.   In your trigger, you can just refer to the variable;
for(Task t :triggerNew) {
     // ...
     System.debug('Current User Profile: ' + t.CurrentuserProfile__c);
     // ...
    }

Hope this helps.... You can do the same for the Role name too.


A.X. PratapA.X. Pratap
its 2015. I know you would have already got what you wanted but just for the update. I created a formula field at the background on the object on which we are writing the trigger. UserProfile__c = $Profile.Name

Called that UserProfile__C in the trigger
trigger NoDeleteOpportunity on Opportunity (Before Delete) {
 
 for  (Opportunity q: trigger.old)
       { if (q.NoDelete__c == true)
                 { 
                 If (q.UserProfile__c == 'Sales Rep'){
                    q.adderror('Closed Opportunity can not be Updated. Please contact System Admin');
                    }
                  }         
                  }
}