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
Adil_SFDCAdil_SFDC 

Insert Selected rows in Custom Object

HiAll

 

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>
                <apex:column headerValue="Contact Name">
                    <apex:outputText value="{!con.contactName}"/>
                </apex:column>
                <apex:column headerValue="Contact Email">
                    <apex:outputText value="{!con.contactEmail}"/>
                </apex:column>
            </apex:pageBlockTable>
                        </apex:pageBlock>

            </apex:form>
</apex:page>

 

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);
                contactDetailsList.add(cd);
            }
        }
    }



   
  
 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);
   consec.Name='test';
   consec.Contact__c=;
   insert consec;
        return null;
    }

    

}

Please help

salvatoreovlassalvatoreovlas

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; }