JavaScript stopped working in Cognos portal

Today I got a call from a friend. Shortly before going to production it was noticed that a report that works flawlessly from Report Studio, and from clicking on the report link in the portal, doesn’t work in the Cognos Portal.

The report relies on JavaScript to alter the prompts. When he runs the report from RS, everything is beautiful. Prompt headers are removed, default options are set, and everything behaves as expected. However, once the report was placedin a Cognos Viewer portlet in the portal an “Object Expected” error would be thrown every time the report was run.

It turns out there are several significant differences between the Report Viewer (from RS) and the Cogons Viewer (what’s used in the portlets). I cordially invite (okay, I’m begging. I really don’t want to go through a few thousand lines of JS.) any actual Cognos Devs to explain the differences between RV and CV.

An example of the original code:

<script language="javascript">
var f = getFormWarpRequest();
var list = f._oLstChoicesPrompt_Years;

list.remove(1);
list.remove(0);
list.removeAttribute("hasLabel");

canSubmitPrompt();
</script>

It removes the first two rows from the prompt (prompt label and the —), then removes the “hasLabel” attribute (which prevents those two rows values from being selected).

The corrected code is:

<script language="javascript">
var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]);
if ( !fW || fW == undefined)
   { fW = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );}

var list = fW._oLstChoicesPrompt_Years;

list.remove(1);
list.remove(0);
list.removeAttribute("hasLabel");

canSubmitPrompt();
</script>

It was a very simple adaptation of the code found at IBM here.

This method will work in all versions from 8.3 and above (at least until they change the engine again).

Advertisements

