+ Start a Discussion

Insert Selected rows in Custom Object



I have written a VF Page to display Contacts related to Opportunity with check boxes. 

Now I want to insert the selected contacts from the VF Page in to a Custom Object called contact_select__c

The custom object is look up to contact

I am stuck at a point where  i can assign selected rows contact name to the contact name. If 2 rows are selected I need two Records created.


<apex:page controller="contactoppdisp" tabStyle="Contact" showHeader="false">
<apex:form >
<apex:commandButton value="Select Contacts" action="{!SelectContact}"/>
<apex:pageBlock >
  <apex:pageBlockTable value="{!contactDetailsList}" var="con" id="conTableId" rendered="{!NOT(ISNULL(contactDetailsList))}">
                <apex:column >
                    <apex:inputCheckbox value="{!con.isSelected}"/>
                <apex:column headerValue="Contact Name">
                    <apex:outputText value="{!con.contactName}"/>
                <apex:column headerValue="Contact Email">
                    <apex:outputText value="{!con.contactEmail}"/>



public class contactoppdisp {


 private Opportunity o;

public List<ContactDetails> contactDetailsList {get; set;}

    public contactoppdisp() {
        o = [select id, Accountid, name,Email__c from Opportunity where id = :ApexPages.currentPage().getParameters().get('id')];
        Contact[] contactList = [select id, name, email from Contact where accountid = :o.Accountid];
        if(contactList.size() > 0)
            contactDetailsList = new List<ContactDetails>();
            for(Contact c: contactList)
                ContactDetails cd = new ContactDetails(c.id, c.name, c.email);

 public class ContactDetails
        public ID contactId {get; set;}
        public String contactName {get; set;}
        public String contactEmail {get; set;}
        public Boolean isSelected {get; set;}       
        public ContactDetails(Id conId,String name,String Email)
            contactId = conId;
            contactName = name;
            contactEmail = email;
            isSelected = false;
   public PageReference SelectContact() {
   ContactSelect__c consec = new ContactSelect__c();
   ContactDetails cds = new ContactDetails(consec.id,consec.name,consec.email__c);
   insert consec;
        return null;



Please help


you have to loop through your list: (if this helps mark it as a solution :) )

   public PageReference SelectContact() {
     list<ContactSelect__c> l = new list<ContactSelect__c>();
for(ContactDetails c: contactDetailsList){
if (c.isSelected){
l.add(new ContactDetails(consec.id,consec.name,consec.email__c);)
}insert l;
} return null; }