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
Evelyn FayadEvelyn Fayad 

'Last Activity Assigned To' custom field on the lead object using an apex trigger

Hi everyone, 

I'm trying to create a custom field that populates with the name of whomever had the last activity on the lead. I'm currently trying to do this using a trigger.

This is the Apex Trigger that I have written:

trigger lastactassigned on task(after insert) {
  map<id,lead> leads = new map<id,lead>();
  for(task record:trigger.new) {
    if(record.ownerid.getsobjecttype()==user.sobjecttype) {
      if(record.whoid!=null&&record.whoid.getsobjecttype()==lead.sobjecttype) {
        leads.put(record.whoid,new lead(id=record.whoid,Last_Act_Assigned_To_Test__c=record.ownerid));
      }
    }
  }
  update leads.values();
}


AND this is the Apex Class that I have written to test the trigger:

@isTest 
public class lastactassignedTest1 
{
    static testMethod void testMethod1() 
    {
        User user = new User();
        
        Lead lead = new Lead();
        lead.LastName ='Test';
        lead.FirstName ='New';
        // Add all required field
        insert lead;
        
        Task task = new Task();
        task.WhoId = user.Id;
        task.Subject = 'Other';
        task.Status = 'Not Started';
        task.Description = 'New  Work';
        insert task;
    }
}

So as of right now, both of them save without any problems; however, when I hit 'run test' on the Apex Class, it fails. 

I'm thinking that there may be something wrong with the trigger, but I am not completely sure. I would love some input!

Thank you,
Evelyn 
Best Answer chosen by Evelyn Fayad
Prateek Singh SengarPrateek Singh Sengar
Hi Evelyn,
There are couple of things that doesnt seem correct.
  1. In your test class you have a statement User user = new User(); You have instantiated a user object but not created the user record. You need to insert the user record that you are trying to create in test class. Try following the below link on best practices on how to create user http://www.nimbleams.com/blog/2014/1/19/apex-testing-username-creation-best-practice/
  2. In your trigger you have the logic
  3. if(record.ownerid.getsobjecttype()==user.sobjecttype) {
          if(record.whoid!=null&&record.whoid.getsobjecttype()==lead.sobjecttype) {
            leads.put(record.whoid,new lead(id=record.whoid,Last_Act_Assigned_To_Test__c=record.ownerid));
          }
        }

    It checks if the ownerId of the task is of type user, then it verifies if the whoid of task is Lead. However in your test class you are assigning the tasks whoid to user record instead of task. As per the trigger the test class should be 
    Task task = new Task();
    task.WhoId = lead.Id;
    task.Subject = 'Other';
    task.Status = 'Not Started';
    task.Description = 'New  Work';
    task.ownerid = user.id;
    insert task;

    Hope this helps.

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi Evelyn,
There are couple of things that doesnt seem correct.
  1. In your test class you have a statement User user = new User(); You have instantiated a user object but not created the user record. You need to insert the user record that you are trying to create in test class. Try following the below link on best practices on how to create user http://www.nimbleams.com/blog/2014/1/19/apex-testing-username-creation-best-practice/
  2. In your trigger you have the logic
  3. if(record.ownerid.getsobjecttype()==user.sobjecttype) {
          if(record.whoid!=null&&record.whoid.getsobjecttype()==lead.sobjecttype) {
            leads.put(record.whoid,new lead(id=record.whoid,Last_Act_Assigned_To_Test__c=record.ownerid));
          }
        }

    It checks if the ownerId of the task is of type user, then it verifies if the whoid of task is Lead. However in your test class you are assigning the tasks whoid to user record instead of task. As per the trigger the test class should be 
    Task task = new Task();
    task.WhoId = lead.Id;
    task.Subject = 'Other';
    task.Status = 'Not Started';
    task.Description = 'New  Work';
    task.ownerid = user.id;
    insert task;

    Hope this helps.
This was selected as the best answer
Evelyn FayadEvelyn Fayad
Hi Prateek! That helped clear a few things up. I created a user record, but I'm still getting some compile errors. The apex trigger is the same as above, but I've changed a few things in the class. 

Here is the new class test I have written:

@isTest 
public class lastactassigned 
{
    public static User insertStandardUser(Id standardProfileId){
        User standardUser = new User(
        Username ='standarduser@testorg.com',
        Alias = 'standt',
        email = 'standarduser@testorg.com',
        emailencodingkey = 'UTF-8',
        LanguageLocaleKey = 'English',
        LocaleSidKey = 'en_US',
        Profile = [SELECT Id FROM Profile WHERE Name='Standard User'],
        LastName = 'Testing',
        TimeZoneSidKey = 'America/Los_Angeles'
        );
        insert standardUser;
  
        Lead lead = new Lead();
        lead.LastName = 'Test';
        lead.FirstName = 'New';
        insert lead;
        
        Task task = new Task();
        task.WhoId = lead.Id;
        task.Subject = 'Other';
        task.Status = 'Completed';
        task.Description = 'New  Work';
        task.ownerid = standardUser.id;
        insert task;

    }
}

Im getting a compile error that reads Error: Compile Error: Non-void method might not return a value or might have statement after a return statement. at line 32 column 9. (which is the 'insert task;' line)

Any ideas on why I'm getting this error?

I really appreciate your help.

Evelyn
 
Prateek Singh SengarPrateek Singh Sengar
Hi Evelyn,
The issue is with the test method signature. Please replace 
public static User insertStandardUser(Id standardProfileId)
with
 
public static void insertStandardUser()

You might also want to change the username 'standarduser@testorg.com' to something more unqiue.
 
Evelyn FayadEvelyn Fayad
Thank you! I figured that out shortly after I posted. Still getting familiar with writing code.  

Thank you so much for all of your help. I was able to package and deploy it. Works wonderfully.