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
Thakkar ParthThakkar Parth 

Issue of not filter by Id on Task object

I'm performing Search for OwnerId on Task object . I have a User (i.e. Owner) named "Nisarg" . Hence when i search for "Nisarg" it does not give me anything.

When i Debug this in Comsole , following is what i get.What it does is , it searches for Subject and Status.But the requirement is also to search for OnwerId as well on Task which is not happening .. Please help to make any modifications .

Thanks in advance,
Marc.

USER_DEBUG [131]|DEBUG|Select OwnerId,Subject,Status,ActivityDate from Task where (Subject like '%Nisarg%' OR Status like '%Nisarg%')  Order By Subject limit 3 offset 0





private static string JoinSetIds(Set<Id> objIds)
     {
       if(objIds == null || objIds.size() == 0)
       {
         return '';
       }
      List<string> lIds = new List<string>();
       for(Id pId : objIds)
       {
         lIds.add((string)pId);
       }
       return '(\'' + String.join(lIds, '\',\'' ) + '\')';
    } 

 public void queryTasks(){
        
        String qStr2= searchText;
        Set<Id> ownerIds = new Set<Id>();
        String strnormal = '';
        try{
             mydate = date.parse(qStr2);
        }catch(Exception e)
        { }
        
        String strDate = '';
        String ownerIdsCondition = '';
        if(tasks!=null&&tasks.size()>0)
        {
          for(Task t : tasks)
          {
            if(!ownerIds.contains(t.OwnerId))
              ownerIds.add(t.OwnerId);
          }
          ownerIdsCondition = ' OwnerId IN '+JoinSetIds(ownerIds) + ' and ';
        }
        if(mydate != null) {
         // strnormal = String.valueOf(mydate );
          String[] qstr3 = String.valueOf(mydate).split(' ',2); 
          strDate = ownerIdsCondition+' ActivityDate =  '+ qstr3[0] + ' ';
        }else{
       
           strDate  =  ownerIdsCondition+'(Subject like \'%'+searchText +'%\' OR Status like \'%' +searchText+ '%\') ' + ' Order By '  + sortField;
        }
           
      String qStr = 'Select OwnerId,Subject,Status,ActivityDate from Task where '+strDate+' limit ' + QueryLimit + ' offset ' + OffsetSize;  
       System.debug(qStr); 
      tasks = Database.query(qStr);         
    
   }

<apex:column headerValue="OwnerId">
            <apex:outputField value="{!tsk.OwnerId}"/>
        </apex:column>



pbattissonpbattisson
Hi Marc

Looking through your code these lines

if(tasks!=null&&tasks.size()>0) {
	for(Task t : tasks) {
		if(!ownerIds.contains(t.OwnerId))
			ownerIds.add(t.OwnerId);
	}
	ownerIdsCondition = ' OwnerId IN '+JoinSetIds(ownerIds) + ' and ';
}

are where the problem is. I would expect that your tasks list is null or empty and as such this conditional is never met allowing the loop to start. How are you retrieveing or declaring this list?
Thakkar ParthThakkar Parth
OK. Let me show you the controller class of how i'm declaring the list . Below is the code.

public class PagingTasksController1{

    public List<Task> Tasks; 

    
    public Task del;
    public Task taskDel;
    public Integer CountTotalRecords{get;set;}
    public String QueryString {get;set;}
    public Integer OffsetSize = 0;
    private Integer QueryLimit =3 ;
    public List<Task> lstTasks {get;set;}
    public String searchText {get;set;}
    public String rowIndex {get;set;}
 //   public List<Task> attendeeList {get;set;}
    public Date mydate;
    public Integer totalCount {get;set;}
    public string sortField = 'Subject';  // default sort column
    private string sApplySOQL = ''; 
//   public List<Task> delattendeeList {get;set;}

  //  public List<Task> delAttendees {get; set;} 

    /*public PagingTasksController1(ApexPages.StandardController controller) {
 
     taskDel= (Task)controller.getRecord();
     Tasks = [Select id,Subject,Status,ActivityDate from Task where OwnerId =: taskDel.Id];
    // this.Tasks=Tasks[0];
     totalCount = Tasks.size();
 
     delattendeeList = new List<Task>();
     delattendees = new List<Task>(); 
 } */


// the current sort direction. defaults to ascending
    public String sortDir {
        get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
        set;
    }

    // the current field to sort by. defaults to role name
    public String getsortField() {
        return sortField;
    }

    // the current field to sort by. 
    public void setsortField(string value) {
        sortField = value;
    }
             
    // toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
       
