0

We are using angular for front-end and Salesforce is being used for maintaining database. REST APIs have been written on Salesforce to communicate and fetch data to front-end. We have created Site/Community for Guest Users. These Guest Users have been assigned a Custom Profile with limited access. We have written a URL Rewritter class which after logging gets the Guest User to the desired URL. We have also written a VF page (HomePage) where the Guest User should be redirected after loggging in.This URL rewritter class is written in such a way that the URL is generated based on the logged in user which adds a prefix to the URL based on certain conditions.

We had another requirement that the Guest User (with Custom Profile) should be redirected to the VF Page (HomePage) however the other Profile users should redirect to default Communities Landing Page (https://LoginToAU.force.com/AU/). To achieve this, we added the condition in CommunitiesLandingController which has solved our problem.

My requirement is, if I am editing the actual URL (which comes based on certain conditions) https://abc.cs15.force.com/ABC/UKRGN to https://abc.cs15.force.com/ABC/UK, or https://abc.cs15.force.com/ABC/UKR or https://abc.cs15.force.com/ABC/UKRG- it gives me 'URL does not exist' error. I need our VF Homepage to be redirected on this URL. How to achieve this. Below is our URL Rewritter class and CommunitiesLandingController.

URL Rewritter Class:

global class UrlRewriterClass implements Site.UrlRewriter {    

//Maps a user-friendly URL to a Salesforce URL

global PageReference mapRequestUrl(PageReference friendlyUrl) {
System.debug('UrlRewriterClass.mapRequestUrl: ' + friendlyUrl);

 if(AppController.isPublicURL(friendlyUrl)) {
 return null;
 } else {

 String url = friendlyUrl != null ? friendlyUrl.getUrl() : null;
 String regionID ='';
 String AU = 'AU_';
 Boolean GuestUser = TRUE;  

 if (url.containsIgnoreCase('AU_Mb')){
regionID = 'Mb';
GuestUser = FALSE; 

 }
 else if (url.containsIgnoreCase('AU_Cb')){
regionID = 'Cb';
GuestUser = FALSE;            
 }  

if (GuestUser == FALSE) {

  String regionCode = '';
  String region = url.toLowerCase().substringAfter(AU.toLowerCase());

  if(region != null && region.length() > 0) {
      regionCode = '?sc=' + region;
  }

  return new PageReference('/apex/LoginTo_au_' + regionID + regionCode);

} else {         

  return new PageReference('https://LoginToAU.force.com/AU/');        


}
}
}

global PageReference[] generateUrlFor(PageReference[] yourSalesforceUrls) {
System.debug('UrlRewriterClass.generateUrlFor: ' + yourSalesforceUrls);
List<PageReference> pageReferenceFormatted = new List<PageReference>();

for(integer i = 0; i < yourSalesforceUrls.size(); i++) {
  PageReference pageReference = yourSalesforceUrls[i];

  if(pageReference.getUrl().containsIgnoreCase('REGION_au_mb')) {
    pageReferenceFormatted.add(new PageReference('/AU_Mb')); 
    } else if(pageReference.getUrl().containsIgnoreCase('REGION_au_cb')) {
    pageReferenceFormatted.add(new PageReference('/AU_Cb'));
    }

  }
return pageReferenceFormatted;
}
}

CommunitiesLandingController:

public with sharing class CommunitiesLandingController {

public CommunitiesLandingController() {       
}

public PageReference forwardToStartPage() {
     // Query the profile ids
     List<Profile> PROFILE = [SELECT Id, Name FROM Profile WHERE 
 Id=:userinfo.getProfileId() LIMIT 1];
       String MyProflieName = PROFILE[0].Name;

    // Return the appropriate landing page
     if(MyProflieName  == 'Guest User') {
        return new PageReference('/apex/HomePage');
    } else {
       return Network.communitiesLanding();
    }          
      }
  }

1 Answer 1

2

Got it resolved:

  1. Applied the appropriate condition (if else) on URL Rewriter Class and CommunitiesLandingController.

  2. Changed the Active Site Home Page on Community to newly created Visualforce Page 'HomePage'.

  3. Changed the Error Page Page Not Found Page (404) to newly created Visualforce Page 'HomePage'.

  4. Enabled the option for Use custom Visualforce error pages in Communities Administration< Preferences.

The required pages are now redirecting depending upon the profiles.

1
  • Could you share your code to inspire others? I'm very curious what you did and how
    – Nielsm
    Oct 14, 2019 at 9:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .