+ Start a Discussion
Carole Reynolds1Carole Reynolds1 

Illegal assignment from LIST<User> to Id

We are trying to assign a user name to a custom field (Assigned_To__c) that is in the standard case object through a trigger along with some other critera - everything is working except for the Assigned To where it is dumping the following error:

 

Save error: Illegal assignment from LIST<User> to Id

Here is the code:

 

if (newCase.Subject=='RS DB Stats') {
                    newCase.Type='RS DB Stats';
                    newCase.Reason='Other';
                    newCase.Assigned_To__c=[select Name from User where Name='Nick Koszykowski'];
                    newCase.Status='Closed';
                }

 

Any thoughts on what we've done wrong?

MoUsmanMoUsman

Hi Ravin Sunkiller,

 

This is not a way to assign an user to the assignto field please try this.

 

if (newCase.Subject=='RS DB Stats') {
                    newCase.Type='RS DB Stats';
                    newCase.Reason='Other';
                    newCase.Assigned_To__c=[select Name from User where Name='Nick Koszykowski'].Id;
                    newCase.Status='Closed';
                }

 

But this is not a good parctice you need to create map of the user and assign user to a case correct way of coding give below you can more polish your code as per your actual requirement.

 

if (newCase.Subject=='RS DB Stats') {

                    newCase.Type='RS DB Stats';
                    newCase.Reason='Other';
                    newCase.Assigned_To__c=userMap('Nick Koszykowski').Id;
                    newCase.Status='Closed';
                }

 

 

map<String,Id> userMap = new map<String,Id>();

for(User u:[select Name from User isActive=true){

     userMap.put(u.Name,u,Id);

}

 

--

Thanks

Usman 

alouie_sfdcalouie_sfdc

The SOQL query is returning a list of users. Instead of

 

newCase.Assigned_To__c=[select Name from User where Name='Nick Koszykowski'];

 

try something like this:

 

List<User> users = [select Name from User where Name='Nick Koszykowski'];
System.assertEquals(1, users.size(), 'Expected a single user to be returned');
newCase.Assigned_To__c = users.get(0).Id;

 

MoUsmanMoUsman

Use SOQL like this [select Name from User where Name='Nick Koszykowski'].get(0).Id instead of [select Name from User where Name='Nick Koszykowski'].Id

Thanks
Usman

SRKSRK

with the above mention HELP full comments
i just want to add one thing

If you use SOQL directly for assingment & it the user of that name doen not exist your code will return null pointer exception
" newCase.Assigned_To__c=[select Name from User where Name='Nick Koszykowski'].Id; " // this may fall in excetion

Also one more thing if a user of "Nick Koszykowsk" exist but it inactive it will still make you fall in some unwanted bussiness sanario when case willl be assing to an inacitive use

 

best pratice if you first query tour desidred record & then check the list size & then take the desidred actions

 

Like this

list<user> tempUsrLst = [select Name from User where Name='Nick Koszykowski' ans active = true limit 1];

 

if (newCase.Subject=='RS DB Stats' && tempUsrLst.size()>0) {
                    newCase.Type='RS DB Stats';
                    newCase.Reason='Other';
                    newCase.Assigned_To__c=tempUsrLst[0].id;
                    newCase.Status='Closed';
                }