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”.
 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();
    System.debug('******************************'+url);
    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
       Contact site_page = [select id from Contact where name=:name LIMIT 1];
       System.debug('******************************'+site_page.id);
       //Construct a new page reference in the form of my Visualforce page
       return new PageReference(VISUALFORCE_PAGE+ site_page.id);
    }
    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());
        Contact site_page2 = [select name from Contact where id =:id LIMIT 1];
        //Construct the new URL
        myFriendlyUrls.add(new PageReference(DIRECTORY + site_page2.name));
     } 
     else
     {
       myFriendlyUrls.add(mySalesforceUrl);
     }
  }
  //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:pageBlock>
<apex:detail />
</apex:form>
</apex:page>



Contact_Page VF Code:


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

4 comments:

  1. 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?
    Reagrds
    Salesforce training in Chennai|Salesforce training institute in Chennai

    ReplyDelete
  2. 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

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

    ReplyDelete