You need to sign in to do that
Don't have an account?
SV 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>
//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>
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
I have gone through your problem.
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