[SalesForce] How to pass a variable’s value of child visualforce page to a variable in parent visualforce using js

I am new to salesforce and I am doing a task to create a new Company by using visualforce page. From visualforce page, I am using <apex:outputLink> to open a popup window with a url like 'apex/page1'. This page has a standard controller that is something like :

<apex:page standardController="Company__c" showHeader="true" sidebar="true">

By clicking outputLink, we got a popupwindow that is on back-end a visualforce page which has a custom controller (apex class) named apexClass1.

From this custom controller, I am using PageReference to make a reference to a page something like :


This page2 has its own custom controller named as "apexClass2".

Now My problem is that from this apexClass2 class, I want value of a variable to use it into the first page's js portion that is opening a popup window.

How can I do that? Is it possible in salesforce?

Please help.

Now I am posting my code so that you can easily my problem…

<<——–1st Page to create a company——->>

var myOpenWindow = function(URL) {
var myOpenedWindow = myOpenedWindow || window.open(URL,'window.childWin','height=600,width=600,left=100,top=100,scrollbars=yes,toolbar=no,status=no');;
myOpenedWindow.location.href= URL;

            window.intervalId = window.setInterval('window.checkIfChildWindowIsClosed()',1*1000);

            // Here value should retrieve...
             var recID = "How to get Value from 3rd page here"; 

            //add check function to window
            window.checkIfChildWindowIsClosed = function()
                    window.parent.location.href = 'https://na11.salesforce.com/'+recID;



Now <<———- Page 2 ————–>>

Enter Name:

            <apex:column value="{!d.name}" headerValue="Name"/>
            <apex:column headerValue="Select">
                <apex:commandButton action="{!QDetail}" value="Go" rerender="hiddenBlock">
                    <apex:param name="code" value="{!d.Code}" assignTo="{!code}"/>
    <apex:pageBlock id="hiddenBlock" rendered="false"></apex:pageBlock>


<<—— Controller of Page 1 is ——->>

public class QuandlCntl {
public string searchQuery {get; set;}
public string updateQuery {get; set;}
public string qAPIKey {get; set;}

public List<SearchedDocs> listDocs{get;set;}

public String code {
    // *** setter is NOT being called ***
    set {
        code = value;
        //System.debug('value: '+value);

public QuandlCntl(){
QuandleConnect QuandleCon = new QuandleConnect();
    qAPIKey = QuandleCon.QuandleAPIKey;
    searchQuery = ApexPages.CurrentPage().getparameters().get('cname');

// @RemoteAction
public void SearchQuandl(){
httpReq(searchQuery.replace(' ', '+'));
catch(System.Exception e){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Quandle Search Data Failed: please retry'));

public PageReference QDetail(){

    integer underScore = code.indexOf('_');

    code = code.substring(underScore+1);
    string recordId = ApexPages.CurrentPage().getparameters().get('recordId');
    PageReference pr = new PageReference('/apex/QuandleStockDetailsVfp?c=' + code+'&recordId='+recordId);
    return pr;


You can see that from method QDetails, I am referencing to a page that is:

                           <<---- Page 3---->> 

display: inline-block;

            function ClosePopup()
                console.log('Call close popup');

                var recordId = '{!recordId}';  
                var resp = '{!message}';

                if(resp == 'success'){
                    }, 100);

<<———- controller for this page is ————–>>
public class QuandlStockDetailsCntl {

public string recordId{get;set;}
public String message {get;set;}

public QuandlStockDetailsCntl(){
    QuandleConnect QuandleCon = new QuandleConnect();
    qAPIKey = QuandleCon.QuandleAPIKey;
    recordId = queryString('recordId');  // This the variable whose value I need on page 1

public PageReference saveQuandleData(){
System.debug('record Id:'+ recordId);
List MasterListValues = new List();
List Company;
Company__c companyObj;
//if(recordId == null){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Successfully saved data in CRM.'));
// }

    Company = [select Address__c, Annual_Revenue__c, Cash_Equivalents__c from Company__c where Id = :recordId];                        

        Company__c compny = new Company__c();

        insert compny;
        recordId = compny.Id;  // here value of recordId is changed that is the required value in page 1...

        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Successfully saved data in CRM.')); 
        PageReference pr = new PageReference('/apex/QuandleSuccessRecVF');
        return pr;


This is the full scenario. From 3rd apex class, value of variable "recordId" is needed in 1st page…

Any help will be appreciated!

Best Answer

You can use either Window.postMessage to safely transfer data between the popup and the original window; this is the preferred method. Alternatively, you can also set an Apex Cookie that can be shared between the two windows. Using expando properties of window.opener should not be used, because you can't even verify that the receiving page is even the original page which opened the popup; this might cause undesirable behavior.

Related Topic