I have a page block table with 2 columns 1st - Application Name and 2nd - Add Contact button

Clicking of Add button opens up a popup to add contact There are 3 fields in pop

First Name, Last Name and Email 2 button Add and Cancel on popup.

Assign -- Adds contact to application (Contact has application as lookup field on it) Cancel -- Just simply closes the popup

Not sure how do i add contact to application?

Following is the code i m trying

public with sharing class TestAddContact {

    public boolean displayPopup {get; set;}

    public List<Id,Application__c> lstApplication{get;set;}

    public TestAddContact(){
        lstApplication = [SELECT Id, Name
                          FROM Application__c 
                          WHERE         Client__c = : account.Id 
                          ORDER BY  CreatedDate Desc]);

    public void closePopup(){        
        displayPopup = false;    

    public void showPopup(){        
        displayPopup = true;
        applicationId = applicationId;
        if(applicationId != null){  


Visualforce page code

<apex:page><style type="text/css">
        background-color: white;
        border-width: 2px;
        border-style: solid;
        z-index: 9999;
        left: 50%;
        position: absolute;
        width: 500px;
        margin-left: -250px;
        opacity: 0.20;
        filter: alpha(opacity = 20);
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        z-index: 9998;

<apex:pageBlockTable value="{!lstApplication}" var="application">
    <apex:column headerValue="Application Name">
        <apex:outputText value="{!application.Name}"  style="font-size:10px;font-weight:bold"/>
    <apex:column headerValue ="Action">
        <apex:commandButton value="delegate" rerender="opDelegateContact" action="{!showPopup}" >
            <apex:param name="applicationId" value="{!application.Id}" assignTo="{!applicationId}" />

<apex:outputPanel id="opContact">
    <apex:outputPanel styleClass="popupBackground" layout="block" rendered="{!displayPopUp}"/>
<apex:outputPanel styleClass="custPopup" layout="block" rendered="{!displayPopUp}">
<apex:pageBlock id="pbContact" >
<table><tr> <td>
<apex:pageBlockSection columns="1">
<apex:outputText value="First Name" />
<apex:inputText value="{!contact.FirstName}"/>
<apex:outputText value="Last Name" />
<apex:inputText value="{!contact.LastName}"/><br/>
<apex:outputText value="Email" /><apex:inputText value="{!contact.Email}"/>
<apex:commandButton value="Cancel" action="{!closePopup}" rerender="opContact"/>
<apex:commandButton value="Add" rerender="opContact" action="{!showPopup}" >
<apex:param name="applicationId" value="{!application.Id}" assignTo="{!applicationId}" />


First i would use apex:actionFunction to open a Contact window and at the same time pass the current application ID in controller (we will need this ID for a new Contact object):

<apex:actionFunction name="open" action="{!showPopup}" reRender="Contract">
    <apex:param name="p1" assignTo="{!currentApplication}" value="">

<apex:pageBlock id="Table">
    <apex:pageBlockTable value="{!applications}" var="application">
        <apex:column value="{!application.name}"/>
        <apex:column headerValue ="Action">
            <apex:commandButton value="Open popup" onclick="open('{!application.id}');"/> 

<apex:outputPanel id="Contract">
    <apex:inputField value="{!con.FirstName}"/>
    <apex:inputField value="{!con.astName}"/>
    <apex:inputField value="{!con.Email}"/>
    <apex:commandButton value="Add" reRender="Table, Contract" action="{!addContact}" >

In controller just paas the current application ID into the lookup field and save a new contact. At te same time we need to rerender two panels "Table" and "Contract" to refresh a new data:

public Contact con { get; set; }
public String currentApplication { get; set; }

public YourClass(){
    con = new Contact();
    currentApplication = '';

public PageReference addContact(){
    // Inserting new contact
    con.application__c = currentApplication;
    insert con;

    // Initialising a new contact object
    con = new Contact();    

    return null;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.