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
SV MSV M 

Create New Contact

Hi, I have a wrapper class where it displays contacts related to account object with a checkbox. I want to add a button which should create a new contact with the same accountId...
//Wrapper Class

public class AccountContactWrapper {
    List<Contact> selectedRecords {get;set;}
    public List<WrapperClass> wrapperList {get;set;}
    public List<Account> accList {get;set;}
    public AccountContactWrapper (ApexPages.StandardController stdController) {
        accList = [SELECT Id, Name FROM Account];
        wrapperList = new List<WrapperClass>();
        for(Contact c : [SELECT FirstName, LastName, Phone FROM Contact WHERE AccountId =: ApexPages.currentPage().getParameters().get('id')]) {
            wrapperList.add(new WrapperClass(c));
        }
    }
    //Deleting SelectedRecords
    public void deleteRecords() {
        List<Contact> selectedRecords = new List<Contact>();
        for (WrapperClass wrap : wrapperList) {
            if(wrap.selected==true) {
                selectedRecords.add(wrap.con);
            }
        }
        delete selectedRecords;
    }
    //Editing Selected Records
    /*public void editRecords() {
        List<Contact> selectedRecords = new List<Contact>();
        for (WrapperClass wrap : wrapperList) {
            if(wrap.selected==true) {
                selectedRecords.add(wrap.con);
            }
        }
        selectedRecords.isEdit=true;
    }*/
    //Creating New Contact
    public void createNewContact() {
        
    }
    public class WrapperClass {
        public Contact con {get;set;}
        public Boolean selected {get;set;}
        public WrapperClass (Contact c) {
            con=c;
            selected=false;
        }
    }
}

//Apex Page


<apex:page standardController="Account" extensions="AccountContactWrapper">
    <apex:form>
        <apex:pageBlock id="block">
            <apex:pageBlockButtons location="bottom">
                <!--<apex:commandButton value="Edit" action="{!processedRecords}"/>-->
                <apex:commandButton value="Delete Selected Records" action="{!deleteRecords}"/>
                <apex:commandButton value="Create Contact" action="{!createNewContact}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection>
                <apex:pageBlockTable value="{!wrapperList}" var="wrap" id="table">
                    <apex:column>
                        <apex:inputCheckbox value="{!wrap.selected}"/>
                    </apex:column>
                    <apex:column value="{!wrap.con.FirstName}" headerValue="First Name"/>
                    <apex:column value="{!wrap.con.LastName}" headerValue="Last Name"/>
                    <apex:column value="{!wrap.con.Phone}" headerValue="Phone"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
ShirishaShirisha (Salesforce Developers) 
Hi Sai Vineeth,

Can you please try the sample code for createContact in the below documentation:

https://developer.salesforce.com/forums/?id=906F000000092OYIAY

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

Warm Regards,
Shirisha Pathuri
sachinarorasfsachinarorasf
Hi Sai,

I have gone through your problem.
 
Vf Page:
<apex:page standardController="Account" extensions="AccountContactWrapper">
    <apex:form>
        <apex:pageBlock id="block">
            <apex:pageBlockButtons location="bottom">
                <!--<apex:commandButton value="Edit" action="{!processedRecords}"/>-->
                <apex:commandButton value="Delete Selected Records" action="{!deleteRecords}"/>
                <apex:commandButton value="Create Contact" action="{!createNewContact}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection>
                <apex:pageBlockTable value="{!wrapperList}" var="wrap" id="table">
                    <apex:column>
                        <apex:inputCheckbox value="{!wrap.selected}"/>
                    </apex:column>
                    <apex:column value="{!wrap.con.FirstName}" headerValue="First Name"/>
                    <apex:column value="{!wrap.con.LastName}" headerValue="Last Name"/>
                    <apex:column value="{!wrap.con.Phone}" headerValue="Phone"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Controller:

public class AccountContactWrapper {
    List<Contact> selectedRecords {get;set;}
    public List<WrapperClass> wrapperList {get;set;}
    public List<Account> accList {get;set;}
    public AccountContactWrapper (ApexPages.StandardController stdController) {
        accList = [SELECT Id, Name FROM Account];
        wrapperList = new List<WrapperClass>();
        for(Contact c : [SELECT FirstName, LastName, Phone FROM Contact WHERE AccountId =: ApexPages.currentPage().getParameters().get('id')]) {
            wrapperList.add(new WrapperClass(c));
        }
    }
    //Deleting SelectedRecords
    public void deleteRecords() {
        List<Contact> selectedRecords = new List<Contact>();
        for (WrapperClass wrap : wrapperList) {
            if(wrap.selected==true) {
                selectedRecords.add(wrap.con);
            }
        }
        delete selectedRecords;
    }
    //Editing Selected Records
    /*public void editRecords() {
        List<Contact> selectedRecords = new List<Contact>();
        for (WrapperClass wrap : wrapperList) {
            if(wrap.selected==true) {
                selectedRecords.add(wrap.con);
            }
        }
        selectedRecords.isEdit=true;
    }*/
    //Creating New Contact
    public void createNewContact() {
        Contact contactObj = new Contact();
        contactObj.LastName = 'LastNameValue';
        contactObj.AccountId = ApexPages.currentPage().getParameters().get('id');
        insert contactObj;
    }
    public class WrapperClass {
        public Contact con {get;set;}
        public Boolean selected {get;set;}
        public WrapperClass (Contact c) {
            con=c;
            selected=false;
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora