You need to sign in to do that
Don't have an account?

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
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.

<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.
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:
Controller:
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
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:
Controller:
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
Visualforce