+ Start a Discussion
Amita TatarAmita Tatar 

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

Hi,

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');
system.debug('********************'+accId);

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) {
        selectedNotes.add(wrapAccountObj.acct);
        system.debug('*********selectedNotes********'+selectedNotes);
      }         
   }

selectedContacts = new List<Contact>();
 for(ContactWrapper wrapContactObj : listWrapper1){
     if(wrapContactObj.checkBool == true){
         selectedContacts.add(wrapContactObj.cons);
         system.debug('*********selectedContacts******'+selectedContacts);
    }
 }
selectedOpps = new List<Opportunity>();
  for(OpportunityWrapper wrapOppObj : listWrapper2){
      if(wrapOppObj.checkBool == true){
          selectedOpps.add(wrapOppObj.ops);
          system.debug('******selectedOpps*********'+selectedOpps);
     }
  }
   
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++){
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
inputCheckBox[i].checked = obj.checked;
      }
    }
  }
</script>

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

<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>
<apex:column value="{!a.acct.Title}"/>
<apex:column value="{!a.acct.Body}"/>  
</apex:pageBlockTable>
</apex:pageBlockSection>
<br/>
<br/>
<apex:pageBlockSection TITle="CONTACTS & OPPORTUNITIES">
<apex:pageblockTable value="{!listWrapper1}" var="a">
<apex:column >
<apex:inputCheckbox value="{!a.checkBool}" id="inputId"/>
</apex:column>
<apex:column value="{!a.cons.name}" headerValue="Related Contacts"/>
</apex:pageblockTable>
<Apex:pageblockTable value="{!listWrapper2}" var="a">
<apex:column >
<apex:inputCheckbox value="{!a.checkBool}" id="inputId"/>
</apex:column>
<apex:column value="{!a.ops.name}" headerValue="Related Opportunities"/>
</Apex:pageblockTable>
</apex:pageBlockSection>
     
</apex:pageBlock>
</apex:form>
</apex:page>
Thanks,
Amita