0

I need to take user name from the select option picklist from vf page and on the basis of that value i need to show tasks related to that user.

i am using below code but unable to get exact reason why i am not getting any value in string username.

below is my code. Thanking in advance for help.

<apex:outputLabel value="From:"/>&nbsp;&nbsp;&nbsp;&nbsp;
       <apex:selectList value="{!username}" multiselect="false" size="1" >
            <apex:selectOptions value="{!UserList}"/>
        </apex:selectList>

public with sharing class TaskAssignmentController {
 public String username{get;set;} 

public List<SelectOption> getUserList()
    {
       List<User> ulist = new List<User>();
       getUsers = new List<SelectOption>();
       ulist = [Select id, Name, UserRole.Name, IsActive from User where IsActive=true and UserRole.Name='Caller'];

       if(ulist.size()>0)
         {
       system.debug('this is user list'+ulist);
       getUsers.add(new SelectOption('0001', '--Select--'));
       for(User u: ulist)
         {
            getUsers.add(new SelectOption(u.Id, u.Name));
         }
        }
         return getUsers;
    }

    public string selectedFromUser()
    {
       return username;
    }

    public void setSelectedFromUser(string username)
    {
        this.username = username;
    }

and below is the method i m calling on button click to get tasks related to that user

 public void showTasks() {

     Set<Id> taskids = new Set<Id>();
     if(wrapTaskList == null) {
        wrapTaskList = new List<wrapTask>();
           }
           try
           {
            System.debug('Inside try');
           User u = [Select Name, id from User where Name=:username LIMIT 1];
           system.debug('this is the selected usr'+u);

               for(Task t: [select Id, OwnerId, Owner.Name, Type, Who.Name, Subject, ActivityDate, Status from Task where Status='Pending' and Subject=:taskType
               and ActivityDate>=:duedate and ActivityDate<=:enddate and Owner.Name=:username limit 50]) {
                  taskids.add(t.id);
                  System.debug('Inside for loop');
                wrapTaskList.add(new wrapTask(t));

                if(wrapTaskList.size()>0)
                showtable = true;
                {
                   System.debug('Inside if');
                   system.debug('List of tasks'+wrapTaskList);
                }
              }

              countTask = [Select count() from Task where id in: taskids];
              System.debug('this is count of task'+countTask);
            }
            catch(Exception e)
            {
              System.debug('##Exception occured'+e.getMessage());
              if(wrapTaskList.size()==0)
              ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.INFO,'No task found!'));
            }

    }

I always get No task found! message

Please provide me the guidance required. I know its easy and i m missing on small detail.

  • 1
    Basically, you're hiding an exception. Check your debug logs. – sfdcfox Oct 30 '18 at 19:17
1

You are mixing up the user's Name and Id.

   <apex:selectList value="{!username}" multiselect="false" size="1" >
        <apex:selectOptions value="{!UserList}"/>
    </apex:selectList>

Here, your select list's selected item is bound to the variable username.

But your SelectOptions have the user's name as their label, and the user's Id as their value. It's the select option value that is placed in the bound variable.

        getUsers.add(new SelectOption(u.Id, u.Name));

When you query for users

       User u = [Select Name, id from User where Name=:username LIMIT 1];

you treat the variable username as if it contains a user name, but it actually contains a user Id. Change the WHERE clause to look for Id, and it should return a result. You should also check, however, that a value other than your --select-- choice has been chosen.

At present, it's throwing a QueryException that your try/catch block then eats.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.