Monday, February 6, 2012

URL Rewriting - Salesforce

We need two Visualforce pages for URL Rewriting.
1)      Home
2)      Contact_Page
               On the Site Edit page, choose a Home Visualforce page for Active Site Home Page.
                Incoming URL requests can only be mapped to Visualforce pages associated with your site. We can't map to standard pages, images, or other entities. To rewrite URLs for links on your site's pages, we have used the !URLFOR function with the $Page merge variable.
Home VF Page:
                This is the Visualforce page created for the test site. Create an output link in the “home” page with label “Click me”. When we click on the link it will be redirected to the second Visualforce page that is “Contact_Page”.
                While redirecting we can observe that the URL is rewrited. Usually In the URL we should see the id of the contact, But the Apex class developed for mapping the standard URL with user friendly URLs will map the id to its corresponding contact name.
Apex Class: URL Rewriter Class

global class URLRewriterClass implements Site.UrlRewriter
 //Variable to represent the friendly URLs for pages
 String DIRECTORY = '/Contact_Page/';
 //Variable to represent my custom Visualforce pages that display page information
 String VISUALFORCE_PAGE ='/Contact_Page?id=';
 // The first global method for mapping external URL to an internal one
 global PageReference mapRequestUrl(PageReference myFriendlyUrl)
    String url = myFriendlyUrl.getUrl();
       String name = url.substring(DIRECTORY.length(),url.length());
       //Select the ID of the page that matches the name from the URL
       Contact site_page = [select id from Contact where name=:name LIMIT 1];
       //Construct a new page reference in the form of my Visualforce page
       return new PageReference(VISUALFORCE_PAGE+;
    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
        //Extract the ID from the query parameter
        String id= url.substring(VISUALFORCE_PAGE.length(), url.length());
        Contact site_page2 = [select name from Contact where id =:id LIMIT 1];
        //Construct the new URL
        myFriendlyUrls.add(new PageReference(DIRECTORY +;
  //Return the full list of pages
  return myFriendlyUrls;
                The “generateUrlFor” method in the apex class that queries name from the id which is there in the standard URL query string and appends the name to the user friendly URL.
                The “mapRequestUrl” method in the apex class that queries the id from the name which is there in the user friendly URL and maps it to the standard URL.
After creating the URL rewriting Apex class, Add the same to the site.
Below are the steps to add the Apex class to the site:
1.       Click Your Name | Setup | Develop | Sites.
2.       Click New or click Edit for an existing site.
3.       On the Site Edit page, choose an Apex class for URL Rewriter Class.
4.       Click Save.

Home Page VF Code: 

<apex:page standardController="Account">
<apex:form >
<apex:pageBlock >
<apex:outputlink value="{!URLFOR($Page.Contact_Page,null,[id='00390000009IBJw'])}">Click Me</apex:outputlink>
<apex:detail />

Contact_Page VF Code:

<apex:page standardController="contact"> 
    <apex:detail relatedList="false"/>


  1. Can I create dynamic objects? Means Need to create an object with fields in run time.Is that possible?
    Salesforce training in Chennnai

    1. Yes, I guess you would need to use metadata API.

    2. Machine Learning Projects for Final Year machine learning projects for final year

      Deep Learning Projects assist final year students with improving your applied Deep Learning skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include Deep Learning projects for final year into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Deep Learning Projects for Final Year even arrange a more significant compensation.

      Python Training in Chennai Python Training in Chennai Angular Training

  2. I want to know some basic ideas in sales force. Suppose I have a requirement to create a custom application which is a CRM system for school management using SFDC. My Doubts are, how can I create the custom application?
    Salesforce training in Chennai|Salesforce training institute in Chennai

  3. Salesforce is the cloud based tool which is used by most of the professional who do CRM. Salesforce is the cost effective tool.
    salesforce training in chennai|salesforce training institute in chennai|salesforce training institutes in chennai|salesforce course in chennai