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
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

How to update parent record field by using wrapper class of child object ---Urgent plz help!!!!

Hi All,
        I am using a wrapper class with checkboxes to display a list of conact record with contact phone field and Account name. In this list  upon selection of a checkbox i wanna 2 updates.
1. Contact Phone Field.
2. Contacts Account Name.
Code is pretty standard , taken from wrapper class documentation example ,Its given below:

Apex:
public class wrapperClassController {
    //Our collection of the class/wrapper objects cContact 
    public List<cContact> contactList {get; set;}
    public List<Account> AccountList {get; set;}
    public List<Id> AccountIdList {get; set;}
    //This method uses a simple SOQL query to return a List of Contacts
    public List<cContact> getContacts() {

        if(contactList == null) {
            contactList = new List<cContact>();
            for(Contact c: [select Id, Name, Email, Phone, Contact.Account.Name,Contact.Account.isPersonAccount from Contact limit 10]) {
                // As each contact is processed we create a new cContact object and add it to the contactList
                contactList.add(new cContact(c));
            }
        }
        return contactList;
    }
    public PageReference processSelected() {
    //We create a new list of Contacts that we be populated only with Contacts if they are selected
      List<Contact> selectedContacts = new List<Contact>();
      //We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
      for(cContact cCon: getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
            }
        }
      // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
      System.debug('These are the selected Contacts...');
      for(Contact con: selectedContacts) {
          
            system.debug('aaaaaaaaaaaaaaaaaaaaaaaa'+con);
            
       }

       update selectedContacts;
       contactList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
       return null;
   }
   // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}
        //This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
        public cContact(Contact c) {
        con = c;
        selected = false;
      }
    }


}
///////////////////////////////////////////VF PAGE////////////////////////////////////////////////
<apex:page controller="wrapperClassController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cContact records -->
            <apex:pageBlockTable value="{!contacts}" var="c" id="table">
            <apex:column >
            <!-- This is our selected Boolean property in our wrapper class -->
            <apex:inputCheckbox value="{!c.selected}"/>
            </apex:column>
            <!-- This is how we access the contact values within our cContact container/wrapper -->
            <apex:column value="{!c.con.Name}" />
            <apex:column value="{!c.con.Email}" />
            <!--<apex:column value="{!c.con.Phone}" />-->
            <apex:column title="Contact Phone" headerValue="Contact Phone">
                 <apex:facet name="Contact Phone">Contact Phone</apex:facet>
                 <apex:inputField value="{!c.con.Phone}" />
           </apex:column>
           <apex:column title="Account Name" headerValue="Account Name">
                 <apex:facet name="Account Name">Account Name</apex:facet>
                 <apex:inputField value="{!c.con.Account.Name}" />
           </apex:column>
           </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Upon click of button phone field is getting updated , plz help me with updation of Account Name as well




Thanks
Shrey Tyagi



Best Answer chosen by shrey.tyagi88@tcs.com
GlynAGlynA
Shrey,

Try this:

List<Contact> selectedContacts = new List<Contact>();
List<Account> selectedAccounts = new List<Account>();     

      for(cContact cCon: getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
                selectedAccounts.add(cCon.con.Account);
            }
        }

update selectedContacts;
update selectedAccounts;

-Glyn

All Answers

GlynAGlynA
Shrey,

Try this:

List<Contact> selectedContacts = new List<Contact>();
List<Account> selectedAccounts = new List<Account>();     

      for(cContact cCon: getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
                selectedAccounts.add(cCon.con.Account);
            }
        }

update selectedContacts;
update selectedAccounts;

-Glyn
This was selected as the best answer
LithiumsLithiums
You should check this link, it has detaile description about wrapper classes

http://wiki.developerforce.com/page/Wrapper_Class
abhijitkulaye1.3947193846807988E12abhijitkulaye1.3947193846807988E12
hi...

I wanted to fetch some records from  custom object and update or save those records. Please read the below code and suggest solution...

Apex class:
public with sharing class wrapperOne{

      public PageReference save() {
               return null;
    }

   public list<wrapperclass> newadmin{get;set;}
   
    list<Admission__c> obj=[select id, name,DateofBirth__c,Annual_Income__c,Email__c from Admission__c limit 10 ];
   
    public list<wrapperclass> getnewadminwrapper()
    {   
        newadmin=new list<wrapperclass>();
        for(Admission__c o:obj)
        {
            newadmin.add(new wrapperclass(o));       
        }
          return newadmin;
    }
   
    public class wrapperclass
    {
        public Admission__c one{get;set;}
       
        public wrapperclass(Admission__c ob)
        {
            this.one=(ob);
        }   
    }

}

Visualforce code:

<apex:page controller="wrapperOne" >
  <apex:form >
  <apex:pageBlock >
  <apex:pageBlockTable value="{!newadminwrapper}" var="ad">
  <apex:column value="{!ad.one.name}"/>
  <apex:column value="{!ad.one.DateofBirth__c}"/>
  <apex:column value="{!ad.one.Email__c}"/>
  </apex:pageBlockTable>
  <apex:pageBlockButtons >
  <apex:commandButton action="{!save}" value="save"/>
  </apex:pageBlockButtons>
    </apex:pageBlock>
    <apex:inlineEditSupport event="ondblclick" showOnEdit="save"/>
  </apex:form>
</apex:page>

Please suggest me solution...i am making one application for engineering college admission process....please help...