+ Start a Discussion
SFDC-DMGSFDC-DMG 

How do I set up apex code to log a task for the current user and not the owner?

I'm trying to create a apex code that will be triggered by a button on the lead/contact page to log an activity. It's currently set up to assign the task to the lead owner, but I'm trying to update the code so that it will assign the task to the current user. I keep running into different errors while I try a bunch of stuff, but I'm having a hard time understanding what I need to do and how I should reference this because I'm a novice updating an existing code. I'm not sure where the "Current User" needs to be defined, and when I've tried I've gotten error message that my value doesn't exist or it works and then when I hit the button I get an error message saying that there are multiple values.

I have been trying to find help online, but, like I said, I'm a beginner and I keep getting lost in the apex language.

Thanks!

Here's the first part of the code -

global class logAttempts {
 
  WebService static String createAttempt(
    String sAttemptType,
    String sCallerType, 
    Id objectId) {
      
      List<Task> lstTasks = new List<Task>();
    
    try {
       
      If (sCallerType == 'Lead') {
        Lead newLead = 
          [Select Id, OwnerId, Status, Lead_Stage__c,
           Title, Email
           from   Lead 
           where  ID = :objectId];


Best Answer chosen by Admin (Salesforce Developers) 
dkadordkador

UserInfo.getUserId() will return the user ID of the context user.  Try setting that to the OwnerId field.