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
Amrita Panda 2Amrita Panda 2 

Getting this error " Illegal assignment from String to List<User> "..Need help

  @AuraEnabled
    public static List <User> fetchSearchQueueUser(String SearchUserKeyword, String grpId) {
        try{
            string searchkey = '%'+ SearchUserKeyword + '%';
            List <User> returnList = new List <User> ();
            List <User> lstOfUser;
            String sortField ;
            boolean isAsc;
            List <String> lstOfSubGroup= new List <String> ();
            List <String> lstOfUserIds = new List <String> ();
            set <String> lstOfGrpIds = new set <String> ();
            List <GroupMember> lstOfGroupMember;
            List <GroupMember> lstOfSubGroupMember;
            Set<id> idGroup = new Set<id>();
            //To get subgroup:
            lstOfSubGroupMember =  [SELECT UserOrGroupId,Group.Name from GroupMember WHERE GroupId=: grpId LIMIT 500];
            for(GroupMember gm : lstOfSubGroupMember){
                if(String.Valueof(gm.UserOrGroupId).startsWith('00G')){
                    idGroup.add(gm.UserOrGroupId);
                }
            }
            lstOfGroupMember =  [SELECT Id,UserOrGroupId,GroupId from GroupMember WHERE GroupId  IN: idGroup LIMIT 500];
            for(GroupMember gm : lstOfGroupMember){
                lstOfUserIds.add(gm.UserOrGroupId);
            }
            system.debug(searchkey);
            if(string.isBlank(searchKey)){
              lstOfUser = [select id,name,federationIdentifier,userrole.name,Manager.name,profile.name from user where Id IN: lstOfUserIds and profile.name in ('Service Agent','Work Order Approver') order by name asc LIMIT 500];
                if (sortField != '') {
                     lstOfUser += ' order by ' + sortField; ---------------------------------------------------->error
                  // if isAsc is equal tp ture then set 'asc' order otherwise set 'desc' order.
                 if (isAsc) {
                    lstOfUser += ' asc';                            ---------------------------------------------------->error
                 } else {
                    lstOfUser += ' desc';                          ---------------------------------------------------->error
         }
      }
            }
            else{
                    lstOfUser = [select id,name, federationIdentifier,userrole.name,Manager.name,profile.name from user where Id IN: lstOfUserIds and (federationIdentifier like: searchkey or name like: searchkey)  and profile.name in ('Service Agent','Work Order Approver') order by name asc LIMIT 500];        
                
            }
            system.debug(lstOfUser);     
            return lstOfUser;
        }
        catch(Exception e)
        {
            System.debug('The following exception has occurred: ' + e.getMessage());
            return null;
        }
  1.     }
SandhyaSandhya (Salesforce Developers) 
Hi,

This error says that you are assigning list as value which should be string.

Check the line no where you are getting and try to assign the only one value using get(0).

For example if lstOfUser is list then use lstOfUser.get(0)

Best Regards,
Sandhya
Amrita Panda 2Amrita Panda 2
hi sandhya,

still getting errors.

thanks,
Amrita
Eric Pepin NHEric Pepin NH
In this block of code:

if (sortField != '') {
                     lstOfUser += ' order by ' + sortField; ---------------------------------------------------->error
                  // if isAsc is equal tp ture then set 'asc' order otherwise set 'desc' order.
                 if (isAsc) {
                    lstOfUser += ' asc';                            ---------------------------------------------------->error
                 } else {
                    lstOfUser += ' desc';                          ---------------------------------------------------->error
         }

You have a variable listOfUser which is a List<User> and you are trying to assign strings to it.
Eric Pepin NHEric Pepin NH
if you are trying to create dynamic soql query, then you would need to create a string for the query and then run query.
Right now, you are using soql query and returning list of users, and then trying to assign string to variable of different data type.
Correct code would look something like this (untested, you can make edits and test with your example):

string query = 'select id,name,federationIdentifier,userrole.name,Manager.name,profile.name from user and profile.name in ('Service Agent','Work Order Approver')'; // will need to so something with your lstOfUserIds here, also leave off sort that will be added below

if (sortField != '') {
    query += ' order by ' + sortField;
    
// if isAsc is equal tp ture then set 'asc' order otherwise set 'desc' order.
if (isAsc) {
    query += ' asc';
} else {
    query += ' desc';
}

query += ' LIMIT 500';

lstOfUser = (List<User>)Database.query(query);