function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pallavi ShendePallavi Shende 

Need to get account name when I create a contact on visualforce page

Hi Experts,
I need a account name against which I create contact on visualforce page. Visualforce page is list view. When I
<apex:page controller="TestPopup">
<apex:form >
<apex:pageBlock title="All Accounts List">
<!--Account list-->
<apex:pageblockTable value="{!acc}" var="account1">
<apex:column value="{! account1.Name}"/> 
<apex:column value="{! account1.AccountNumber}"/> 
<apex:column > 
<apex:commandButton value="Create Contact" action="{!showPopup}" rerender="popup"/>
</apex:column>
</apex:pageblockTable>
</apex:pageBlock>
<apex:pageBlock >
<apex:outputPanel id="popup">
<apex:outputPanel id="popupblock" layout="block" styleClass="customPopup" rendered="{!displayPopup}">
<apex:commandButton value="Close" title="Close the popup" action="{!closePopup}" styleClass="closeButton" rerender="popup"> </apex:commandbutton>
<apex:pageBlockSection title="Contact Form">
<apex:pageBlockSectionItem >
<apex:outputLabel >Contact Name</apex:outputLabel>
<apex:inputText value="{!Contactname}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton value="Save Contact" action="{!save}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
<style type="text/css">
.customPopup {
    background-color: white;
    border-style: solid;
    border-width: 2px;
    left: 20%;
    padding: 10px;
    position: absolute;
    z-index: 9999;
    /* These are the 3 css properties you will need to tweak so the pop 
                            up displays in the center of the screen. First set the width. Then set 
                            margin-left to negative half of what the width is. You can also add 
                            the height property for a fixed size pop up.*/
    width: 500px;
    top: 20%;
}

.disabledTextBox {
    background-color: white;
    border: 1px solid;
    color: black;
    cursor: default;
    width: 90px;
    display: table;
    padding: 2px 1px;
    text-align:right;
}   

.closeButton {
    float: right;
}
</style>
</apex:page>
 
public class TestPopup{
    Account account=new Account();
    public list<Account> acc{get;set;} 
    ApexPages.StandardSetController setCon{get;set;}
    public TestPopup(){
           acc = [select ID, Name, AccountNumber,Type from Account];
        	
    }
    Id id = ApexPages.currentPage().getParameters().get('id');
    public id aaccountid;

    public list<Account> getacc(){
        return acc;
    }
    
    public Boolean displayPopup {get;set;}
    
    public void showPopup()
    { 
    displayPopup = true;
    }
       
    public void closePopup() {
        displayPopup = false;
        
    }
    
    public Id posId {get;set;}

    public string Contactname {get;set;}

    public PageReference save(){
        Contact con= new Contact();
        con.LastName = Contactname;
        con.AccountId= id;
        insert con;
        ID contactId =con.Id;
        PageReference pr = new PageReference('/' + contactId);
   		return pr;
    }  
        
}

click create contact a popup opens with input field Contact Name. When click on save, it is navigate to the contact I created. I am getting the contact saved but not the account name to it. User-added imageUser-added image
Best Answer chosen by Pallavi Shende
Khan AnasKhan Anas (Salesforce Developers) 
Hi Pallavi,

