I need to get the portal id of a portal user in apex

I have the userId, but I cannot find a way to get it's portal id

Is that through the User? Profile?

does anyone know?


  • Why do you need that?
    – Raul
    Feb 21 '17 at 14:35
  • I am creating a "log in as portal user" button on the contact record home - for lightning (This capability is not available in lightning). I have developed successfully a lightning component action to do that (open a new url that logs in), and it needs the portal id that is hard coded, and I want it not to be hard coded. Feb 21 '17 at 21:41
  • What's your approach, to form URL same as in Log in to Community as User and redirect to it?
    – Raul
    Feb 21 '17 at 21:58
  • yes, redirect from within my lightning component action - it works good. Feb 21 '17 at 22:18
  • Wrote an answer, hope it helps!
    – Raul
    Feb 21 '17 at 22:44

From Setup, enter Customer Portal Settings in the Quick Find box, then select Customer Portal Settings Select a Customer Portal name, and on the Customer Portal detail page, the URL of the Customer Portal displays. The Portal ID is in the URL.
ref: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_header_loginscopeheader.htm

One important thing to note

The portalID value may be null or an empty key if there is no portal configured with this provider.

ref: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_auth_plugin.htm#apex_Auth_RegistrationHandler_updateUser

  • Thanks, but I am asking how to get the portalId from APEX and not from the Salesforce UI Feb 21 '17 at 20:54

It's quite possible that any give Portal User will have access to multiple Portals (or Communities). You will probably have to get the User's Profile, and Permission Sets, and using those results figure out which Portals (and/or Communities) that User has access to.

It's actually something that you should probably Plan for in advance, IF you intend to support more than one Portal/Community

  • How can Salesforce know that portal id of the contact then ? you can see that they have implemented the "Log in as portal user" button on the contact page - for portal contacts? Feb 21 '17 at 20:53
  • If the org has multiple Portals/Communities, when you click that "Log in as..." button it will ask which Portal/Community that you want to log in as. The button will show ALL Portals/Communities to choose from and will only verify that the Contact/User has access when a choice is made. Feb 22 '17 at 13:57
  • I understand, but it wont be a problem for me since I want to SOQL query the sObject (which I cannot find), and then I can decide to which record to login... Feb 22 '17 at 14:19

I did something like this for classic UI, you need NetworkId which represents the Community not which is different than PortalId.

I believe, PortalId is for old customer Portals.

Lets breakdown the URL of Log in to Community as User button/link:


oid={Org Id}&

retURL={Return Url}&



  1. Org Id can be retrieved by: UserInfo.getOrganizationId()

  2. Network Id can be retrieved by querying NetworkMember and filtering against the User:

    SELECT MemberId, NetworkId FROM NetworkMember WHERE MemberId = userId AND Network.Status = 'Live'

    • Status = 'Live' would mean published community, refer Network Object for more details.

Now, lets move ahead to code. With collecting pieces together, you can construct URL by following code:

public List<String> fetchCommunityLoginUrlForUser(Id userId) {

    List<String> lstCommunityUrls = new List<String>();

    String communityUrlTemplate = '/servlet/servlet.su?' +
        'oid={0}&' +
        'retURL={1}&' +
        'sunetworkid={2}&' +

    for(NetworkMember objNetworkMember: [SELECT MemberId, NetworkId 
        FROM NetworkMember WHERE 
        MemberId = :userId AND 
        Network.Status = 'Live']) {
            new List<String> {
                'your return URL',

    System.debug('lstCommunityUrls: ' + lstCommunityUrls);

    System.debug('lstCommunityUrls: ' + lstCommunityUrls);

    return lstCommunityUrls;

Result: This should give you URLs for active communities to which user is part of.

  • Thanks @Rahul. Yes - I am using the old portal and I understand now that I need to query the Portal sObject, but I cannot - I get INVALID_TYPE: sObject type 'Portal' is not supported. everywhere I try. Also the Network object is returning an INVALID_TYPE error, is there a way to enable these types? Feb 22 '17 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.