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
shobana shobanashobana shobana 

how to show the open task as separate related list for lead in pagelayout?

Hi  everyone
This is my first wrapper class.
I would like to show open task as seperate section on my lead page layout.
For that i took a standard controller with extension.In this scenario i have finished upto 3steps and I got output also.
Step 1; showing  task record related to lead object.
Step 2:i have created new task button.
Step 3: i have show edit option for each records of task.

My 3rd and 4th step is to add a checkbox and close button for that section.whenever am clicking  checkbox and close botton i want to show that particular record in activity history.
for this concept i took wrapper class concept but its showing error " opentasks Compile Error: Variable does not exist: status
my controller page

public class opentasks {
public List<OpenActivity> open{get;set;}
    public Id leadld{get;set;}
    Public boolean allbool{get;set;}
    public list<wrapperclass> listwrapper{get;set;}
    public list<lead> c{get;set;}
    public opentasks(ApexPages.StandardController controller) {
        leadld = ApexPages.CurrentPage().getparameters().get('id');
         c =[SELECT Name, 
                                  (Select Id,Subject,IsTask, WhoId, ActivityDate,
                                    Status, Priority, OwnerId FROM OpenActivities WHERE IsTask=True) 
                            FROM Lead WHERE Id = :leadld];
                         
        if(c.size() > 0){
            open = c[0].OpenActivities;
        }
        
        }
    public opentasks(){
   list<openactivity> open1 =new list<openactivity>(); 
  list<lead>   le =[SELECT Name, 
                                  (Select Id,Subject,IsTask, WhoId, ActivityDate,
                                    Status, Priority, OwnerId FROM OpenActivities WHERE IsTask=True) 
                            FROM Lead WHERE Id = :leadld];
                         
        if(c.size() > 0){
            open1 = c[0].OpenActivities;
        }
    for(Openactivity k:open1)
    {
   
    listwrapper.add(new Wrapperclass(k));
    }
    }
    
    
    
    public class wrapperclass
    {
    public  boolean checked{get;set;}
    public Openactivity k{get;set;}
    public Wrapperclass(openactivity k)
    {
    this.k=k;
    }
    }
    public void close()
    {   
    list<openactivity> listofopen=new list<openactivity>();
    list<wrapperclass> listofwrapper=new list<wrapperclass>();
    for(wrapperclass w:listwrapper)
    {
   if(w.checked==true)
   {
   system.debug(w);
   w.status='completed';   HERE  AM GETTING ERROR
   listofopen.add(w.k);
   
   }
   
   
    else
    {
    listofwrapper.add(w);
    }
    }
    if(listofopen.size()==0)
    {
     ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Select atleast one column t'));
    }}   
 }

can anyone help me out


Thank you in advance


 
Best Answer chosen by shobana shobana
Bhanu MaheshBhanu Mahesh
Hi Shobana,

Try the below code

VF Page
<apex:page standardController="Lead" extensions="opentasks" >
    <apex:form >
        <apex:pageBlock title="open Tasks" mode="edit" id="opnTsks"> 
            <apex:Messages />         
            <center><a href="https://ap1.salesforce.com/00T/e?who_id={!leadld}&retURL={!leadld}" target="_top"><button type="button">New Task</button></a>
                <apex:commandButton value="close" action="{!close}" rerender="opnTsks"/>
            </center>&nbsp;&nbsp;
            <apex:pageBlockTable value="{!listwrapper}" var="each" >
                <apex:column headerValue="Action">
                     <apex:inputCheckbox value="{!each.checked}"/>
                    &nbsp;|&nbsp;
                    <apex:outputlink value="/{!each.k.id}/e?retURL={!each.k.WhoId}" target="_top"> Edit </apex:outputlink>
                   </apex:column>
                <apex:column headerValue="Subject" value="{!each.k.Subject}"/>
                <apex:column headerValue="ActiveDate" value="{!each.k.ActivityDate}"/>
                <apex:column headerValue="Status" value="{!each.k.Status}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
     </apex:form> 
</apex:page>

Extension
 
public class opentasks {
    public List<OpenActivity> open{get;set;}
    public Id leadld{get;set;}
    public list<wrapperclass> listwrapper{get;set;}
   
    public opentasks(ApexPages.StandardController controller){
        leadld = ApexPages.CurrentPage().getparameters().get('id');
        list<task> open1 =new list<Task>(); 
        listwrapper = new list<wrapperclass>();
        list<lead>   le =[SELECT Name, (Select Id,Subject, WhoId, ActivityDate,Status, Priority, OwnerId FROM Tasks WHERE IsClosed=false) 
                            FROM Lead WHERE Id = :leadld];
        if(!le.isEmpty()){
            for(Task k:le[0].Tasks)
            {
                listwrapper.add(new Wrapperclass(k));
            }
        }
    }
    
