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
Rama Gaikwad 1Rama Gaikwad 1 

Auto populate Account Id On Contact Visualforce Page to create Account related contacts

My requirement is to create a contact , by clicking a button on the account detail page. Button click will open a new vf page with contact fields onto it.Account name field on new vf page must be auto popuated, where as rest of the contact fields can be typed in.
How do I achieve this?
lnallurilnalluri
@Rama this can be achieved using URL hacking. Make sure you have right field id's (starts with 00N)

Check this link Pre-populate Opportunity (https://www.salesforceben.com/salesforce-url-hacking-tutorial/)

Thanks.
Rama Gaikwad 1Rama Gaikwad 1
Hey Inalluri , 
Thanks  for  reply But i want  to achieve it by using visualforce pages . 



Even I write code for the same :

<apex:page showHeader="true" showChat="false" sidebar="false" applyHtmlTag="true" standardController="Account" extensions="DisplayAccountRelatedContactOnVfPage" >
    
    <script type="text/javascript">
    function openPopup(page,param){
        window.open(page+'?param='+param,'_blank', 'toolbar = No, scrollbars = No, resizable = yes, top = 500, left=500, width=700, height=250')
    }
    // twistSection(document.getElementById('{!$Component.section1}').getElementsByTagName('img')[0]) 
   
    </script>
    <apex:form id="form1" >
        <div id="error">Div tag :</div>
        <apex:pageBlock id="block1">
            
            <apex:outputLink onclick="openPopup('{!$Page.CreateAccountVF}','Create Account');">
                <apex:commandButton value="+" image="{!URLFOR($Resource.PlusSign)}"  style="background:LightBlue;width:50px;" reRender="block1" />  
            </apex:outputLink> <br/>
            
            <apex:repeat value="{!AccountsRelatedContact}" var="accVal" id="repeat1"  >
                <apex:pageBlockSection collapsible="true" title="{!accVal.Name}" id="section1" columns="1"  >
                   
                    <!-- 
                    <input type="text" hidden="false" id="textVal" value="{!accVal.Id}" /> 
                    -->
                    <apex:commandLink  onclick="openPopup('{!$Page.CreateContactVf}','Create Contacts');" action="{!takeAccountId}" >
                        <apex:commandButton  id="accountButton" image="{!URLFOR($Resource.PlusSign)}"  style="background:white; width:15px;" reRender="block1"   />  
                        <apex:param value="" name="accId" assignTo="{!accVal.Id}"/>
                    </apex:commandLink>
                    
                    <apex:repeat value="{!accVal.Contacts}" var="con" >
                        <br/><div>{!con.LastName}</div> 
                    </apex:repeat>
                </apex:pageBlockSection>
            </apex:repeat>
        </apex:pageBlock>
    </apex:form>
</apex:page>




Controller : 



public class DisplayAccountRelatedContactOnVfPage {
    public  String accId{get;set;}
    
    public DisplayAccountRelatedContactOnVfPage(ApexPages.StandardController stdcontroller){}
    
    public  List<Account> getAccountsRelatedContact()
    {
        List<Account> accounts = [Select Id,(Select Id,LastName, AccountId From Contacts where AccountId != NULL), Name from Account LIMIT 10 ];
        return accounts;
    }
    
    public  PageReference takeAccountId() {
        accId = ApexPages.currentPage().getParameters().get('accId');
        System.debug('accId >>'+ accId);
        return null;
    }
    
}

I am trying to , fetch visualforce value into my controller but I got NULL value for Account Id .
Can you help me to solve it.

Thank You
R@ma.