I trust you are doing very well.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="AccountContactPopupC" tabStyle="Account" >
    
    <apex:form id="frm">
        <div id="formid">
            <apex:outputPanel id="tstpopup">
                <apex:outputPanel styleClass="popupBackground" layout="block" rendered="{!displayPopUp}" />
                <apex:outputPanel styleClass="custPopup" layout="block" rendered="{!displayPopUp}" >
                    <apex:pageBlock title="My Content" mode="edit">
                        
                        <apex:pageBlockSection title="My Content Section" columns="1">
                            <apex:outputField value="{!con.Name}"/>                            
                            <apex:inputText value="{!contact.firstName}"/>                            
                            <apex:inputText value="{!contact.lastName}"/>                                 
                        </apex:pageBlockSection>
                        <apex:pageBlockButtons >
                            <apex:commandButton value="Save" action="{!updateContact}" >
                                <apex:param name="con1" assignTo="{!con.Id}" value="con1" />
                            </apex:commandButton>
                        </apex:pageBlockButtons>
                    </apex:pageBlock>
                    <apex:commandButton value="Close" action="{!closePopup}" reRender="tstpopup" />
                    
                </apex:outputPanel>
            </apex:outputPanel>
            
            <apex:pageBlock title="Accounts" >
                <apex:pageBlockTable value="{!accnt}" var="ac" >
                    <apex:column id="two">
                        <apex:commandButton value="Click" action="{!showPopup}" reRender="tstpopup" >
                            <apex:param name="accid" value="{!ac.id}" assignTo="{!accid}"/>
                        </apex:commandButton>
                    </apex:column>
                    <apex:column value="{!ac.Name}" />
                    <apex:column value="{!ac.AccountNumber}" />
                    <apex:column value="{!ac.Type}" />
                    <apex:column value="{!ac.Rating}" />
                    <apex:column value="{!ac.Phone}" />
                </apex:pageBlockTable>
            </apex:pageBlock>
        </div>
    </apex:form>
    
    <style type="text/css">
        .custPopup{
        background-color: white;
        border-width: 2px;
        border-style: solid;
        z-index: 9999;
        left: 50%;
        padding:10px;
        position: absolute;
        width: 500px;
        margin-left: -250px;
        top:100px;
        }
        .popupBackground{
        background-color:black;
        opacity: 0.20;
        filter: alpha(opacity = 20);
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        z-index: 9998;
        }
    </style>
    
</apex:page>

Controller:
public class AccountContactPopupC {
    
    public boolean displayPopup {get; set;}
    public String accid {get;set;}
    
    public List<Account> accnt {get;set;}
    public Account con {get;set;} 
    public Contact contact {get;set;}
    public List<Contact> acnt;
    
    public AccountContactPopupC() {
        accnt = [SELECT Id, Name, AccountNumber, Type, Rating, Phone FROM Account LIMIT 100];
        contact = new Contact();
        con = new Account();
        acnt = new List<Contact>();
    }
    
    public void closePopup() {
        displayPopup = false;
    }
    
    public void showPopup() {
        displayPopup = true;
        con = [SELECT Id, Name, Phone, (SELECT firstName, lastName FROM Contacts) FROM Account WHERE Id = :accid];
    }
    