    public class wrapperclass
    {
        public  boolean checked{get;set;}
        public Task k{get;set;}
        public Wrapperclass(Task k){
            this.k=k;
        }
    }
    public PageReference close()
    {   
        list<Task> listofopen=new list<Task>();
        if(!listwrapper.isEmpty()){
            for(Integer i=0; i < listwrapper.size();i++)
            {
                wrapperclass w = listwrapper[i];
                if(w.checked==true){
                    system.debug(w);
                    w.k.status='completed';
                    listofopen.add(w.k);
                    listwrapper.remove(i);
                }
            
            }
        }
        if(listofopen.isEmpty())
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Select atleast one column t'));
        }
        else{
            update listofopen;    
        }
        return null;
    }   
 }
Hope it may help you

Regards,
Bhanu Mahesh
 

All Answers

Bhanu MaheshBhanu Mahesh

Hi Shobana,

You should acces it using w.k.Status because Status is in OpenActivity which is an attribute of Wrapper class

But you cannot update OpenActivity object. It is Read Only object. 
Please refer the below link
https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_openactivity.htm
https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_openactivity.htm
So Intead of fetching records from OpenActivity, fetch the records from task Object and display it in the Vf page and then you can update the task records

Your SOQL Query will be 

list<lead> le =[SELECT Name, (Select Id,Subject,IsTask, WhoId, ActivityDate, Status, Priority, OwnerId FROM Tasks WHERE isClosed = false) FROM Lead WHERE Id = :leadld];

Please provide your vf page if possible

Regards,
Bhanu Mahesh
Arunkumar RArunkumar R
Hi,

You need to change w.status to k.status. Since In OpenActivity you have a field Status. But in you just refering the status field with Wrapper instance, that why you are getting error.

Take a look on the below example, it will be helpful to you,

https://developer.salesforce.com/page/Checkbox_in_DataTable
shobana shobanashobana shobana
Thank you for ypour replay
I have changed my code still am getting same error
my controller

public class opentasks {
public List<OpenActivity> open{get;set;}
    public Id leadld{get;set;}
    Public boolean allbool{get;set;}
    public list<wrapperclass> listwrapper{get;set;}
    public list<lead> c{get;set;}
    public opentasks(ApexPages.StandardController controller) {
                 leadld = ApexPages.CurrentPage().getparameters().get('id');
                        c =[SELECT Name, 
                               (Select Id,Subject,IsTask, WhoId, ActivityDate,
                                Status, Priority, OwnerId 
                                FROM OpenActivities
                                WHERE IsTask=True) 
                            FROM Lead
                            WHERE Id = :leadld];
                                       
        if(c.size() > 0){
            open = c[0].OpenActivities;
        }
       system.debug(open);
        }
       
        public opentasks(){
             list<Schema.task> t =new list<Schema.task>();
             list<lead> le =[SELECT Name, (Select Id,Subject,WhoId, ActivityDate, Status, Priority, OwnerId 
                             FROM Tasks WHERE isClosed = false) 
                             FROM Lead WHERE Id = :leadld];
                 if(le.size()>0){
                  t = le[0].Tasks;  
                  }       
                  for(Schema.task k:t){
                  listwrapper.add(new Wrapperclass(k));
                  }
    
        }
    
    
    
    
    public class wrapperclass
    {
    public Task k{get;set;}
    public Wrapperclass(Task k)
    {
    this.k=k;
    
    system.debug(k);
    }
    }
    public void close(){   
    
        if(allbool==true)
        {
         for(wrapperclass w:listwrapper){
         system.debug(w);
       w. k.Status='completed';
      
       }
    }
    }
    }

My visual force page

<apex:page standardController="Lead" extensions="opentasks" >
    <apex:form >
        <apex:pageBlock title="open Tasks" >          
<center><a href="https://ap1.salesforce.com/00T/e?who_id={!leadld}&retURL={!leadld}" target="_top"><button type="button">New Task</button></a>
<apex:commandButton value="close" action="{!close}"/>
</center>&nbsp;&nbsp;
                <apex:pageBlockTable value="{!open}" var="each"  >
                <apex:column headerValue="Action">
                 <apex:inputCheckbox value="{!allbool}"/>
                &nbsp;|&nbsp;
                <apex:outputlink value="/{!each.id}/e?retURL={!each.WhoId}" target="_top"> Edit </apex:outputlink>
                   </apex:column>
                <apex:column headerValue="Subject" value="{!each.Subject}"/>
                <apex:column headerValue="ActiveDate" value="{!each.ActivityDate}"/>
                <apex:column headerValue="Status" value="{!each.Status}"/>
                </apex:pageBlockTable>
            </apex:pageBlock>
     </apex:form> 
