4

I'm building a Communities site with several custom objects that Communities users should access through Visualforce pages. All other Salesforce users should access these objects through the standard Salesforce pages: record list, record detail ("view"), record new/edit.

Seems like the easiest way to handle this would be to override the list, detail, and new/edit pages for these objects with the Visualforce pages, and use the apex:page action parameter to redirect all non-communities users to the standard Salesforce pages, but it doesn't look like you can detect whether the current user is a Communities user in Visualforce. (I need to detect userType, and it doesn't look like I can do so with the global $User variable.)

I could detect the user type using a controller extension...

// set containing the three Communities user types
private set<String> Communities_user_types = new set<String>{'CspLitePortal','CustomerSuccess','PowerCustomerSuccess'};

private final ApexPages.StandardController ctrl;
public Redirect_Ext(ApexPages.StandardController stdController) {  
    this.ctrl = stdController;
}

private final string theUserType = UserInfo.getUserType();

public void isCommunitiesUser(){  // This would be changed to a pageReference, of course.
    if(Communities_user_types.contains(theUserType)){
            system.debug('******** True');
    } else {
        system.debug('******** False');
    }
}

...and then redirect non-communities users to the appropriate standard (non-overridden) page. The problem there is that, as far as I can tell, I'm stuck creating a different extension for every page!

  • Object_1 list view page
  • Object_1 detail page
  • Object_1 new/edit page
  • Object_2 ...

...and so on. If I could pass the object prefix or record ID, and an indicator telling the extension which page type (list, detail, new/edit) it was being called from, then I could write a nicely abstracted extension and use it to return the correct URL, but I don't see a way of making that work.

Another option would be to code the URL for the standard page (including nooverride=1 in the querystring) into the page action of each visualforce page, and just use a method in the extension to return true if the page should redirect: Something like, "if isCommunitiesUser == true, set the action to redirect; otherwise, set the action to null." But I can't find a way of using the return value from a method in visualforce logic.

Does anyone have any other ideas, or see something that I'm overlooking?

Thanks in advance!

Shane

  • I'm even having ugly ideas like using javascript remoting to determine the user type and handle the redirect on page load. This can't be that complicated. Help!! – Shane Steinfeld Dec 16 '14 at 18:18
  • 1
    All community users are associated with a Contact record. You can check if the current user is a community user using {!$User.ContactId}. This value will be non-null for community users. – Aayush K Dec 16 '14 at 19:01
0

$User.UserType provides the type of user directly in VF. The possible values are in the User Object documentation. But $User.IsPortalEnabled may be even closer to what you need.

With Usertype I have this sort of thing in my overrides (just showing all the current types for demonstration, here they all go to null just like the default at the end):

<apex:page action="{!case($User.UserType,
                          "Standard",urlFor($Action.Case.Tab, $ObjectType.Case, null, true) /*show default page*/,
                          "PowerPartner",null /*show this page (=the override)*/,
                          "CSPLitePortal",null,
                          "CustomerSuccess",null,
                          "PowerCustomerSuccess",null,
                          "CsnOnly",null,
                          null)}"
1

Of course, there's always the Ugly Javascript Solution:

<apex:page>
    <apex:includeScript value="/soap/ajax/30.0/connection.js" />
    <apex:includeScript value="/soap/ajax/30.0/apex.js" />

    <script>
        sforce.connection.sessionId = "{!$Api.Session_ID}";

        var records = sforce.connection.query("SELECT UserType FROM User WHERE Id = '{!$User.Id}'").getArray('records');

        if (records.length) {
            var userType = records[0].UserType;
            alert(userType); // do something more in here, like redirect...
        }
    </script>
</apex:page>
0

You can use ConnectAPI for detecting community

 public class CommunityHelper{

 public static String communityId=Network.getNetworkId();

 public static ConnectApi.Community getCommunityInfo(){
    if(communityId!=null){
        return connectapi.Communities.getCommunity(communityId);
    }else {
        return null;
    }
 }

}

If community Id is null then you are not in community and for User In community you will see community object being returned .

Edit

The only way I see is to use Id.getsobjecttype() to get object name and use custom setting to store object name and then store the different URL there and Use redirection to specified URL in custom setting

  • Hmm. Thanks for that shortcut -- it would save me a few lines of code and make it more evergreen than hard-coding the user types in a map. It doesn't get me around the problem though: I want to be able to either write one class that can handle redirecting to the correct page -- record list, record view, or record new/edit -- or find a way to use the page action to do the same. – Shane Steinfeld Dec 16 '14 at 18:33
  • updated the answer .Hope I am clear Shane :) – Mohith Shrivastava Dec 16 '14 at 18:43

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.