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
Abhilash MAbhilash M 

Hi! I am trying to print the username in a Lightning Component.The code is not working. Please fix the code.

public class OwnerController
{


@AuraEnabled
public static List<Contact> getOwner()
{
List<Contact> Owner =
[select name from user where id =: Userinfo.getUserId()].name;];

return Owner;
}


    
}
Paul_BoikoPaul_Boiko
You are running query against user object and trying to assign result of the query to List of Contact sObjects. You need to assign to List of User sObjects. And then you try to get name from the user. If you need to return user object of logged in user you need to change code to this:
public class OwnerController {

      @AuraEnabled
      public static User getOwner() {
             User Owner = [select name from user where id =: Userinfo.getUserId()];
             return Owner;
       }    
}
if you need to return just name of the user then you can do this like this: 
 
public class OwnerController {

      @AuraEnabled
      public static String getOwner() {
             String Owner = [select name from user where id =: Userinfo.getUserId()].Name;
             return Owner;
       }    
}

 
Abhilash MAbhilash M
How to print the above user name on a component?
UX DeptUX Dept
Hi,
Could you please show me how to run an Apex test on this code?