Search code examples
salesforceapex-codevisualforce

How to open a VF page as popup and pass parameters to that VF page?


I have a VF page which has 2 date fields and search button.

On click of search i get some results. i also have another button to print those results which is nothing but a new VF page rendered as pdf.

I want to pass this date values into the printVF page. i also want to have this page come up as popup.

I have the pageref with parameters set. Since i need to open this page as popup i need to find a way to use the pageref in the window.open.

Anybody has ideas on how to accomplish this?

Thanks

Prady

EDIT :What i did was build the url in the controller and use the controller variable in window.open. Something very strange is happening... The first time search results are displayed and i click the print button, the dates dont get populated in the string, they default to todays date.. if i again click the print button the dates are populated correctly on the url from the dateinput.

      public Class1(){
      System.debug('inside constructor');
       fieldContainer=new DummyTable__c(); // it contains date fields for date inputs
      date todays=date.today();

     If (fieldContainer.Start_Date__c== null)
     {
        startdate1=todays;
     }else
     {
     startdate1=fieldContainer.Start_Date__c;
     }

     If (fieldContainer.End_Date__c== null)
     {
        enddate1=todays;
     }
     else
     {
     enddate1=fieldContainer.End_Date__c;
     }
     }

     public void search()
{

    startdate1=fieldContainer.Start_Date__c;
    system.debug('startdate1'+startdate1);


    enddate1=fieldContainer.End_Date__c;
    system.debug('enddate1'+enddate1);
    system.debug('inside search()....after clicking search button');
    system.debug('startdate1'+startdate1);
    system.debug('url'+url);
    LoadData();
}

    public string geturl()
{
    url='apex/VF1?Pstartdate='+string.valueof(startdate1)+'&Penddate='+string.valueof(enddate1);
    return url;
}

public string geturlRec()
{
    urlRec='apex/VF2?Pstartdate='+string.valueof(startdate1)+'&Penddate='+string.valueof(enddate1);
    return urlRec;
}    

VF Page

   <script  language="javascript" type="text/javascript">
  function printOut()
  {

  window.open("{!url}");

      }
  function printIn()
  {
  window.showModalDialog("{!urlRec}","dialogWidth:800px; dialogHeight:200px; center:yes");

   }
   </script>


    <div style="width:900px;margin:0 auto;" id="pagediv">
     <span style="padding-left:30px;padding-right:10px">From </span><apex:inputfield value="{!fieldContainer.Start_Date__c}" id="startdt" style="padding-left:5px;padding-right:20px;"/>
   <span style="padding-left:30px;padding-right:10px">To </span><apex:inputfield value="{!fieldContainer.End_Date__c}" id="enddt" style="padding-left:5px;padding-right:20px;"/>

     <span style="padding-left:30px;padding-right:10px"><apex:commandButton action="{!search}" value="Search" ReRender="dtshipdep,dtshiprec,shippageblock">       </apex:commandButton></span>

     <apex:commandButton action="{!saveDeparted}" value="Update " ReRender="dtshipdep"></apex:commandButton>
    <apex:commandButton value="Print "  onclick="printOut();"></apex:commandButton>

Solution

  • The salesforce recommended way to generate URLs is through URLFOR() function. Or, alternatively, you can create a PageReference in the extension/controller code.

    Though, keep in mind, before any form data can become part of the URL it has to be posted to the server and come back in newly generated pageReference, for that reason you cannot jut click on it in the first run.

    One way I used to solve this server side is using this code on VF

    <apex:outputBlock rendered="{!openPopup}">
       <script>
          window.open('{!myTargetUrl}');
       <script>
    </apex:outputBlock>
    

    openPopup is a bool that control whether this segment gets rendered (transient, you set it to true when you click on open button). myTarget is a PageReference based URL (alternatively you can use URLFOR here to generate URL in VF). If you have any more questions, ask.

    Another option is to have your javascript code in VF which will pickup button click event and build the URL client side and open it up. You can use $Component to locate form fields.