9 Responses to JavaScript stopped working in Cognos portal

  1. atul says:

    Hi,
    I am using the same code but it is not working .

    ———–Please find code below:……………————–

    function AS()
    {
    var fW1 = (typeof getFormWarpRequest == “function” ? getFormWarpRequest() : document.forms[“formWarpRequest”]);
    if ( !fW1 || fW1 == undefined)
    { fW1 = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );}
    // fW1._oLstChoicesMyFieldName.selectedIndex = 0;
    var preFix = “”; if (fW1.elements[“cv.id”])
    { preFix = fW1.elements[“cv.id”].value; }

    var fW1 = getFormWarpRequest();
    var dropDownL3 = fW1._oLstChoicesXYZ1;
    var d = new Date();
    //var dropDownA = fW1._oLstChoicesA;
    //dropDownA .value=”1″;
    var x =d.getDate();
    if(x>7)
    {
    dropDownL3.value=”0″;
    }
    else
    {
    dropDownL3.value=”1″;

    }
    setTimeout(“promptButtonFinish();”,0);
    }

    function displayWeek()
    {
    if (getFormWarpRequest()._oLstChoicesA.value ==”0″)
    {
    AS();
    }
    }

    Please help.
    Regards

    • cognospaul says:

      Hi Atul,

      You’re defining fw1 correctly, but immediately afterwords you’re redefining it back to getFormWarpRequest().

      My suggestion is create an HTML item with this script:

      <script language="javascript">
      var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]);
      if ( !fW || fW == undefined)
         { fW = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );}
      </script>
      

      Then your remaining scripts could look like:

      function AS()
      {
      var dropDownL3 = fW._oLstChoicesXYZ1;
      var d = new Date();
      //var dropDownA = fW._oLstChoicesA;
      //dropDownA .value="1";
      var x =d.getDate();
      if(x>7)
      {
      dropDownL3.value="0";
      }
      else
      {
      dropDownL3.value="1";
      
      }
      setTimeout("promptButtonFinish();",0);
      }
      
      function displayWeek()
      {
      if (fW._oLstChoicesA.value =="0")
      {
      AS();
      }
      } 
      
  2. Olga says:

    Hi Paul,

    I have used your modified script to remove first 2 lines in the value prompt. It works great in IE, but it stops working in FireFox (running in Cognos Viewer portlet) after I make a selection.
    The value prompt is on the report page with AutoSubmit set to Yes. The first time I open the portal page, the first 2 lines don’t show up as expected. The minute I select a value from the prompt and report re-runs, the first 2 lines are comming back. I have tested in FireFox 3.6 and 5.0.
    Here is the script I have used:
    —————————————————————————————-

    var fW = (typeof getFormWarpRequest == “function” ? getFormWarpRequest() : document.forms[“formWarpRequest”]);
    if ( !fW || fW == undefined) {
    fW = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );}
    var list = fW._oLstChoicesP2;
    list.remove(1);
    list.remove(0);
    list.removeAttribute(“hasLabel”);
    canSubmitPrompt();

    ——————————————————————————————

    • cognospaul says:

      Hi Olga,

      I just create a quick report to test this, but it still works fine on my end with Firefox 3.6. Do you have any JavaScript anywhere else on the page? My guess is the JavaScript interpreter is failing somewhere.

      Install the Firebug add-on and run the page with Firebug open to the Scripts tab. If there is a JavaScript error anywhere on the page you’ll see exactly what it is.

      • kamal says:

        Hi Paul,

        I have used the script provided above. I have a multi page dashboard board with 11 reports in two folders
        After modifying the script it works fine for the first report and in remaining reports its not working..Please suggest

        • CognosPaul says:

          Hi Kamal,

          Make sure you give each element in the reports unique ids. Multi page dashboards are essentially a single HTML page, so getElementById will only reference the first existing element with any given ID.

  3. Irfan Serkhel says:

    Hi Paul,
    I have used the similar code you have given above, it works fine in IE for both RS and portals, but in FF it does not work for portal.
    Code:

    var fW =(typeof getFormWarpRequest == “function” ? getFormWarpRequest() : document.getElementById(‘formWarpRequest’));
    if ( !fW || fW == undefined)
    { fW = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );}

    var choices1= fW._oLstChoicesPrompt1;
    var choices1_Absolute_Percent = fW._oLstChoicesPrompt_Absolute_Percent;
    check(choices1,choices1_Absolute_Percent);

    choices1.onchange=check;

    function check(choices1,choices1_Absolute_Percent )
    {
    var idx=choices1.options.selectedIndex;
    if ( choices1.options[idx].value == ‘Married’ || choices1.options[idx].value == ‘Has Children’ )
    choices1_Absolute_Percent.disabled=true;
    else
    choices1_Absolute_Percent.disabled=false;
    }
    check_AP();
    choices1_Absolute_Percent.onchange = check_AP;
    function check_AP( )
    {
    var idx_AP=choices1_Absolute_Percent.options.selectedIndex;
    if ( choices1_Absolute_Percent.options[idx_AP].value == ‘Percent’)
    choices1.disabled=true;
    else
    choices1.disabled=false;
    }

    Error on FF is “Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.”
    its on line : “if ( !fW || fW == undefined)
    { fW = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );} ”

    Thanks
    Irfan

  4. Gaurav says:

    Hi Paul,

    I have a scenario where I want to conditionally display/hide the subtotals in a list;based on comma separated input (CSV) in textbox prompt.

    For e.g. we have a list with 3 columns: Prod Line code (Grouped), Prod type and Revenue (Summary/sub-total @ Prod Line Code) and a textbox prompt for CSV input. Whatever Product Line Code(s), I input in this prompt in CSV format, only for those PLCs, we should show the subtotals (list row) and rest should be hidden.

    The approach that I have taken is validating the CSV input through Javascript and pass it to a hidden value-prompt where all PLCs pre-populated and can be multi-selected. Only the common values to Textbox and Value prompts are selected upon a button-click.

    Then using a boolean variable (where there is condition ) as style-variable, I set the box-type=None for list-subtotal-row. But I’m having two problems here:

    1. The boolean condition considers only single PLC value so one subtotal is shown ignoring the rest
    2. The page gets submitted only once (upon calling promptFinishButton()) but not subsequent button-clicks. I’m using HTML Input Type and not Cognos’ standard prompt button

    Can you please assist or suggest another approach?

  5. Paul,

    Thank you. I was going crazy with this one trying to limit the number of entries in a multiselect text box prompt. I was trying to go for an if != “undefined” oCV, oCV_NS_ and oCVRS in the finish button, but then I was getting an object expected error on thegetFormWarpRequest. This makes it much easier.

    Thanks again
    -dan.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: