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
Amita TatarAmita Tatar 

how to assign selected records to other selected records using wrapper class


I have a requirement where i want to insert selected notes to the selected contacts & opportunities using wrapper class.
How  can i achieve this. Here is my code:
Apex class------

public class DisplayNotesNew{

public List<WrapperClass> listWrapper {get;set;}
public List<ContactWrapper> listWrapper1 {get;set;}
public List<OpportunityWrapper> listWrapper2 {get;set;}
public Id accId;
Public Account acc;
public List<Note> selectedNotes{get;set;}
public List<Contact> selectedContacts{get;set;}
public List<opportunity> selectedOpps{get;set;}
public DisplayNotesNew(ApexPages.StandardController Controller) {
accId = ApexPages.currentPage().getParameters().get('id');

List<Note> listAcct = [SELECT Id,Title, Body,Parent.Id FROM Note where Parent.Id =: accId];
  if(listAcct.size() > 0) {
    listWrapper = new List<WrapperClass>();
       for(Note a : listAcct) {
          listWrapper.add(new WrapperClass(a));
List<Contact> listContact = [Select Id, FirstName, LastName,name From Contact where AccountId =: accId];
    if(listContact.size() > 0){
        listWrapper1 = new List<ContactWrapper>();
            for(Contact c : listContact){
                listWrapper1.add(new ContactWrapper(c));

List<Opportunity> listOpp = [select id,name from Opportunity where AccountId =: accId];
    if(listOpp.size() > 0){
        listWrapper2 = new List<OpportunityWrapper>();
            for(Opportunity o : listOpp){
                listWrapper2.add(new OpportunityWrapper(o));
public pagereference assignNotes(){
selectedNotes = new List<Note>();
  for(WrapperClass wrapAccountObj : listWrapper ) {
     if(wrapAccountObj.checkBool == true) {

selectedContacts = new List<Contact>();
 for(ContactWrapper wrapContactObj : listWrapper1){
     if(wrapContactObj.checkBool == true){
selectedOpps = new List<Opportunity>();
  for(OpportunityWrapper wrapOppObj : listWrapper2){
      if(wrapOppObj.checkBool == true){
return null;

public class WrapperClass{
   public Boolean checkBool {get;set;}
   public Note acct {get;set;}
   public WrapperClass(Note acct) {
        this.acct = acct;
public class ContactWrapper{
   public Boolean checkBool {get;set;}
   public Contact cons {get;set;}
   public ContactWrapper(Contact cons) {
        this.cons = cons;

public Class OpportunityWrapper{
   public Boolean checkBool {get;set;}
   public Opportunity ops {get;set;}
   public OpportunityWrapper(Opportunity ops) {
        this.ops = ops;


VF Page-----------------------

<apex:page standardController="Account" extensions="DisplayNotesNew" tabStyle="Account">
<apex:form >

<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID){
 var inputCheckBox = document.getElementsByTagName("input");
 for(var i=0; i<inputCheckBox.length; i++){
inputCheckBox[i].checked = obj.checked;

<apex:pageMessages />
<apex:pageBlock id="pg">
<apex:pageblockButtons >
<apex:commandButton value="Assign Notes"/>

<apex:pageBlockSection title="ACCOUNT NOTES">
<apex:pageBlockTable value="{!listWrapper}" var="a">
<apex:column >
<!-- <apex:facet name="header">
<apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
</apex:facet> -->
<input type="radio" name="selection" value="{!a.checkBool}"/> 
<!-- <apex:inputCheckbox value="{!a.checkBool}" id="inputId"/> -->
<apex:column value="{!a.acct.Title}"/>
<apex:column value="{!a.acct.Body}"/>  
<apex:pageBlockSection TITle="CONTACTS & OPPORTUNITIES">
<apex:pageblockTable value="{!listWrapper1}" var="a">
<apex:column >
<apex:inputCheckbox value="{!a.checkBool}" id="inputId"/>
<apex:column value="{!}" headerValue="Related Contacts"/>
<Apex:pageblockTable value="{!listWrapper2}" var="a">
<apex:column >
<apex:inputCheckbox value="{!a.checkBool}" id="inputId"/>
<apex:column value="{!}" headerValue="Related Opportunities"/>