</apex:page>

can anyone please help be out 
Thank you
Bhanu MaheshBhanu Mahesh
Hi Shobana,

Try the below code

VF Page
<apex:page standardController="Lead" extensions="opentasks" >
    <apex:form >
        <apex:pageBlock title="open Tasks" mode="edit" id="opnTsks"> 
            <apex:Messages />         
            <center><a href="https://ap1.salesforce.com/00T/e?who_id={!leadld}&retURL={!leadld}" target="_top"><button type="button">New Task</button></a>
                <apex:commandButton value="close" action="{!close}" rerender="opnTsks"/>
            </center>&nbsp;&nbsp;
            <apex:pageBlockTable value="{!listwrapper}" var="each" >
                <apex:column headerValue="Action">
                     <apex:inputCheckbox value="{!each.checked}"/>
                    &nbsp;|&nbsp;
                    <apex:outputlink value="/{!each.k.id}/e?retURL={!each.k.WhoId}" target="_top"> Edit </apex:outputlink>
                   </apex:column>
                <apex:column headerValue="Subject" value="{!each.k.Subject}"/>
                <apex:column headerValue="ActiveDate" value="{!each.k.ActivityDate}"/>
                <apex:column headerValue="Status" value="{!each.k.Status}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
     </apex:form> 
</apex:page>

Extension
 
public class opentasks {
    public List<OpenActivity> open{get;set;}
    public Id leadld{get;set;}
    public list<wrapperclass> listwrapper{get;set;}
   
    public opentasks(ApexPages.StandardController controller){
        leadld = ApexPages.CurrentPage().getparameters().get('id');
        list<task> open1 =new list<Task>(); 
        listwrapper = new list<wrapperclass>();
        list<lead>   le =[SELECT Name, (Select Id,Subject, WhoId, ActivityDate,Status, Priority, OwnerId FROM Tasks WHERE IsClosed=false) 
                            FROM Lead WHERE Id = :leadld];
        if(!le.isEmpty()){
            for(Task k:le[0].Tasks)
            {
                listwrapper.add(new Wrapperclass(k));
            }
        }
    }
    
    public class wrapperclass
    {
        public  boolean checked{get;set;}
        public Task k{get;set;}
        public Wrapperclass(Task k){
            this.k=k;
        }
    }
    public PageReference close()
    {   
        list<Task> listofopen=new list<Task>();
        if(!listwrapper.isEmpty()){
            for(Integer i=0; i < listwrapper.size();i++)
            {
                wrapperclass w = listwrapper[i];
                if(w.checked==true){
                    system.debug(w);
                    w.k.status='completed';
                    listofopen.add(w.k);
                    listwrapper.remove(i);
                }
            
            }
        }
        if(listofopen.isEmpty())
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Select atleast one column t'));
        }
        else{
            update listofopen;    
        }
        return null;
    }   
 }
Hope it may help you

Regards,
Bhanu Mahesh
 
This was selected as the best answer
Arunkumar RArunkumar R
I have save your controller in my dev org? Did you received this error?  Illegal variable declaration: k.Status at line 56 column 11

In your code there is space between w and k. So remove the space   w.k.Status='completed';  and try it.

After that you code get saved to me.
shobana shobanashobana shobana
Thank you for your reply Mr. Arunkumar and  Mr.Bhanu Mahesh

 Arunkumar Actually i am getting  error as" Variable does not exist: Status "
  
Thank you for sending me code Banu mahesh, actually i have tried your code but showing error in 13th line as Loop variable must be an SObject or list of Task .




 
Bhanu MaheshBhanu Mahesh
Hi Shobana,

It is working fine in my trial org.

As you can see that the iteration is over the Task list.

Might be the error because you have a class in your org with name 'task'. So please change that class name and try again.

Regards,
Bhanu Mahesh
shobana shobanashobana shobana
Thank you for your replay i will try that one...
 
shobana shobanashobana shobana
Hi Bhanu Mahesh
Thank you  its working ..
But the thing is  when am selecting bulk record its closing only two record.If am  again selecting bulk records only one record geting close.
 
Bhanu MaheshBhanu Mahesh
Hi Shobana,

Add i--; in the if of close() after this statement listwrapper.remove(i);

if(w.checked==true){
         system.debug(w);
         w.k.status='completed';
         listofopen.add(w.k);
         listwrapper.remove(i);
         i--;
 }

Try now. It should work.

Previously it is not iterating through all  the records as I am removing the record from list and checking with size, It is iterating through all the records. Now it will.

Regards,
Bhanu Mahesh
shobana shobanashobana shobana
HI Bhanu Mahesh

Thanks a lot its working...