+ Start a Discussion
Deepak Pandey 13Deepak Pandey 13 

wrapper not updating Lead status

hello.  i have wrapper class want to update status field.
Apex class-
public class wrapperleadClassController {
public lead led {get;set;}
public List<wraplead> listlead {get; set;}
public wrapperleadClassController()
{

}
public List<wraplead> getleads() 
{
listlead = new List<wraplead>();
  
for(lead l : [select Name,Status from lead limit 10]) 
{
listlead.add(new wraplead(l));
system.debug('lead list display'+listlead);
}
return listlead;
}
public PageReference updt() 
{
        List<lead> selectedlead = new List<lead>();
       
        
        for(wraplead ldup : getleads()) 
        {

            if(ldup.selected == true) 
            {
            lead ll = new lead();
           ll.Status=led.Status;
                selectedlead.add(ll);
                system.debug('lead list display'+selectedlead);
               }
            }
        update selectedlead;

        return null;
    }

vf pages-

<apex:page controller="wrapperleadClassController" sidebar="false" showHeader="false">
 
<apex:form >
    <apex:pageBlock id="table" >
       <apex:pageBlockTable value="{!leads}" var="aa" id="table">
             <apex:column >
           <apex:inputcheckbox value="{!aa.selected }"/>
         </apex:column>
          <apex:column value="{!aa.ld.Name}" />
         <apex:column value="{!aa.ld.Status}" />           
       </apex:pageBlockTable>   
     </apex:pageBlock>
   
     
     <apex:pageBlock >
       <apex:pageBlockSection >
          <apex:pageblockSectionItem >
         Status    
           <apex:inputField value="{!led.Status}"/>
          </apex:pageblockSectionItem>
          <apex:commandButton value="Update" action="{!updt}" />
           </apex:pageBlockSection>
    </apex:pageBlock>
           
 </apex:form>

</apex:page>                     


 
Best Answer chosen by Deepak Pandey 13
Deepak Pandey 13Deepak Pandey 13
public class wrapperleadClassController {
public lead led {get;set;}
public list<lead> selectedlead {get;set;}
public List<wraplead> listlead {get; set;}

public wrapperleadClassController()
{   
led= new lead();
listlead =new List<wraplead>();
leadtWrapperObject();
}

 
public void leadtWrapperObject(){
listlead =new List<wraplead>();
for(lead l:[select id,name,status from lead limit 5])
{
listlead.add(new wraplead(l));
}
}

public void getselected(){
selectedlead = new list<lead>();
selectedlead.clear();
for(wraplead awrap : listlead )
if(awrap.selected==true){
selectedlead.add(awrap.ld);
}
}

public void ChangeStatus()
{
for(lead a : selectedlead)
{
a.status = led.Status;
//selectedaccount.add(a);
update a;
}
selectedlead.clear();
}

public void deleteselected()
{
list<lead> deleteleadlist = new list<lead>(); 
for(lead a : selectedlead)
{
deleteleadlist.add(a);

}
delete deleteleadlist ;
}

                     
public class wraplead {
public lead ld {get; set;}
public Boolean selected {get; set;}
public Boolean Checked {get; set;}
public wraplead(lead l) {
ld = l;
selected = false;
Checked =true;
}
}
}

All Answers

bob_buzzardbob_buzzard
The problem is that in your update method you iterate over the result of getLeads():
 
for(wraplead ldup : getleads())

However, getLeads always retrieves the leads from the database, which will clear out any user input. You should only go back to the database if listleads is null - that way you will set them up once, and every call after that will return the cached list including any changes from the user.
 
Deepak Pandey 13Deepak Pandey 13
i clear the listl of lead but it's not working. 
bob_buzzardbob_buzzard
Why would you clear the list of leads? In my answer I've said that you need to retain the list of leads, not generate it again every time the method is called.
Deepak Pandey 13Deepak Pandey 13
for(wraplead ldup : listlead)  i calld the list but he value is not updated.
Deepak Pandey 13Deepak Pandey 13
public class wrapperleadClassController {
public lead led {get;set;}
public list<lead> selectedlead {get;set;}
public List<wraplead> listlead {get; set;}

public wrapperleadClassController()
{   
led= new lead();
listlead =new List<wraplead>();
leadtWrapperObject();
}

 
public void leadtWrapperObject(){
listlead =new List<wraplead>();
for(lead l:[select id,name,status from lead limit 5])
{
listlead.add(new wraplead(l));
}
}

public void getselected(){
selectedlead = new list<lead>();
selectedlead.clear();
for(wraplead awrap : listlead )
if(awrap.selected==true){
selectedlead.add(awrap.ld);
}
}

public void ChangeStatus()
{
for(lead a : selectedlead)
{
a.status = led.Status;
//selectedaccount.add(a);
update a;
}
selectedlead.clear();
}

public void deleteselected()
{
list<lead> deleteleadlist = new list<lead>(); 
for(lead a : selectedlead)
{
deleteleadlist.add(a);

}
delete deleteleadlist ;
}

                     
public class wraplead {
public lead ld {get; set;}
public Boolean selected {get; set;}
public Boolean Checked {get; set;}
public wraplead(lead l) {
ld = l;
selected = false;
Checked =true;
}
}
}
This was selected as the best answer