5

So the question "Know previous page in Winter '18" answers my question as to why my packaged Visualforce sidebar component is now failing to find the window's URL correctly to work out the context of what is being viewed.

Of course, now I need a genuine workaround as telling clients to disabled a security settings Salesforce themselves have added is by and large a non-starter. Would have been nice if there was an alternative level of protection that allowed Visualforce access, but that's clearly not a thing.

So the question in short:

How do you get the window's URL inside the controller for a Visualforce page hosted in the sidebar?

1

Not sure if I have undertood your question correctly, but only for this question "How do you get the window's URL inside the controller for a Visualforce page hosted in the sidebar?" this is the approach to find out.

I had a requirement that during case creation the Parent object details must be shown as sidebar as follows.

To implement this functionality I have created a visualforce with controller and placed that visualforce as sidebar console component.

Sidebar

Here is the tricky part

We cannot get id the parent object in the controller. For this reason, in the visualforce page load I have called actionFunction to pass values to the controller.

Visualforce

<apex:page id="sidebarPage" Controller="SidebarComponentController" showHeader="false" sidebar="false">
    <apex:includeScript value="/support/console/28.0/integration.js" />
    <!-- On window load getEnclosingPrimaryTabId() is getting called to retrieve the tabLink which will be passed to actionFunction-->
    <!-- Function has been written as in controller, getParameters().get('Id') will not work in console -->
    <script type="text/javascript">
        var getEnclosingPrimaryTabId = function getEnclosingPrimaryTabId() {
            sforce.console.getEnclosingPrimaryTabId(getTabLink);
        }
        var getTabLink = function getTabLink(result) {
            sforce.console.getTabLink(sforce.console.TabLink.PARENT_AND_CHILDREN,
                result.id, showTabLink);
        }
        var showTabLink = function showTabLink(result) {
            var link = result.tabLink;
            passLinkFunc(link);
        };
        window.onload = getEnclosingPrimaryTabId;               
    </script>
    <apex:form id="form1">

        <apex:actionFunction name="passLinkFunc" action="{!init}" immediate="true" reRender="form1">
            <apex:param id="URLString" name="URLString" assignTo="{!URLString}" value="" />
        </apex:actionFunction>

        <!--
            All necessary fields to display in the page

        -->
    </apex:form>
</apex:page>

Controller

In the init method url will be captured like this:

https://cs67.salesforce.com/500/e?CF00N0n000000gH4P=test1%2B-%2B3HUA&CF00N0n000000gH4P_lkid=a0l16000006y9q7&ekp=500&ent=Case&nooverride=1&RecordType=0120n0000008dmO&scontrolCaching=1

From that url, we can get the id from Custom object through parsing logic.

public class SidebarComponentController 
{

    public String urlid{get;set;}
    public String URLString {get;set;}

    public SidebarComponentController() {
        System.debug('Inside Constructor=' + ApexPages.currentPage().getParameters());        
    }

    public void init()
    {   
        System.debug('URLString=' + URLString);     
        String url = EncodingUtil.urlDecode(URLString, 'UTF-8');

        System.debug('init:url= ' + url);

        //retrieve CustomObject Id in New mode 
        //urlid= retriveQueryStringValue(url, 'CF00N0n000000gH4P_lkid');

        if(url.contains('CF00N0n000000gH4P_lkid'))  //put this in Custom label
        {
            urlid= retriveQueryStringValue(url, 'CF00N0n000000gH4P_lkid');
        }

        system.debug('***urlid***'+urlid);        
    }


    public String retriveQueryStringValue(String url, String input)
    {
        String returnString = null;

        if(input != null)
        {
            //this will return CustomObject Id
            Integer position = url.indexOf('?');
            if(position != -1)
            {
                String queryString = url.substring(position + 1);
                List<String> parts = queryString.split('&');
                for(String str:parts)
                {
                    if(str.contains(input))
                    {
                        returnString = str.substring(input.length()+1);
                        return returnString;
                    }
                }           
            }
        }        
    }
}

Let me know if it helps!

  • Hey Santanu, it looks as thought that may well do the job. I'll give it a try and see how I go, I already have a tonne of parsing code as I support VF and setup screens as well, be interesting to see if the console API behaves as expected for those. – Matt Lacey Oct 18 '17 at 8:25

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.