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
Rutvij Pathak 5Rutvij Pathak 5 

unable to pass Account ID to New Contact List

I am creating a Page to add multiple contacts to an Account. The Page is Called by a Button Click on Account Detail Page.'
For now, Contacts are getting Created however, they are not getting Associated to Account.

VISUALFORCE PAGE ---------

<apex:page name="Create Multiple Contacts" controller="AddMultipleContacts" showHeader="true" sidebar="true">        
    <apex:form >    
    <apex:pageBlock title="Add Contacts">
        <apex:variable var="rowNum" value="{!0}"/>
        <apex:pageBlockTable value="{!ContactList}" var="con">
            <apex:facet name="footer">               
                <apex:commandButton value="Add" style="float: right;" action="{!insertRow}" />                    
             </apex:facet>
            <apex:column headerValue="First Name">
                <apex:inputField value="{!con.FirstName}"/>
            </apex:column>
            
            <apex:column headerValue="Last Name">
                <apex:inputField value="{!con.LastName}"/>
            </apex:column>
            <apex:column headerValue="Phone">
                <apex:inputField value="{!con.Phone}"/>
            </apex:column>
            <apex:column headerValue="Email">
                <apex:inputField value="{!con.Email}"/>                
            </apex:column>
            
             <apex:column headerValue="Action" >
                    <apex:commandLink style="font-size:12px; font-weight:bold; text-align:center;color:Black;" value="Delete" action="{!delRow}">
                    <apex:param value="{!rowNum}" name="index" />
                    </apex:commandLink> 
                    <apex:variable var="rowNum" value="{!rowNum+1}"/>
             </apex:column> 
         </apex:pageBlockTable>
            <apex:commandButton value="Cancel" onclick="window.history.previous()"/>
            
            <apex:pageBlockButtons location="bottom" >
                <apex:commandButton value="Save Contacts" action="{!insertContacts}"/>           
            </apex:pageBlockButtons>
    </apex:pageBlock>
    </apex:form>        
</apex:page>

CONTROLLER ---------

public class AddMultipleContacts {
    Id AcctID;
    public List<Contact> ContactList {get;set;}
    public Integer rowNum{get;set;}
    
    public AddMultipleContacts(){
        AcctID = ApexPages.currentPage().getParameters().get('AcctID');
        ContactList = new List<Contact>();  
        ContactList.add(new contact());        
    }        
    public void insertContacts(){
        insert ContactList;
    }                    
    public void insertRow(){
        ContactList.add(new contact()); 
    }    
    public void delRow(){
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        ContactList.remove(rowNum);   
    }        
}
Best Answer chosen by Rutvij Pathak 5
Arshadulla.ShariffArshadulla.Shariff
Hello

Try change your Constrcutor with follwing .
public AddMultipleContacts(){
        AcctID = ApexPages.currentPage().getParameters().get('AcctID');
        ContactList = new List<Contact>();  
        ContactList.add(new contact(AccountID=AcctID));        
    }      

Hope the answer helps you.
Thanks

All Answers

Arshadulla.ShariffArshadulla.Shariff
Hello

Try change your Constrcutor with follwing .
public AddMultipleContacts(){
        AcctID = ApexPages.currentPage().getParameters().get('AcctID');
        ContactList = new List<Contact>();  
        ContactList.add(new contact(AccountID=AcctID));        
    }      

Hope the answer helps you.
Thanks
This was selected as the best answer
EldonEldon
Hi Rutvij,

Make you button in account object with below config,

User-added image


Then Change you apex class with below class,
 
public class AddMultipleContacts {
    public Id AcctID{get;set;}
    public List<Contact> ContactList {get;set;}
    public Integer rowNum{get;set;}
    
    public AddMultipleContacts(){
        AcctID = ApexPages.currentPage().getParameters().get('id');
        ContactList = new List<Contact>();  
        ContactList.add(new contact());        
    }        
    public void insertContacts(){
    for(contact c : ContactList){
        c.accountid = AcctID ;
    } 
        insert ContactList;
    }                    
    public void insertRow(){
    
      
        ContactList.add(new contact());    
    }    
    public void delRow(){
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        ContactList.remove(rowNum);   
    }        
}



Let me know if you have any issues

Regards
Rutvij Pathak 5Rutvij Pathak 5
Hi , @Arshadulla.Shariff, Thank you for your input, My Issue got resolved by using standard controller Account and my Controller as extension and then implementing your solution.

Thank you so much