With all of the nice things in the new API, there are still a few annoyances. For instance, the functions for adding values only work on textboxes, meaning you can’t programmatically add and remove elements. You also can’t easily move things around, or flip switches, or do any of the normal things you would do with Javascript.
Poking through the JS files, I did find a few things that should make life easier. Today I had an issue where I had to make a value prompt to allowing users the choice of viewing the report YTD or for the entire year. The cube has a nice hierarchy for that, with the captions yes/no or the all level for everything (yes, I know the cube could have been designed so that the No choice shows the entire year, this is a post about JS not OLAP so shush). The issue is that the users want to select “Everything” or “Until December 11”. The everything choice is fairly easy, it’s a static choice with the display set to “Everything”. The “Until December 11” is more difficult, the caption should change every day. Instead of altering the cube to put in a dynamic caption, I opted to use another static choice (this way I don’t need to go to the cube at all), and change the option text with JS.
In order to do this, I need to find the prompt on the page. The ID for the prompt itself is “PRMT_N19F64920x0B67C9E4RS” but that looks like it will change every time the report is run, how can I guarantee that I will always be able to access that. Keep in mind that I’m incredibly lazy, and don’t want to spend the time wrapping the prompt with my own uniquely identified div (because then I’d have to keep track of all of the div IDs, and make sure to move the HTML items when I move the prompt and it’s just too much of a hassle). Surely there must be an easier way.
It turns out that you can get the ID of the prompt with the Prompt API:
var acme = {}; acme.getControl = function(promptName) { var ocr = cognos.Report.getReport("_THIS_"); return ocr.prompt.getControlByName(promptName); }; alert(acme.getControl('FilterType')._id_);
That would return N19F64920x0B67C9E4RS.
So that means that we can very easily modify the text of the prompt:
<script> var acme = {}; acme.getControl = function(promptName) { var ocr = cognos.Report.getReport("_THIS_"); return ocr.prompt.getControlByName(promptName); }; Date.prototype.getMonthName = function() { var monthNames = [ 'January','February','March','April','May','June','July','August','September','October','November','December']; return monthNames[this.getMonth()]; } var dt = new Date(), dtCaption = 'Until ' + dt.getMonthName() + ' ' + dt.getDate(); document.getElementById('PRMT_' + acme.getControl('FilterType')._id_).getElementsByTagName('OPTION')[1].dv = dtCaption; document.getElementById('PRMT_' + acme.getControl('FilterType')._id_).getElementsByTagName('OPTION')[1].innerHTML = dtCaption; </script>
This will change the display value (dv) and the label (innerHTML) with Until December 11 (or whatever the current date is).
Hi have been your reader since some days :), as a beginner. I have 5 prompts and i want to hide then until user ask for prompts. I am using javascript to hide and display a div, now i want to know how i can put a prompt inside my html or javascript code
Hi Paul
Been looking at scripting on 10 and noticed the debugging seems to have fallen over in 10.
In 8 I could run developer tools using F12 in internet explorer and step through my script without problems – now in 10 it it just says no source code. Major pain. Have you worked out how to debug and step through the code using the debugger?
thanks
I still use the developer tools in IE with Cognos 10. The script tab can be used with the /cognos/cgi-bin/cognos.cgi file to view embedded javascript in the report.
I find that the “Watch” feature is the most useful for discovering the Prompt API methods and properties. Just type in one of your variables from your script that you created using the acme.getControl(‘promptName’) and you can then see all of your options for manipulation.
Piggybacking on this solution, I discovered a way to default a date prompt to be blank instead of a specific date. You use the following code in and HTML Item:
var acme = {};
acme.getControl = function(promptName) {
var oCR = cognos.Report.getReport(“_THIS_”);
return oCR.prompt.getControlByName(promptName);
};
document.getElementById(‘txtDate’ + acme.getControl(‘MyDateFieldName’)._id_).value = ”;
A more specific way to get the object id is to use “.m_oForm.id” which returns the entire id string and not just the part after the “PRMT_”. This was found with Cognos 10.2.1.
example:
document.getElementById(acme.getControl(‘txtIndexDesc’).m_oForm.id).readOnly = true;