[SalesForce] Pass record id from popup vf page to parent vf page

I have this below vf page where on click a popup vf page is opening where i am creating a new account.
I want on save button of the pop up vf page, the newly created account record id should get pass to parent vf page lookup field.

apex class

public class Accpopup2 
{
    public Account a {get;set;}
    public string status {get;set;}
    public Accpopup2()
    {
      a = new account();  
    }
    public pagereference save()
    {
     Account a = New Account(Name = a.Name, Phone = a.Phone);
     insert a;
     return null;
    }

    public pagereference cancel()
    {
        status = 'true';
          return null;    
    }
}

parent vf page –

 <apex:page standardController="Account" recordSetVar="accs">
    <script>
      function OpenVfpage(pid){
          var newwindow = window.open('/apex/Accsmallpopup?id='+pid, 'name=_blank','height=500,width=500,left=250,top=100'); 
      newwindow.focus();
     }
    </script>
        <apex:form >
            <apex:pageBlock >



    ParentAccount : <apex:inputfield value="{!account.ParentAccount__c}" label="ParentAccount"/>

                        <apex:commandButton value="Click" onclick="OpenVfpage('{!account.id}')" reRender="two"/>

            </apex:pageBlock>

        <script>
    function processAccount(accountId){
        console.log('Inside Parent page --> AccountId passed from popup is-->'+accountId);
    }
</script>
        </apex:form>
    </apex:page>

Popup vf page –

<apex:page controller="Accpopup2" showHeader="false" sidebar="false" id="the">
   <apex:form id="page">
    <apex:pageblock id="close">
    <apex:pageBlockSection >
        <apex:inputfield value="{!a.Name}"/>
         <apex:inputfield value="{!a.Phone}"/>
     </apex:pageBlockSection>
        <apex:pageblockbuttons >
        <apex:commandButton value="save" action="{!save}" reRender="dyanamicJs"/>
        <apex:commandButton value="cancel" action="{!cancel}"/>
            </apex:pageblockbuttons>
            <apex:inputHidden id="pass" value="{!status}"/>
             </apex:pageblock>

            <script language="JavaScript" type="text/javascript">
            if(document.getElementById('the:page:close:pass').getAttribute('value') == 'true')
                {
                    window.top.close();
                }
            </script>   

        <apex:outputPanel id="dyanamicJs"> 

window.opener.processAccount('{!a.Id}');

Best Answer

You can pass parameters from popup to parent page. Consider below example which will give idea about how you can pass values from popup to parent page.

Parent Page code

<apex:page >

       <input type="submit" value="Submit" 
              onclick="window.open('/apex/PopupPage', 'name=_blank','height=500,width=500,left=250,top=100'); return false;"/>
    <script>
        function processAccount(accountId){
            console.log('Inside Parent page --> AccountId passed from popup is-->'+accountId);
        }
    </script>
</apex:page>

Popup Page Code

<apex:page >
    <input type="submit" value="Pass Value" 
              onclick="window.opener.processAccount('THIS_IS_ACCOUNT_ID');"/>
</apex:page>

Output enter image description here

As you can see when the button from popup is clicked, console of parent page is showing the value of parameter passed.

In your case lets say your controller account variable name is theAccount. So you can write in popup page.

<apex:outputPanel id="dyanamicJs"> 
   <script> window.opener.processAccount('{!theAccount.Id}'); 
   <script> 
</apex:outputPanel> 

Then you can reRender dynamicJs output panel on save of account

Related Topic