[SalesForce] How to check whether user logged in as portal user or community user

How to check whether user logged in as portal user or community user through apex.

Through that i have to redirect to different Visual force pages.

Requirement:

For community user i have to keep one vf page for new case and portal user i have to keep another vf page for new case.

Thanks,
Suresh.

Best Answer

I would not use the Profile name, as suggested by Saroj. You could have multiple different Profiles within your Portal/Community, so you would have to add lots of clauses to the conditions where you check the Profile Name. And also, admins can easily change the name of the Profile, which would break your code.

Instead, I would use User.UserType, as described here:

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_user.htm#profilelicensetype

The category of user license. Each UserType is associated with one or more UserLicense records. Each UserLicense is associated with one or more profiles. In API version 10.0 and later, valid values include:

  • Standard: user license. This user type also includes Salesforce Platform and Salesforce Platform One user licenses. Label is Standard. PowerPartner: User whose access is limited because he or she is a partner and typically accesses the application through a partner portal or community. Label is Partner.
  • CSPLitePortal: user whose access is limited because he or she is an organization's customer and accesses the application through aCustomer Portal or community. Label is High Volume Portal.
  • CustomerSuccess: user whose access is limited because he or she is an organization's customer and accesses the application through a Customer Portal. Label is Customer Portal User.
  • PowerCustomerSuccess: user whose access is limited because he or she is an organization's customer and accesses the application through a Customer Portal. Label is Customer Portal Manager. Users with this license type can view and edit data they directly own or data owned by or shared with users below them in the Customer Portal role hierarchy.

  • CsnOnly: user whose access to the application is limited to Chatter. This user type includes Chatter Free and Chatter moderator users. Label is Chatter Free.

You can get this value by using UserInfo.getUserType()

Related Topic