You need to sign in to do that
Don't have an account?
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?
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
The SOQL query is returning a list of users. Instead of
try something like this:
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
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';
}