    public void updateContact(){
        contact.AccountId = con.Id;
        acnt.add(contact);
        System.debug('acnt-> ' + acnt);
        upsert acnt;  
        closePopup();
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Pallavi,

I trust you are doing very well.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="AccountContactPopupC" tabStyle="Account" >
    
    <apex:form id="frm">
        <div id="formid">
            <apex:outputPanel id="tstpopup">
                <apex:outputPanel styleClass="popupBackground" layout="block" rendered="{!displayPopUp}" />
                <apex:outputPanel styleClass="custPopup" layout="block" rendered="{!displayPopUp}" >
                    <apex:pageBlock title="My Content" mode="edit">
                        
                        <apex:pageBlockSection title="My Content Section" columns="1">
                            <apex:outputField value="{!con.Name}"/>                            
                            <apex:inputText value="{!contact.firstName}"/>                            
                            <apex:inputText value="{!contact.lastName}"/>                                 
                        </apex:pageBlockSection>
                        <apex:pageBlockButtons >
                            <apex:commandButton value="Save" action="{!updateContact}" >
                                <apex:param name="con1" assignTo="{!con.Id}" value="con1" />
                            </apex:commandButton>
                        </apex:pageBlockButtons>
                    </apex:pageBlock>
                    <apex:commandButton value="Close" action="{!closePopup}" reRender="tstpopup" />
                    
                </apex:outputPanel>
            </apex:outputPanel>
            
            <apex:pageBlock title="Accounts" >
                <apex:pageBlockTable value="{!accnt}" var="ac" >
                    <apex:column id="two">
                        <apex:commandButton value="Click" action="{!showPopup}" reRender="tstpopup" >
                            <apex:param name="accid" value="{!ac.id}" assignTo="{!accid}"/>
                        </apex:commandButton>
                    </apex:column>
                    <apex:column value="{!ac.Name}" />
                    <apex:column value="{!ac.AccountNumber}" />
                    <apex:column value="{!ac.Type}" />
                    <apex:column value="{!ac.Rating}" />
                    <apex:column value="{!ac.Phone}" />
                </apex:pageBlockTable>
            </apex:pageBlock>
        </div>
    </apex:form>
    
    <style type="text/css">
        .custPopup{
        background-color: white;
        border-width: 2px;
        border-style: solid;
        z-index: 9999;
        left: 50%;
        padding:10px;
        position: absolute;
        width: 500px;
        margin-left: -250px;
        top:100px;
        }
        .popupBackground{
        background-color:black;
        opacity: 0.20;
        filter: alpha(opacity = 20);
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        z-index: 9998;
        }
    </style>
    
</apex:page>

Controller:
public class AccountContactPopupC {
    
    public boolean displayPopup {get; set;}
    public String accid {get;set;}
    
    public List<Account> accnt {get;set;}
    public Account con {get;set;} 
    public Contact contact {get;set;}
    public List<Contact> acnt;
    
    public AccountContactPopupC() {
        accnt = [SELECT Id, Name, AccountNumber, Type, Rating, Phone FROM Account LIMIT 100];
        contact = new Contact();
        con = new Account();
        acnt = new List<Contact>();
    }
    
    public void closePopup() {
        displayPopup = false;
    }
    
    public void showPopup() {
        displayPopup = true;
        con = [SELECT Id, Name, Phone, (SELECT firstName, lastName FROM Contacts) FROM Account WHERE Id = :accid];
    }
    
    public void updateContact(){
        contact.AccountId = con.Id;
        acnt.add(contact);
        System.debug('acnt-> ' + acnt);
        upsert acnt;  
        closePopup();
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Steven NsubugaSteven Nsubuga
Here you go
public class TestPopup{
    Account account=new Account();
    public list<Account> acc{get;set;} 
    ApexPages.StandardSetController setCon{get;set;}
    public TestPopup(){
           acc = [select ID, Name, AccountNumber,Type from Account];
            
    }
    Id id = ApexPages.currentPage().getParameters().get('id');
    
    public String aaccountid {get;set;}

    public list<Account> getacc(){
        return acc;
    }
    
    public Boolean displayPopup {get;set;}
    
    public void showPopup()
    { 
    displayPopup = true;
    }
       
    public void closePopup() {
        displayPopup = false;
        
    }
    
    public Id posId {get;set;}

    public string Contactname {get;set;}

    public PageReference save(){
        Contact con= new Contact();
        con.LastName = Contactname;
        con.AccountId= aaccountid;
        insert con;
        ID contactId =con.Id;
        PageReference pr = new PageReference('/' + contactId);
        return pr;
    }  
        
}

Visualforce
<apex:page controller="TestPopup">
<apex:form >
<apex:pageBlock title="All Accounts List">
<!--Account list-->
<apex:pageblockTable value="{!acc}" var="account1">
<apex:column value="{! account1.Name}"/> 
<apex:column value="{! account1.AccountNumber}"/> 
<apex:column > 
<apex:commandButton value="Create Contact" action="{!showPopup}" rerender="popup"><apex:param value="{!account1.Id}" name="acctId" assignTo="{!aaccountid}"/></apex:commandButton>
</apex:column>
</apex:pageblockTable>
</apex:pageBlock>
<apex:pageBlock >
<apex:outputPanel id="popup">
<apex:outputPanel id="popupblock" layout="block" styleClass="customPopup" rendered="{!displayPopup}">
<apex:commandButton value="Close" title="Close the popup" action="{!closePopup}" styleClass="closeButton" rerender="popup"> </apex:commandbutton>
<apex:pageBlockSection title="Contact Form">
<apex:pageBlockSectionItem >
<apex:outputLabel >Contact Name</apex:outputLabel>
<apex:inputText value="{!Contactname}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton value="Save Contact" action="{!save}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
<style type="text/css">
.customPopup {
    background-color: white;
    border-style: solid;
    border-width: 2px;
    left: 20%;
    padding: 10px;
    position: absolute;
    z-index: 9999;
    /* These are the 3 css properties you will need to tweak so the pop 
                            up displays in the center of the screen. First set the width. Then set 
                            margin-left to negative half of what the width is. You can also add 
                            the height property for a fixed size pop up.*/
    width: 500px;
    top: 20%;
}

.disabledTextBox {
    background-color: white;
    border: 1px solid;
    color: black;
    cursor: default;
    width: 90px;
    display: table;
    padding: 2px 1px;
    text-align:right;
}   

.closeButton {
    float: right;
}
</style>
</apex:page>