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
Deja BondDeja Bond 

Get a default value for a look up to be the current user in apex

Hi all, 
I am trying to have my Candidate Lookup field on my UI to default to the current user
public LightningActivity(Contact candidateRecord){
        this();
        system.debug(candidateRecord);
        string userid = UserInfo.getUserID(); // getting current user info
        List<MTX_Matrix_Candidate__c> user= [SELECT Id, Name, Candidate__c FROM MTX_Matrix_Candidate__c 
        WHERE Id =: userid]; //storing the query into list for user

        if(candidateRecord.Candidate__c == null) //checking to see if  the lookup is populated
        {
            candidateRecord.Candidate__c = user.candidateName; //default to the current user
        }
    }
User-added imageRight now the Candidate Look up is blank, can someone help me with my code?
Egor Gerasimenko 9Egor Gerasimenko 9
candidateRecord.Candidate__c = user.candidateName;
First of all, `user` is a list. How do you expect to get the field `candidateName` from a list of records? Second, you never queried for the field `candidateName`. You only queried for Id, Name, Candidate__c