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
belabela 

From list if one record is selected (checbox) and click continue for that record task should be inserted and should show remaining list

Hi All,

I have list of candidates  for batch.so I have to assign task to them.for giving task,I have to select the students and once click continue,task should be assigned to selected candidates. and should show only remaining unselected students,so that we can assign task to remaining students.

I am able to do this but once selected one student and task is inserted for that student and next i select another student and trying to insert,The task is assigning to first student which i selected previously and inserting again.means the list is not cleared .can any one help me in this .below is my code.

/*****Apex code:
===========
public class taskrepeat {

public task__c t{set;get;}

public list<task__c> ttt{set;get;}


public list<opportunity__c> OppList{set;get;}

public list<opportunity__c> Oppclear{set;get;}


public list<batch__c > Batch  {set;get;}

public boolean studentshow {set;get;}

public list<studentwrapper> stwrap{set;get;}

public list<studentwrapper> stwrap11{set;get;}


public list<studentwrapper> falselist{set;get;}

public list<studentwrapper> olist{set;get;}
public map<id,opportunity__c> mapopp {set;get;}
public boolean wrapcheckbox{set;get;}

public boolean wrapfalse{set;get;}

public boolean taskshow{set;get;}

public taskrepeat(){

falselist = new list<studentwrapper>();

        stwrap = new list<studentwrapper>();


taskshow=true;
    
Batch  = [select id,name,Course_Detail__c,Status__c,End_Date__c,Course_Detail__r.id,Course_Detail__r.name from batch__c where id ='a0KO0000007Kq1jMAC'];

t = new task__c();

wrapfalse =false;
}

    public pagereference studentlist(){
        stwrap = new list<studentwrapper>();

        OppList = [select id ,name,Course_Detail__c,Course_Detail__r.name,Contact__c,Batch__r.Name,Contact__r.name,Batch__r.id,Batch__c from opportunity__c where batch__r.id  ='a0KO0000007Kq1jMAC'];

        for(opportunity__c o : OppList){
            wrapcheckbox=false;
            stwrap.add(new studentwrapper(o,wrapcheckbox));
        }

        studentshow = true;
        wrapfalse =false;
taskshow=false;

        return null;
    }

    public void assigntask(){
    
        ttt = new list<task__C>();
              falselist = new list<studentwrapper>();

        mapopp = new map<id,opportunity__c>();
                    olist = new list<studentwrapper>();
                    
                     Oppclear = new list<opportunity__c>();

        for(studentwrapper aw  : stwrap){
                     // stwrap = new list<studentwrapper>();

           if(aw.check == true ){
           
                   mapopp.put(aw.oppwrap.id , aw.oppwrap);

               
               Task__c tr = new Task__c();
               
               tr.name=t.name;
     
               tr.Batch__c = aw.oppwrap.Batch__r.id;
               
     
               tr.Student__c = aw.oppwrap.Contact__r.id ;
     
    
              ttt.add(tr);
              
              
           }
           
          if(aw.check == false){

           falselist.add(aw);

           wrapfalse =true;

           studentshow = false;
        
           taskshow=false;

           }  
         

     }
     
    
     

   insert ttt;
//remove();
        /*stwrap = new list<studentwrapper>();

 for (integer i = 0; i < stwrap.size(); i++){
     if(stwrap[i].check == true){
    stwrap[i] = null; 
    }
    }*/
     
   //stwrap.clear(olist);
   
   //studentnotselected();
    }
    
   public void remove(){
   
   stwrap = new list<studentwrapper>();

 for (integer i = 0; i < stwrap.size(); i++){
     if(stwrap[i].check == true){
    stwrap[i] = null; 
    }
    }
   
   }

    /*public void studentnotselected(){
    
            ttt = new list<task__C>();

        mapopp = new map<id,list<opportunity__c>>();
                    olist = new list<opportunity__c>();

       falselist = new list<studentwrapper>();


        for(studentwrapper aw  : stwrap){

           if(aw.check == false){
       
           falselist.add(aw);
           }

        }
        
             //     olist.clear();

        wrapfalse =true;

        studentshow = false;
taskshow=false;

    }*/



public class studentwrapper{
    public opportunity__c oppwrap{set;get;}
    public boolean check {set;get;}

    public studentwrapper(opportunity__c at, boolean c){

    oppwrap=at;

    check=c;
    }

}



}


visualforce
=============
<apex:page controller="taskrepeat">

<apex:pageBlock >

<apex:pageBlockSection >
<apex:pageBlockTable value="{!stwrap}" var="fa" >
<apex:column >
<apex:inputCheckbox value="{!fa.check}"/>
</apex:column>
<apex:column value="{!fa.oppwrap.name}"/>

<apex:column />

</apex:pageBlockTable>

</apex:pageBlockSection>
 <apex:commandButton value="continue" action="{!taskassign}" id="aa" onclick="if(!confirm('Do you want to continue?')){return false};"/>

</apex:pageBlock>-->
<!--<apex:pageBlock rendered="{!taskshow}" >
<apex:pageBlockSection >
<apex:inputField value="{!t.name}"/>
<apex:inputField value="{!t.Start_Date__c}"/>
<apex:inputField value="{!t.End_Date__c}"/>
<apex:inputField value="{!t.Description__c}"/>


</apex:pageBlockSection>
<apex:commandButton value="Next" action="{!studentlist}"/>
</apex:pageBlock>

<apex:pageBlock rendered="{!studentshow}">
<apex:pageBlockSection >
<apex:pageBlockTable value="{!stwrap}" var="op">
<apex:column >
<apex:inputCheckbox value="{!op.check}"/>
</apex:column>
<apex:column value="{!op.oppwrap.name}"/>
</apex:pageBlockTable>
 </apex:pageBlockSection>
 <apex:commandButton value="continue" action="{!assigntask}" id="aa" onclick="if(!confirm('Do you want to continue?')){return false};"/>
</apex:pageBlock>

<apex:pageBlock rendered="{!wrapfalse}">

<apex:pageBlockSection >
<apex:pageBlockTable value="{!falselist}" var="fa" >
<apex:column >
<apex:inputCheckbox value="{!fa.check}"/>
</apex:column>
<apex:column value="{!fa.oppwrap.name}"/>

<apex:column />

</apex:pageBlockTable>

</apex:pageBlockSection>
 <apex:commandButton value="continue" action="{!assigntask}" id="aa" onclick="if(!confirm('Do you want to continue?')){return false};"/>

</apex:pageBlock>
</apex:page>

Balayesu ChilakalapudiBalayesu Chilakalapudi
after the below statement,
insert ttt;
Use the below code to remove assigned student from the list
Integer j = 0; 
while (j < stwrap.size()){
   if(stwrap.get(j).check) { 
      stwrap.remove(j);
   }
   else {
      j++; 
   }
 }
Let us know if it helps.