Find your content:

Search form

You are here

generateURLfor method in urlrewriter? how will it be invoked?

 
Share

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 :(



Attribution to: Sathya

Possible Suggestion/Solution #1

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


Attribution to: Sathya

Possible Suggestion/Solution #2

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.


Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3996

My Block Status

My Block Content