4

I was trying to implement the urlrewriter as mentioned in the below url

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_site_urlRewriter.htm

want to know how this generateURLfor method will be invoked?

I have a mentioned a List<PageReference> as a parameter to this, I dont see this function called anywhere in debug logs for any of my sites user or site guest user.

Not sure how this piece is invoked and am getting a too many script statements error. Not sure why?

But if I remove this URLRewriter part, am not getting that error.

Can someone help here please ? I have posted my code below.

    String FRIENDLY_GROUPS_PAGE = '/groups/';
    Map<Id, Group__c> mapgroups;
    String GROUPDETAILS_VISUALFORCE_PAGE = '/GroupDetails?groupid=';

    global List<PageReference> generateUrlFor(List<PageReference> mySalesforceUrls)
    {
        system.debug('inside generateurlfor');
        //A list of pages to return after all the links  
        //have been evaluated 
        if (mySalesforceUrls.size() > 0 && mySalesforceUrls.get(0).getUrl().startsWith(GROUPDETAILS_VISUALFORCE_PAGE) && !mySalesforceUrls.get(0).getUrl().contains('&'))
        {
            List<PageReference> myFriendlyUrls = new List<PageReference>();

            //a list of all the ids in the urls 
            List<id> grpIds = new List<id>();

            // loop through all the urls once, finding all the valid ids 
            for(PageReference mySalesforceUrl : mySalesforceUrls)
            {
                //Get the URL of the page     
                String url = mySalesforceUrl.getUrl();        
                if(url.startsWith(GROUPDETAILS_VISUALFORCE_PAGE) && !url.contains('&'))
                {
                    //Extract the ID from the query parameter 
                    //and store in a list 
                    //for querying later in bulk. 
                    string id= url.substring(GROUPDETAILS_VISUALFORCE_PAGE.length(),url.length());
                    grpIds.add(id);
                }
            }

            // Get all the group names in bulk 

            List<Group__c> groups = [SELECT Name FROM Group__c WHERE Id IN :grpIds];

            // make the new urls 

            Integer counter = 0;

            // it is important to go through all the urls again, so that the order         
            // of the urls in the list is maintained.  
            if (mySalesforceUrls != null)
            {
                system.debug(LOGGINGLEVEL.WARN,'count of mySalesforceUrls : ' + mySalesforceUrls.size());
            }
            for(PageReference mySalesforceUrl : mySalesforceUrls) 
            {
                //Get the URL of the page 
                String url = mySalesforceUrl.getUrl();

                if(url.startsWith(GROUPDETAILS_VISUALFORCE_PAGE)  && !url.contains('&') )
                {
                    myFriendlyUrls.add(new PageReference(FRIENDLY_GROUPS_PAGE + groups.get(counter).name));
                    counter++;
                } 
                else 
                {
                    //If this doesn't start like a groups page,     
                    //don't do any transformations     
                    myFriendlyUrls.add(mySalesforceUrl);
                }
            }
            //Return the full list of pages 
            return myFriendlyUrls;
        }
        else
        {
            return mySalesforceUrls;
        }
    }

** edited and added the following ******* now the issue is even worser.. I have commented out everything inside these methods and just said return null. despite doing that its throwing too many script statements error..

But strange thing is that, if I remove this URLRewriter class configuration from my sites config, its not throwing any error and its working fine :(


3

Finally I found the answer guys..

  1. I dont need to use generateURLfor at all.. this method is for reverse mapping from id to friendly url.

  2. I had used $currentpage.parameters.paramname in my visualforce page in the rendered attribute of an apex:column inside an apex:datatable and the list that was bound to the datatable had a lot of records and due to that urlrewriter was getting called everytime and i hit the script statement limits. I removed that $currentpage.. and replaced it with a property from my controller and everything worked fine...

1

Are you exposing this page on a Force.com Site ?

The implementation pattern requires for your URL Rewriter to implement the Interface Site.UrlRewriter

My understanding of it is its use in serving up Visualforce Pages on a Force.com site

So you could map a friendly URL name and use your URL Rewriter to translate it into a concrete Salesforce URL.

"To rewrite URLs for links on your site's pages, use the !URLFOR function with the $Page merge variable."

So it provides a sort of a phone book for you to resolve user friendly URL's into actual Salesforce URL's

Salesforce invokes the method internally when a Page Request is made to resolve the friendly URL to the actual URL. It does not need to be explicitly invoked.

  • oh ok but why am I hitting script statements limit when the code hits urlrewriter? If I remove that everything works fine? Moreover I am just returning the same urls passed as arguments to this generateurlfor method if my url is not of the specified pattern and my case in which the error is thrown is not of this pattern at all. so it should just return the URL that is passed. :( Not sure whats wrong?! – Sathya Nov 2 '12 at 16:25
  • 1
    salesforce.stackexchange.com/questions/2134/… you may like to refer this question as well. – Mohith Shrivastava Nov 2 '12 at 16:30
  • So you've declared this method in a global class that implements the implements Site.UrlRewriter interface ? Do you have a need to rewrite URL's? – techtrekker Nov 2 '12 at 16:51
  • yes I have the need to rewrite URLs, from my sites say mycompany.force.com/mysite/groups/200 should redirect to the page mycompany.force.com/mysite/groupdetail?id=sadjfhsfadkh (Some sf id corresponding to that group record 200) – Sathya Nov 3 '12 at 9:23
  • hi @MohithKumar i looked at it and it didnt work out.. – Sathya Nov 3 '12 at 10:28

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.