function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gautamgautam 

Url rewriting not taking other fields except page name for output url in cmsforce

Hi all,

 

I have written the url rewriter class to construct friendly urls for cmsforce. But i am not able to use some other field except page name to get the final url. I have used a text field url name in page object to get the final url.

 

Please see my code below and let me know if i am missing something !

 

 

global class myurlrewriter implements Site.UrlRewriter {

 //Variables to represent the friendly URLs for pages assuming 3 account managers
 String DIRECTORY = '/internal/';
 

 //Variables to represent my custom Visualforce pages that display page information

 String VISUALFORCE_PAGE = '/page?pageid=';

 // The first global method for mapping external URL to an internal one

 global PageReference mapRequestUrl(PageReference myFriendlyUrl){

    String url = myFriendlyUrl.getUrl();

    if(url.startsWith(DIRECTORY)){

       String name = url.substring(DIRECTORY.length(),url.length());

       //Select the ID of the page that matches the name from the URL

       Page__c site_page = [select id,PageTemplate__r.id  from Page__c where name =:name LIMIT 1];
       
       //Construct a new page reference in the form of my Visualforce page

       return new PageReference(VISUALFORCE_PAGE + site_page.id+'&tempid='+site_page.PageTemplate__r.id);

    }

    //If the URL isn't in the form of a cmsforce page, continue with the request

    return null;

  }

  // The second global method for mapping internal Ids to URLs

  global List<PageReference> generateUrlFor(List<PageReference> mySalesforceUrls){

    //A list of pages to return after all the links have been evaluated

    List<PageReference> myFriendlyUrls = new

    List<PageReference>();

    for(PageReference mySalesforceUrl : mySalesforceUrls){

      //Get the URL of the page

      String url = mySalesforceUrl.getUrl();

      //If this looks like a page that needs to be mapped, transform it

      if(url.startsWith(VISUALFORCE_PAGE)){

        //Extract the ID from the query parameter

        String id= url.substring(VISUALFORCE_PAGE.length(), url.length());

        //Query for the name of the cmsforce page to put in the URL

       Page__c site_page2 = [select url_name__c from Page__c where id =:id LIMIT 1];
        
        
        //Construct the new URL
       
        myFriendlyUrls.add(new PageReference(DIRECTORY+ site_page2.url_name__c));
        
        
     }  

     else {

       //If this doesn't start like an cmsforce page, don't do any transformations

       myFriendlyUrls.add(mySalesforceUrl);

     }

  }

  //Return the full list of pages

  return myFriendlyUrls;

  }

}
Best Answer chosen by Admin (Salesforce Developers) 
gautamgautam

 issue resolved

 

 

 Page__c site_page = [select id,PageTemplate__r.id  from Page__c where url_name__c =:name LIMIT 1];

this line made it work

All Answers

gautamgautam

 issue resolved

 

 

 Page__c site_page = [select id,PageTemplate__r.id  from Page__c where url_name__c =:name LIMIT 1];

this line made it work

This was selected as the best answer
swathiswathi

Hi Gautam,

 

Do we have to create custom object as page__c and we have to store the url name, pagetemplates etc.,

 

I want to know on thing pageTemplate is an custom objects?

 

Please let me know. Thanks in advance