COGNOiSe.com - The IBM Cognos Community

IBM Cognos 10 Platform => Cognos 10 BI => Report Studio => Topic started by: CognosQ on 20 Sep 2017 08:23:10 AM

Title: Pre populate values in Select & search prompt.
Post by: CognosQ on 20 Sep 2017 08:23:10 AM
We have to use Select & search prompt.
Requirement is that the Choices box should Pre populate all the values of the prompt parameter, even before the user enters the search string so that user can have a look on the list and then search accordingly in single select or multiselect.
Does anyone has any java script code to achieve this?


Thanks!
Title: Re: Pre populate values in Select & search prompt.
Post by: CognosPaul on 20 Sep 2017 12:34:59 PM
Which version and how soon do you intend to upgrade?
Title: Re: Pre populate values in Select & search prompt.
Post by: CognosQ on 20 Sep 2017 03:38:28 PM
Version - Cognos 10.2
Upgrade- May be in a year to Cognos Analytics.
Title: Re: Pre populate values in Select & search prompt.
Post by: CognosPaul on 27 Sep 2017 08:39:37 PM
Okay, thing to remember is that any JS used in 10 will not work in interactive mode in Cognos Analytics.

Take a look at the attached report. I don't have C10, but it might work.

The JS is pretty basic:
<script>

var paulScripts ={}
, oCR = cognos.Report.getReport("_THIS_");


paulScripts.getControl = function(promptName) {
  return oCR.prompt.getControlByName(promptName);
};

paulScripts.loadSNSDefaults = function(SNSName,valueName){
  var SNS=paulScripts.getControl(SNSName)._id_
  , val = paulScripts.getControl(valueName)._id_
  , SNSSel = document.getElementById('PRMT_SV_'+SNS)
  , valSel=document.getElementById('PRMT_SV_' + val)
;

  if(sessionStorage['SNSDefault'+SNSName]) return true;

  for(var i=2;i<valSel.length;++i){
    SNSSel.options.add(valSel.options[i].cloneNode(true));
  }

  sessionStorage.setItem('SNSDefault'+SNSName,1)

};


paulScripts.loadSNSDefaults('companies','defaultCompanies');
</script>


You need two prompts, one is your SNS and the other being a value prompt that is loaded with your desired prompt objects. The script finds the actual select elements, and copies the options from the value to the SNS. Notice the loop starts at 2 to skip the prompt name and dashed line. After, you simply call the function with the names of the SNS and Value prompt.

To hide the value prompt, simply wrap them in <div style="display:name"> and </div>
Title: Re: Pre populate values in Select & search prompt.
Post by: CognosQ on 04 Oct 2017 10:38:03 AM
Thank you so much for the reply! Will definitely try.
Title: Re: Pre populate values in Select & search prompt.
Post by: CognosQ on 05 Oct 2017 08:23:51 AM
Hi Paul,

I tried your code, unfortunately it didn't work.
Could you please help me in understanding the code, may be my approach is wrong.

I copied the XML also, that you attached and change the prompt with desired prompt objects Objects - SNS & value prompt , reprompt button shown but it didn't pre-populate any thing.

I gave the name of my prompt :

SNS : companies
Value Prompt - defaultCompanies

Could you please tell me apart for the above name do I need to define anything?
promptName --?? which name it is trying to refer
SNSName?? which name it is referring as we already defined "companies" as our prompt name for SNS
valueName?? which name it is referring as we already defined "default companies" as our prompt name for value prompt
SNSDefault??

I understand the concept of copying value list into SNS but don't understand the code.

Thanks
Title: Re: Pre populate values in Select & search prompt.
Post by: CognosPaul on 09 Oct 2017 08:49:00 AM
The only thing in the script you should change is the function call to loadSNSDefaults.

In the report you should have two prompts. The SNS and a value prompt that contains all of the values you want preloaded. The name of the prompts are what you put into the function. It looks like you're doing that.

The next thing to check, are you running the function before or after the prompt? Try moving the HTML item to after the prompt and see if that works.
Title: Re: Pre populate values in Select & search prompt.
Post by: CognosQ on 11 Oct 2017 07:56:42 AM
Hi,

I am running this script:
SNS Name : - companies
ValueName - default companies

<script>

var paulScripts ={}
, oCR = cognos.Report.getReport("_THIS_");


paulScripts.getControl = function(promptName) {
  return oCR.prompt.getControlByName(promptName);
};

paulScripts.loadSNSDefaults = function(companies,default companies){
  var SNS=paulScripts.getControl(companies)._id_
  , val = paulScripts.getControl(default companies)._id_
  , SNSSel = document.getElementById('PRMT_SV_'+SNS)
  , valSel=document.getElementById('PRMT_SV_' + val)
;

  if(sessionStorage['SNSDefault'+companies]) return true;

  for(var i=2;i<valSel.length;++i){
    SNSSel.options.add(valSel.options.cloneNode(true));
  }

  sessionStorage.setItem('SNSDefault'+companies,1)

};


paulScripts.loadSNSDefaults('companies','default companies');
</script>


********************************************

I have taken objects in this sequence
SNS prompt --- Value prompt---HTML ( Script )
I have highlighted the changes which I am trying. Am i doing anything wrong?


PS: Initially I didn't changed anything on script, just changed the SNS prompt name = 'companies' & Value prompt name = 'default companies'  on my report and copied the script as it is. Now thought to change as above, that also missing something.
Title: Re: Pre populate values in Select & search prompt.
Post by: CognosPaul on 11 Oct 2017 09:00:41 AM
The only place you should change is:

paulScripts.loadSNSDefaults('companies','default companies');


by changing the function definition you're preventing it from working.

Also, can you confirm that this is placed AFTER the SNS and value prompts?
Title: Re: Pre populate values in Select & search prompt.
Post by: CognosQ on 12 Oct 2017 02:25:31 AM
Hi Paul,

Yes, initially I didn't changed anything on script and kept the name of my prompt as given over here.

SNS : Companies
Value Prompt : default companies

Yes, the script is place AFTER SNS & value prompt same as you did in report xml. I just didn't use reprompt button

Sequence used:
SNS Prompt---Value Prompt---HTML item

Is still I am missing something?
Title: Re: Pre populate values in Select & search prompt.
Post by: CognosPaul on 12 Oct 2017 03:53:02 PM
Post your report XML and I'll take a look.
Title: Re: Pre populate values in Select & search prompt.
Post by: CognosQ on 08 Nov 2017 05:23:01 AM
Thanks for your reply. However, we did different approach.
We used TEXT prompt & Value prompt . Whatever the use entered in text prompt will get filtered on Value prompt ( using like or % filter in value prompt query)

Drawback - User needs to enter 2 times submit button.
Once a user submit, records get filtered for value in Value Prompt.
Second time when the user select value from Value prompt , the report get filtered.
Title: Re: Pre populate values in Select & search prompt.
Post by: radid8460 on 21 Apr 2020 10:21:31 AM
Hi Paul, I am able to make your code working but the problem is .. whenever I am searching and clicked on search button the field gets empty and no value comes to the window. could you please help me out..!!! I have attached my XML copy.. thanks 
Title: Re: Pre populate values in Select & search prompt.
Post by: radid8460 on 21 Apr 2020 10:49:38 AM
Hi Paul,

Its works after SNS parameter defined Query and Value... first time I forgot to assign them ... you are awesome and its helping me out of my problem . I was trying your XML to run in my Cognos11 but somehow I am getting error. but hopefully re prompt will help to refresh the value again . could you please tell me how I may refresh the prompt value again..!!! thanks for your help...