        integer iIndex = sApplySOQL.indexOf('Order By');
        if (iIndex > -1){
          sApplySOQL = sApplySOQL.substringBefore('Order By');
          sApplySOQL = sApplySOQL + ' Order By ' + sortField + ' ' + sortDir +  ' limit ' + QueryLimit + ' offset ' + OffsetSize;
        }
        tasks = Database.query(sApplySOQL );
    }
   
    public PagingTasksController1 (){
        //CountTotalRecords= [select count() from Task];
        //String qStr2= '7/23/2014';
           
    }
 
   public List<Task> getTasks(){
        if(tasks == null){
            tasks = new List<Task>();
        }
        return tasks;
    }
 
    public void findTasks(){
        String qStr2 = 'Select count() from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\'';
        CountTotalRecords = Database.countQuery(qStr2);
        queryTasks();
    }
 
   private static string JoinSetIds(Set<Id> objIds)
     {
       if(objIds == null || objIds.size() == 0)
       {
         return '';
       }
      List<string> lIds = new List<string>();
       for(Id pId : objIds)
       {
         lIds.add((string)pId);
       }
       return '(\'' + String.join(lIds, '\',\'' ) + '\')';
    } 
    
        
    public void queryTasks(){
        
        String qStr2= searchText;
        Set<Id> ownerIds = new Set<Id>();
        String strnormal = '';
        try{
             mydate = date.parse(qStr2);
        }catch(Exception e)
        { }
        
        String strDate = '';
        String ownerIdsCondition = '';
        if(tasks!=null&&tasks.size()>0)
        {
          for(Task t : tasks)
          {
            if(!ownerIds.contains(t.OwnerId))
              ownerIds.add(t.OwnerId);
          }
          ownerIdsCondition = ' OwnerId IN '+JoinSetIds(ownerIds) + ' and ';
        }
        if(mydate != null) {
         // strnormal = String.valueOf(mydate );
          String[] qstr3 = String.valueOf(mydate).split(' ',2); 
          strDate = ownerIdsCondition+' ActivityDate =  '+ qstr3[0] + ' ';
        }else{
       
           strDate  =  ownerIdsCondition+'(Subject like \'%'+searchText +'%\' OR Status like \'%' +searchText+ '%\') ' + ' Order By '  + sortField;
        }
           
      String qStr = 'Select OwnerId,Subject,Status,ActivityDate from Task where '+strDate+' limit ' + QueryLimit + ' offset ' + OffsetSize;  
       System.debug(qStr); 
      tasks = Database.query(qStr);         
    
   }
    
      
    public Boolean getDisablePrevious(){
        if(OffsetSize>0){
            return false;
        }
        else return true;
    }

    public Boolean getDisableNext() {
        if (OffsetSize + QueryLimit < countTotalRecords){
            return false;
        }
        else return true;
    }

    public PageReference Next() {
        OffsetSize += QueryLimit;
        queryTasks();
        return null;
    }

    public PageReference Previous() {
        OffsetSize -= QueryLimit;
        queryTasks();
        return null;
    }

      public PageReference save() {
        update tasks;
        return ApexPages.CurrentPage();
     }
     
   public void deleteRow(){
    delete new Task(Id = rowIndex);
    for (Integer i = 0; i < Tasks.size(); i++) {
        if (Tasks[i].Id == rowIndex) {
            Tasks.remove(i);
            break;
        }
    }
}
  }


pbattissonpbattisson
Looking through your code as I mentioned your tasks list is always empty (lines 71-75) and so whent he page is laoded your tasks variable is null, will get set to empty when retieved and won't be set to contain any values until someone calls find, next or previous. 
Thakkar ParthThakkar Parth
Thanks pbattison for the quick response . Yeah so everytime it will show me null with the page loaded first time.

So how can i achieve the goal to search the OwnerId ..Will be grateful if you can please help me with the implementation ..I'm just stucked and not able to move ahead .
pbattissonpbattisson
A quick workaround would be to do the following:

1) change the variable declaration from
public List<Task> Tasks;

to 
public List<Task> tasks {get; set;}
Then remove your getTasks method and in your constructor (line 66) add something like:
tasks = [Select id,Subject,Status,ActivityDate, OwnerId from Task];

This will then at least give you a default list of all tasks within the system to start working with. From there you need to filter as per your own requirements as this list may overrun the governor limits if you have a large enough volume of tasks.
Thakkar ParthThakkar Parth
But this isnt helping me to search by OwnerId , whereas its searching by Subject and Status. Below is the Debug

15:21:41:280 USER_DEBUG [131]|DEBUG|Select OwnerId,Subject,Status,ActivityDate from Task where  OwnerId IN ('00590000002ki3mAAA','00590000002kvyWAAQ') and (Subject like '%Nisarg%' OR Status like '%Nisarg%')  Order By Subject limit 3 offset 0


pbattissonpbattisson
Marc

If you follow the guidance I gave you above and use your existing code after those tweaks your search method will include the Owner Ids of the tasks you have retrieved as per your original code. As you can see in your debug you are having the Owner Ids included. 

If you are wanting to include "Nisargs" user as the Owenr Id then you shoudl remove the code looping through the tasks (lines 113-119) and instead retrieve the User record for the given name and change your filter to use the User Id from that query.