Here's my code that i have tried.

According to below code i am able to add selected accounts to new object. Here are the issues:

  1. Duplicate records are added to New Object.
  2. When unchecked on account page and saved, the records are not deleted from new object.
  3. Values of Checkboxes are not retained on the vf page.

Original Object - Account, New Object - Selected_Accounts__c

VF Page:

<apex:page controller="TestNewA" sidebar="false">
  <script type="text/javascript">
  function selectAllCheckboxes(obj,receivedInputID)
  {  var inputCheckBox = document.getElementsByTagName("input");
     for(var i=0; i<inputCheckBox.length; i++)
     { if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1)
       {  inputCheckBox[i].checked = obj.checked;
  <apex:form >
     <apex:pageBlock >
        <apex:pageBlockButtons >
           <apex:commandButton value="Add/Show Selected Accounts" action="{!processSelected}" rerender="table2"/>

   <apex:pageblockSection title="All Accounts" collapsible="false" columns="2">
     <apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Accounts">
        <apex:column >
           <apex:facet name="header">
             <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
             <apex:inputCheckbox value="{!accWrap.isSelected}" id="inputId"/>
         <apex:column value="{!accWrap.acc.Name}" />
     <apex:pageBlockTable value="{!selectedAccounts}" var="c" id="table2" title="Selected Accounts">
           <apex:column value="{!c.Name}" headerValue="Account Name"/>

And My Controller:

public class TestNewA 
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}

    public TestNewA()
    {  if(wrapAccountList == null) 
        {  wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name from Account]) 
                wrapAccountList.add(new wrapAccount(a,false));
    }  }  }

   public  List<Selected_Accounts__c> cList=new List<Selected_Accounts__c>();

   public void processSelected() 
   {   selectedAccounts = new List<Account>();
         for(wrapAccount wrapAccountObj : wrapAccountList) 
         {    if(wrapAccountObj.isSelected == true) 
                {   selectedAccounts.add(wrapAccountObj.acc); // got all selected accounts
         }     }
         for(Account acc:selectedAccounts)
           Selected_Accounts__c c = new Selected_Accounts__c();


    public class wrapAccount 
    {   public Account acc {get; set;}
         public Boolean isSelected {get; set;}
         public wrapAccount(Account acc, Boolean isSelected) 
            this.acc = acc;
            this.isSelected = isSelected;
   }  }
  • Your indentation/bracing style is pretty hard to read and will invariably lead you to confusion down the road. – Adrian Larson Nov 14 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.