Concepts or coding lessons of Salesforce that you can implement easily

Simplest Way To Get Current User Profile In Apex Class

how to do it in salesforce
Sample Code :
Id profileId= userinfo.getProfileId();
String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
system.debug('ProfileName'+profileName);

userinfo.getProfileId() function give user`s profile id, use that id and query to profile to get profile name.

Some other UserInfo Methods :


The following are methods for UserInfo. All methods are static.

getDefaultCurrency()
This Function returns default currency code of the context user's for multiple currency organizations OR It can also returns the organization's currency code for single currency organizations.
getDefaultCurrency function returns null for single currency organizations for Apex saved using SalesforceAPI version 22.0 or earlier,.

getFirstName()
This Function returns the context user's first name.

getLanguage()
This Function returns the context user's language.

getLastName()
This Function returns the context user's last name.

getLocale()
This Function returns the context user's locale.

getName()
This Function returns the context user's full name. The format of the name rely on the language preferences specified for that organization.

getOrganizationId()
This Function returns the context organization's ID.

getOrganizationName()
This Function returns the context organization's company name.

getProfileId()
This Function returns the context user's profile ID.

getSessionId()
This Function returns the session ID for the current session.

getTimeZone()
This Function returns the current user’s local time zone.

getUiTheme()
This Function returns the preferred theme for the current user. With the help of this getUiThemeDisplayed () you can check the actual theme displayed to the current user.

getUiThemeDisplayed()
This Function returns the theme being displayed for the current user.

The User.UITheme and User.UIThemeDisplayed global variables and the UserInfo.getUiTheme() and UserInfo.getUiThemeDisplayed() Apex utility methods are improved to support the Salesforce1 mobile app and Lightning Experience.
These system calls and existing variables have expanded the range of return values to support the new user experience contexts. Possible return values include the following:

  1. Theme1—Obsolete Salesforce theme
  2. Theme2—Salesforce Classic 2005 user interface theme
  3. Theme3—Salesforce Classic 2010 user interface theme
  4. Theme4d—Modern “Lightning Experience” Salesforce theme
  5. Theme4t—Salesforce1 mobile Salesforce theme
  6. PortalDefault—Salesforce Customer Portal theme
  7. Webstore—Salesforce AppExchange theme
Above system calls and global variables are more delicate methods of detecting the user experience context and also it can replace other. Like in Lightning we can check whether the sforce.one JavaScript global variables exist or not. 
This is very useful to write Visualforce pages and apps that adapt to the user experience context in which they’re running.

getUserEmail()
This Function returns the current user’s email address.

getUserId()
This Function returns the context user's ID

getUserName()
This Function returns the context user's login name.

getUserRoleId()
This Function returns the context user's role ID.

getUserType()
This Function returns the context user's type.

isCurrentUserLicensed(namespace)
This Function returns true if the context user has a license to the managed package denoted by the namespace. Otherwise, returns false.

isMultiCurrencyOrganization()
Specifies whether the organization uses multiple currencies.

Thank you!!!


More Salesforce Blogs:

Check out Salesforce Daily Limit Only in 5 Simplest Steps
Learning Pagination In Salesforce Is Not Difficult At All ! You Just Need 3 Easy Steps
How To Learn Get Field Values From Visualforce Page To Apex Class Controller Without Losing Your Mind
Main Difference Between ISBLANK And ISNULL in Salesforce
How To Get Total Amount Of Records Processed In Batch Job In 10 Minutes And Still Look Your Best 
Export VisualForce Data into Excel Sheet in 3 Easiest Steps
7 Easy Steps to Generate Apex Class From WSDL In Salesforce
Simplest Way To Find Number of Days Between Two Dates in Salesforce
3 Easy Steps To Send Emails With Attachment From Your Apex Class In Salesforce
How Insert Comma In Formula Fields Can Help You Improve Your Productivity
Simple Guidance For You In Access Of Subquery Field Value Using Apex - Upwards Traversal.
Access Subquery Field Value Using Apex in 2 Easy Steps- Downwards Traversal

How Learning Enable Inline Editing In Visual Force Pages Could Save Your Money And Time

Enjoy! If you have any questions, comments etc. please feel free to let me know. As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.
loading...

No comments:

Post a Comment