Showing a VF in jQuery modal dialog on standard page button click

Showing a VF page as a popup on click of a custom button is fairly easy. But the problem arises when we have to refresh the parent when the user has completed the operation on the VF pop-up page because of the same-origin policy. The browser doesn’t allow any communication across domain/ protocol. But as a workaround we can use the postMessage() to communicate between parent and pop-up window even if they are on different domain, but again IE8 doesn’t support this.

jQuery Modal Dialog to the rescue. In this demo we will place a custom button on the account detail page, clicking on which will pop-up a modal-vf page which will allow the user to update the name of the account.

I will be using 4 components to demo this :

1) A javascript file, stored in Static resource
2) An images zip file in static Resource. Click here to download the images zip file and store it in the static resource by the name images
3) A Custom button
4) An apex class (extension)
5) A VF page (pop-up)

Lets start with the javascript file. Store this in the static resource by the name mainjs

	/*@description: This script will insert a modal div in the standard page.
					the modal will contain a VF page. This file should be located in static resource.*/
	var j$ = jQuery.noConflict();
	var currentUrl = window.location.href;
	var hostIndex = currentUrl.indexOf(window.location.host+'/')+(window.location.host+'/').length;
	var accountId = currentUrl.substring(hostIndex,hostIndex+15); 
	j$(function(){
		/*Insert the jQuery style sheets in the Head.*/
		/*Insert the Modal dialog along with the VF as an iframe inside the div.*/
		j$("head").after(
			j$("<link>",{rel:"stylesheet",
						href:"https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"}));
		j$("body").after(
			j$("<div>",{id:"modalDiv",
					    style:"display:none;"
			   }).append(
				j$("<iframe>",{id:"vfFrame",
							 src:"/apex/popuppage?id="+accountId,
							 height:200,
							 width:550,
							 frameBorder:0})
			   ));
		/*Initialize the Dialog window.*/
		j$("#modalDiv").dialog({
			autoOpen: false,
			height: 210,
			width: 605,
			modal:true
		});
	});

Apex Class :

public class PopUpExtension{
    public Account account{get;set;}

    public PopUpExtension(ApexPages.StandardController stdController){
        this.account = (Account)stdCOntroller.getRecord();
    }
    
    public void saveAccount() {
        try{
            update Account;
        }catch(exception e) {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Occured while updating the account '+e.getMessage());
            ApexPages.addMessage(myMsg);
        }
    }
}

VF page, name it as popuppage:

<apex:page showHeader="false" sidebar="false" standardController="Account" extensions="PopUpExtension">
    <apex:form>
        <apex:pageBlock title="{!Account.Name}" id="pageBlock">
            Update the Account Name:<br/>
            <b>Account Name</b><apex:inputField value="{!Account.Name}"/>
            <apex:commandButton action="{!saveAccount}" value="Save" onComplete="closeAndRefresh();" status="actionStatus" reRender="pageBlock"/>
            <apex:actionStatus id="actionStatus">
                <apex:facet name="start"> 
                    <img src="/img/loading.gif"/>
                </apex:facet>
            </apex:actionStatus>
        </apex:pageBlock>
    </apex:form>
    <script>
        function closeAndRefresh(){
            console.log('clicked on the button');
            window.top.location = '/{!$CurrentPage.parameters.id}';
        }
    </script>
</apex:page>

Finally create a button on the Account and call it jQuery modal. Select
‘Detail Page Button’ as the Display Type ,
‘Execute Javascript’ as the Behavior and
‘onClick JavaScript’ as content source.

Add the below code in the button:

{!REQUIRESCRIPT("https://code.jquery.com/jquery-1.10.2.js")} 
{!REQUIRESCRIPT("https://code.jquery.com/ui/1.10.4/jquery-ui.js")} 
{!REQUIRESCRIPT("/resource/mainjs")}

j$("#modalDiv").dialog('option','title','{!Account.Name}').dialog('open');

For more info on the Dialog check out the doc here

Hope this helps!

3 thoughts on “Showing a VF in jQuery modal dialog on standard page button click”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: