1

Thanks to Marty C., I am able to create links in VF (with the JS defined in the page) that work in the Service Console (okay - still working on mailto:), and I want to move them to a Static Resource. The function(s) are working to a point (opening new subtabs), but the URLs return 'URL No Longer Exists'.

I am using the following code to generate the URLs:

var url = "{!URLFOR($Action.Case.View, '500')}";

/* replace the Id with the Case.Id passed in to the function */
url = url.replace("500", caseId);

Generating the URL as a var defined on the page and passing it to the function also fails. Here's the function as defined in the Static Resource 'SvcConsoleJSLib' and the related VF code:

  function caseSubtabView(caseId, caseNumber) {

    sforce.console.getEnclosingPrimaryTabId(function(response) {
            var primaryTabId = response.id;

            /* create a Case detail url (with a default value of 500 (Case prefix)) */
            var url = "{!URLFOR($Action.Case.View, '500')}";

            /* replace the Id with the Case.Id passed in to the function */
            url = url.replace("500", caseId);

      /* Define other parameters for openSubtab() */
      var isActive = true;
      var tabLabel = caseNumber;
      var subTabId = null;

      // Call the standard openSubtab method with the current Case's info
      sforce.console.openSubtab(
        primaryTabId, url, isActive, tabLabel, subTabId);

        });     
}

Visualforce:

    <script src="/support/console/33.0/integration.js" type="text/javascript" />    
<apex:includeScript value="{!$Resource.SvcConsoleJSLib}" />

Link (historyItem = one of a list of Cases returned by the Controller):

<apex:outputLink style="small" value="/{!historyItem.Id}" onclick="caseSubtabView('{!historyItem.Id}','{!historyItem.caseNumber}')">view</apex:outputLink>/
2

Merge fields will not work in a Static Resource. You will have to keep all your merged information on your Page and namespace it in.

<apex:page>
    <script>
        (function (w) {
            "use strict";
            w.MyNamespace = w.MyNamespace || {};
            w.MyNamespace.caseView = "{!URLFOR($Action.Case.View, '500')}";
            // perform any other merges here
        })(window);
    </script>
</apex:page>

And then in your Static Resource:

// wrap your whole resource in a closure
(function (w, $) {
    "use strict";
    // check if your namespace is already defined on the window
    w.MyNamespace = w.MyNamespace || {};
    // add to your namespace
    w.MyNamespace.someFunction1 = function () {
        // check if a property is defined on the namespace
        if (this.caseView) {
            // do stuff
        }
    }
    // optionally freeze object when the page is loaded
    $(function () {
        Object.freeze(w.MyNamespace);
    });
    // everything else
})(window, jQuery);
  • Haven't worked with namespaces yet, so please bear with me. In the 'Static Resource', does '(function (window) { ...' wrap the entire Resource (all the existing functions, each individual function to which it applies, or does it go inside the existing function(s)? For instance, inside existing functions would look like this: function caseSubtabView(caseId, caseNumber) { (function (window) { "use strict"; etc.? – Duncan Stewart Mar 20 '15 at 18:19
  • 1 - (function (window) { "use strict"; w.namespaceName = ... then keep the existing functions as is, referencing the w.namespaceName.varName, 2 - Each function that required the variable would = (function (window) { "use strict"; ... function openCaseSubtab( etc., or 3 - function openCaseSubtab(caseId, caseNumber) { (function (window) { "use strict"; – Duncan Stewart Mar 20 '15 at 18:31
  • You should wrap the whole resource in a closure so your state doesn't leak, except what you explicitly add to the window. I updated my answer with some comments. – Adrian Larson Mar 21 '15 at 2:35

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.