+ Start a Discussion
Deepak Pandey 13Deepak Pandey 13 

wrapper update list of lead with another field

hello my 

apex class-
public class wrapperleadClassController {
public lead led {get;set;}
public List<lead> AccountsOrigList {get;set;}
public List<wraplead> listlead {get; set;}

public ApexPages.StandardSetController setCon {
 get {
 setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
 [SELECT Name,Status, ID FROM Lead  ]));
 return setCon;
}
set;
}


public wrapperleadClassController()
{

}
public List<wraplead> getleads() 
{
if(AccountsOrigList == null)

AccountsOrigList = setCon.getRecords();
listlead = new List<wraplead>();

for (lead l: AccountsOrigList)
{
listlead.add(new wraplead(l));
system.debug('lead list display'+listlead);
}
}
return listlead;

    
    public Pagereference deletes()
     {
        List<lead> leadsToDelete = new List<lead>();
        
        for (wraplead avar : listlead)
        {
            if (avar.selected == True)
                {
                 if (avar.ld.id != null)   
                    {                
                    leadsToDelete.add(avar.ld);
                    }               
                }
        }
       delete leadsToDelete;
       return null;
     }
 
    

public PageReference updt() 
{
        List<lead> leadsToUpsert = new List<lead>();         
        for(wraplead ldup : listlead) 
        {

            if(ldup.selected == true) 
            {
           if (ldup.ld.id != null)   
           {
            ldup.ld.id=led.id;
          ldup.ld.Status=led.Status;
                leadsToUpsert.add(ldup.ld);
               }
            }
           } 
        update leadsToUpsert ;

        return null;
    }

                     


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;
}
}
}

page-
<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:column >
           <apex:selectRadio value="{!aa.Checked}" />
         </apex:column>    
       </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:commandButton value="Delete" action="{!deletes}" />
           </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

VineetKumarVineetKumar
Please be more clear with your query, and whenever you post a code please use <> in the editor toolbar.
So, it will be easy for us to refer to the line number
Deepak Pandey 13Deepak Pandey 13
in this method i wanna update list of lead with led.status
but is not happening?
public PageReference updt() 
{
        List<lead> leadsToUpsert = new List<lead>();         
        for(wraplead ldup : listlead) 
        {

            if(ldup.selected == true) 
            {
           if (ldup.ld.id != null)   
           {
            ldup.ld.id=led.id;
          ldup.ld.Status=led.Status;
                leadsToUpsert.add(ldup.ld);
               }
            }
           } 
        update leadsToUpsert ;

        return null;
    }
VineetKumarVineetKumar
It's not updating beacause you are updating the same record.
ldup.ld.id=led.id; // here you are assigning the lead id to a lead object.
ldup.ld.Status=led.Status; // here you are assigning the status of the lead to the same lead object.
Also there is nothing for led.id,  I can't see you querying the lead or something, now since there is no Id as well, update cannot be done.
Let me know if this helps.
Deepak Pandey 13Deepak Pandey 13
i know that sir. Can you asked me how to put led id in wrapper lead ?
VineetKumarVineetKumar
What I believe is that the below line is not required:
ldup.ld.id=led.id;
since, you already the required Id in ldup.ld.id and you just blanking it out, what I believe is that you upsert must be creating new records.
 
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