[SalesForce] How to pass variable from Button to popup – close the popup and refresh the opener

I am trying to pass an oppty id from a button (on the oppty page) through an apex wrapper of a flow, to another apex page (pop up that closes itself, and should refresh the opener page).

Main problem is that I can't get to refresh the opener page (the opportunity page with the button. parent that called the popup) (everything else works)

To make it a bit more clear: I am able to refresh the page using 'href' to any URL, but I want to either reload the same page, or simply: 'href' to the opptyid (that will have the same effect)

FIRST TRY (The simple method)

I created the wrapper page called: WrapperAddLicenseToOppty :

<apex:page sidebar="false" showHeader="false">
<flow:interview name="Add_Licenses_to_Opportunity" finishLocation="CloseWin"/>
</apex:page>

The page Add_Licenses_to_Opportunity is a flow. Once completed, I want the pop up window to close, and refresh the opener.

The button on the oppty page is that calls the wrapper:

enter image description here

On the CloseWin page (one that should close current page, and refresh parent)

<apex:page >
<script language="JavaScript">
window.close();
window.opener.location.href="/{!$opptyId}";
</script>
</apex:page>

I also tried with:

  • window.opener.location.reload(true);
  • window.top.location.reload(true);

    • If I can get the opptyid, that will work. (that's what I am trying to do) – that will refresh the oppty page.

SECOND TRY

When I couldn't, I created an apex class, to get the oppty id from the URL of the button, and to add that to the URL for the CloseWin page

public class getOpptyId{
    Public String varInputOpptyID = System.currentPagereference().getParameters().get('varInputOpptyID');  }

New wrapper page

<apex:page sidebar="false" showHeader="false">
<flow:interview name="Add_Licenses_to_Opportunity" finishLocation="{!URLFOR('/apex/CloseWin')}"/>
</apex:page>

THIRD TRY (more of a hail merry attempt)

That didn't work as well, so next wrapper page looks like this:

<apex:page controller="getOpptyId" sidebar="false" showHeader="false">
<flow:interview name="Add_Licenses_to_Opportunity" finishLocation="{!varInputOpptyID}"/>
</apex:page>

And the button link looks like this:

/apex/WrapperAddLicenseToOppty?varInputOpptyID={!Opportunity.Id18__c}

and the CloseWin page looks like this:

<apex:page controller="getOpptyId" sidebar="false" showHeader="false">
<flow:interview name="Add_Licenses_to_Opportunity" finishLocation="{!varInputOpptyID}"/>
</apex:page>

I am positive, I have the simplest error somewhere, as I don't believe this should be so difficult, but like always, I can't find it.

So, any way to succeed in this will assist. (any method)

Best Answer

You can open the window via your button JS code and then continously check within javascript (button code) if the popup window has closed or not. If it has closed, refresh the page

var win = window.open("/apex/WrapperAddLicenseToOppty?varInputOpptyID={!Opportunity.Id18__c}", "_blank", "width=400, height=400, top=200, left=400"); 
var pollTimer = window.setInterval(function() {
    if (win.closed !== false) { // !== is required for compatibility with Opera
        window.clearInterval(pollTimer);
        window.location.href = window.location.href;
    }
}, 200);

For more details refer:- https://stackoverflow.com/questions/3291712/is-it-possible-to-open-a-popup-with-javascript-and-then-detect-when-the-user-clo

Related Topic